Rossini Posted May 8, 2010 Share Posted May 8, 2010 Hello, I am new here and making a web page to display player stats from a video game. That stats are inputted with MySQL, and have everything set up when it comes to connecting to the database and displaying the results. What I want to add now, is to have a drop down menu that sorts the table based on certain criteria. (the default is sorting by kills, but also want by name, deaths, etc.) I been trying to figure it out but with no luck. What I did was make a drop down menu and a submit button that posted one of the selected variables which equated to that sort statement...but nothing happens it just keeps on showing the default query. Here is the page:http://steven.branzone.com/legions.phpHere is the code without the drop-down menu and submit button: <html><head><style type="text/css">body {background-image:url('legions.jpg');} body {color:white;}table{width:50%;}</style></head><body> <?php$conn = mysql_connect('XXX', 'XXX', 'XXX');mysql_select_db("stevenbr_legions", $conn);$resultall = mysql_query("SELECT * From stats ORDER BY 'kills'");?><table border="1" align="center"><tr><td>Name</td><td>Kills</td><td>Deaths</td><td>Score</td><td>Games</td></tr><?phpwhile($row = mysql_fetch_array($resultall)) {$name = $row['name'];$kills = $row['kills'];$deaths = $row['deaths'];$score = $row['score'];$games = $row['games']; echo "<tr><td>$name</td><td>$kills</td><td>$deaths</td><td>$score</td><td>$games</td>"; }?></body></html> Thanks for the help. Link to comment Share on other sites More sharing options...
chibineku Posted May 8, 2010 Share Posted May 8, 2010 Well, what does the drop down look like? And how are you handling it?I would do something like: <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>"><select name="sort"><option value="kills" selected="selected" name="kills">kills</option><option value="name" name="name">name</option><option value="death" name="death">death</option><option value="score" name="score">score</option><option value="games" name="games">games</option></select><input type="submit" value="order"></form> Which will post the page to itself. Then add this to the PHP already on the page, before the query, obviously: if(isset($_POST["sort"])) {$sort = mysqli_real_escape_string($conn, $_POST["sort"]);}$order = isset($sort)?$sort:"kills"; And the query becomes: $resultall = mysql_query("SELECT * From stats ORDER BY $order"); Link to comment Share on other sites More sharing options...
Rossini Posted May 8, 2010 Author Share Posted May 8, 2010 Got it working, thanks for the help! ($conn, $_POST["sort"]) This was backwards, the $conn had to be second and the _POST first. (took me awhile to figure out why it was giving me an error for that) One more thing, is there a way to make it where the drop down menu doesn't go back to the default first option (kills) after you click on an option and sort? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 8, 2010 Share Posted May 8, 2010 because the page is reloaded and the kills are <option value="kills" selected="selected" name="kills">kills</option> are by default selected' this will always happen.php solution: create option values dynamically from within array, or database table, echo these as options in the dropdown list, then when the current array/table value equals that of the posted value, assign the 'selected' attribute.javascript DOM: search through all options values, if value equals posted value assign 'selected' attribute. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.