Jump to content

help with dropdown menu search


hawkontvn

Recommended Posts

hey need some help here :)

 

created a dropdown menu populated by a 'category'-table from my database.

code:

 

<?php
                                $query = "SELECT categoryID AS ci,
                                    categoryName AS cn FROM category
                                    ORDER BY categoryName";
                                $result = mysql_query($query);

                                while ($row = mysql_fetch_array($result))
                                {
                                    $id = $row['ci'];
                                    $cat = $row['cn'];
                                    $options.="<option value='$id'>".$cat."</option>";
                                }
                                ?>

                                <form method="get" action="/biblio/">
                                    <select name="showcat">
                                        <option value="">
                                            Choose category
                                            <?=$options?>
                                        </option>
                                    </select>
                                    <input type="submit" value="Search" />
                                </form>

 

Now the dropdown menu is working, but im trying to have it print out all books in the particular category i selected.

 

Code so far:

<?php
                                $list_cats = mysql_query("SELECT * FROM category
                                    ORDER BY categoryName;");

                                while($row = mysql_fetch_array($list_cats))
                                {
                                    $cid = $row['categoryID'];
                                    $cname = $row['categoryName'];
                                }

                                if($_GET['showcat'])
                                {
                                    $cid = $_GET['showcat'];
                                    $cname = $_GET['showcat'];

                                    echo "<br /><p><b>Books in picked category:</b></p>";

                                    $query = "SELECT * FROM category
                                    WHERE categoryName = $cname";

                                    $res = mysql_query($query) or die(mysql_error());

                                    while($row = mysql_fetch_array($res))
                                    {
                                        $cid = $row['categoryID'];
                                        $cname = $row['categoryName'];
                                    }
                                                                       
                                }

                                ?>

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/201667-help-with-dropdown-menu-search/
Share on other sites

Your form's a little wrong. Try changing to this:

<?php
                                $query = "SELECT categoryID AS ci,
                                    categoryName AS cn FROM category
                                    ORDER BY categoryName";
                                $result = mysql_query($query) or die (mysql_error());

echo '<form method="get" action="/biblio/">';
echo '<select name="showcat">';
echo'<option value="">Choose category</option>
   while ($row = mysql_fetch_array($result))
                                {
                                    $id = $row['ci'];
                                    $cat = $row['cn'];
                                    echo'<option value="'.$id.'">'.$cat.'</option>';
                                }
                                  echo '</select>'
                                    echo '<input type="submit" value="Search" />'
                                echo'</form>'
?>                          

 

Also never open php tags with just <? it's not common for modern servers to support short tags for php so as a best practice you should always use <?php to open them.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.