-2

I'm trying to create a webpage that shows me the contents of an SQL-Database. This works, but now i'm creating a search option that allows for filtering.

I can't seem to get the superglobal $_GET to work though, and I always get his error:

Parse error: syntax error, unexpected '$_GET' (T_VARIABLE) HTML/PHP code:

<header>
    <div id="divSearchBar">
        <form name="SearchBar" method="get" action="index.php">
            <input type="text" name="txtSearchBar"/>
            <input type="submit" value="Search"/>
            <input type="submit" name="sbmAddItem" value="Add Item"/>
        </form>
    </div>
</header>
<main>
        <?php
            include_once "Repository.php";

            $r = new Repository();
            $tmp = "";
            if (isset $_GET["txtSearchBar"])
            {
                $tmp = $_GET["txtSearchBar"];   
            }

            $myItems = $r->GetItems(tmp);
            for($i=0;$i<count($myItems);$i++)
            {
                print '<div id="divGen"><p id="pGen">' . $myItems[$i]->Item . '</p><p id="pGen2">' . $myItems[$i]->Category . "</p></div>";
            }
        ?>
</main>
Qirel
  • 21,424
  • 7
  • 36
  • 54
MyNameIsGuzse
  • 199
  • 1
  • 3
  • 15

3 Answers3

2

You need to use parentheses with the isset function

if (isset($_GET["txtSearchBar"])) // <-- Here
{
    $tmp = $_GET["txtSearchBar"]; 
}
Jerodev
  • 29,019
  • 11
  • 72
  • 94
1

change your code to

if (isset($_GET["txtSearchBar"]))
{
      $tmp = $_GET["txtSearchBar"];   
}
B. Desai
  • 16,092
  • 5
  • 22
  • 43
1

Use isset() function in round brackets

<?php
            include_once "Repository.php";

            $r = new Repository();
            $tmp = "";
            if (isset ($_GET["txtSearchBar"]))
            {
                $tmp = $_GET["txtSearchBar"];   
            }

            $myItems = $r->GetItems(tmp);
            for($i=0;$i<count($myItems);$i++)
            {
                print '<div id="divGen"><p id="pGen">' . $myItems[$i]->Item . '</p><p id="pGen2">' . $myItems[$i]->Category . "</p></div>";
            }
        ?>
Amit Gaud
  • 706
  • 6
  • 13