magicmoose Posted February 27, 2007 Share Posted February 27, 2007 Hi, I am just getting into php and am struggling to get it to do anything useful. I have a drop down box on one page, with 4 possible options: title, author, artist and publisher. When the user clicks submit, the following script is run. I just want it to sort the results based on the field the user selects from the drop down box, but at the moment it sorts by title no matter what they choose. Can anyone help me work out where I'm going wrong? Thanks in advance. Here's the code. <? $dbServer=mysql_connect("localhost","",""); if (!$dbServer) {echo "Failed to connect to MySQL"; exit; } mysql_select_db("database",$dbServer); if ($_POST['Comics']= "Title"){ $sql ="SELECT * FROM comics ORDER BY title"; } elseif ($_POST['Comics']= "Author") { $sql ="SELECT * FROM comics ORDER BY author"; } elseif ($_POST['Comics']= "Artist") { $sql ="SELECT * FROM comics ORDER BY artist"; } elseif ($_POST['Comics']= "Publisher") { $sql ="SELECT * FROM comics ORDER BY publisher"; } $queryResult=mysql_query($sql); if (mysql_error()) { echo "Problem with Query<BR>"; echo "The following error message was returned from MySQL:<BR>"; echo mysql_error(); exit; } if (mysql_num_rows($queryResult)==0) { echo "No results that match your enquiry"; } else { while ($dbRecord=mysql_fetch_array($queryResult)) { echo "found: ".$dbRecord["title"].", ".$dbRecord["author"].", ".$dbRecord["artist"].", ".$dbRecord["publisher"]."<BR>"; } } ?> EDIT: Here's the HTML <form action="includes/dispbytitle.php" method="POST"> <select name="Comics"> <option value="Title">Title</option> <option value="Author">Author</option> <option value="Publisher">Publisher</option> <option value="Artist">Artist</option> </select> <INPUT type="submit" value="Search"> </form> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 let's see the html form. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 27, 2007 Share Posted February 27, 2007 Why not just do it like this? <?php $comics = $_POST['Comics']; if (isset($comics)){ $sql ="SELECT * FROM comics ORDER BY $comics"; } else { $sql ="SELECT * FROM comics ORDER BY title"; } ?> Quote Link to comment Share on other sites More sharing options...
magicmoose Posted February 27, 2007 Author Share Posted February 27, 2007 I am not yet good enough to always work things out the easiest way possible Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 27, 2007 Share Posted February 27, 2007 Did the code work? Quote Link to comment Share on other sites More sharing options...
magicmoose Posted February 28, 2007 Author Share Posted February 28, 2007 It works for two fields, but I'm not sure how to extend this to sort by other fields. I think I may have misunderstood the way drop down menus work with php. Is it based simply on this line "<select name="Comics">" rather than the values of that are selected? EDIT: Ignore the above, it does work perfectly, just took me way longer than it should have to see what was happening Thanks! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 28, 2007 Share Posted February 28, 2007 Okay, lets see if I can explain this. You have this code: <select name="Comics"> <option value="Title">Title</option> <option value="Author">Author</option> <option value="Publisher">Publisher</option> <option value="Artist">Artist</option> </select> Lets take this line for example: <option value="Author">Author</option> the value of $_POST['Comics'] is going to be "Author" because you have the option VALUE set to "Author". If you didn't put a value it would be whatever was inbetween the <option>'s..which in this case would be the same thing. You should probably change all the values you set to lowercase since the cols in your table are lowercase: <select name="Comics"> <option value="title">Title</option> <option value="author">Author</option> <option value="publisher">Publisher</option> <option value="artist">Artist</option> </select> So when I gave you this chunk of code: <?php $comics = $_POST['Comics']; if (isset($comics)){ $sql ="SELECT * FROM comics ORDER BY $comics"; } else { $sql ="SELECT * FROM comics ORDER BY title"; } ?> ORDER BY $comics is going to be different each time depending on what option they chose. If they chose "authors" that line is going to read ORDER BY authors since the value of $comics is the POST value. Does that make sense at all? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.