outchy Posted August 23, 2008 Share Posted August 23, 2008 I almost have this working... I have a concert database where I'm trying to create a simple drop down which will allow me to select either "ascending" or "descending" and have it display the results accordingly by date. It's sort of working, but it's only showing one date for "descending" and two dates for "ascending", and then it goes into an infinite loop each time. Here are my two files: sortform.htm: <html> <body> <form name="form1" method="post" action="sortby.php"> <table width="188" border="0" cellpadding="0" cellspacing="0"> <tr> <td><select name="select"> <option value="1">sort by DESC</option> <option value="2">sort by ASC</option> </select></td> <td width><input type="image" src="graphics/query.jpg" alt="submit" title="submit" value="Submit"></td> </tr> </table> </form> </body> </html> sortby.php: <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); $result = mysql_query("SELECT * FROM shows"); //gets the variables the user selected from the form. $select= $_POST['select']; echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ //If the user hasnt selected a sorting preference, just display all the records which havent expired if($select=='1') { $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow DESC"); echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } else { $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow ASC"); echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } } echo '</table><br /></center></body>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/ Share on other sites More sharing options...
JasonLewis Posted August 24, 2008 Share Posted August 24, 2008 Shouldn't you be doing the sort in the first query? <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); //gets the variables the user selected from the form. $select= $_POST['select']; if(!isset($select) || $select == 1){ $order = "DESC"; }else{ $order = "ASC"; } $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow {$order}"); echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } echo '</table><br /></center></body>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624006 Share on other sites More sharing options...
outchy Posted August 24, 2008 Author Share Posted August 24, 2008 Shouldn't you be doing the sort in the first query? <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); //gets the variables the user selected from the form. $select= $_POST['select']; if(!isset($select) || $select == 1){ $order = "DESC"; }else{ $order = "ASC"; } $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow {$order}"); echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } echo '</table><br /></center></body>'; mysql_close($con); ?> That looks a lot better than mine... but it didn't work. I get a blank page when I hit the submit button. Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624013 Share on other sites More sharing options...
Hooker Posted August 24, 2008 Share Posted August 24, 2008 Tag this onto the end of the query: or die(mysql_error()); and you will probably see it's not displaying but returning an error, without taking a good look i'd have to say the curlies were causing it. <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); //gets the variables the user selected from the form. $select= $_POST['select']; if(!isset($select) || $select == 1){ $order = "DESC"; }else{ $order = "ASC"; } $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow $order"); echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } echo '</table><br /></center></body>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624037 Share on other sites More sharing options...
outchy Posted August 24, 2008 Author Share Posted August 24, 2008 I did both things you suggested and it's still showing up as a blank page upon submit. Here's what I'm using currently: <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); //gets the variables the user selected from the form. $select= $_POST['select']; if(!isset($select) || $select == 1){ $order = "DESC"; }else{ $order = "ASC"; } $result = mysql_query("SELECT * FROM shows ORDER BY dateofshow $order") or die (mysql_error()); echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } echo '</table><br /></center></body>'; mysql_close($con); ?> Any other thoughts? Thanks for all of your help, btw. Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624045 Share on other sites More sharing options...
Hooker Posted August 24, 2008 Share Posted August 24, 2008 <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test2", $con); //gets the variables the user selected from the form. $select= $_POST['select']; $sql = "SELECT * FROM shows ORDER BY dateofshow"; if((!isset($select))||($select == 1)){ $sql.=" DESC"; }else{ $sql.=" ASC"; } $result = mysql_query($sql) or die (mysql_error()); echo '<table width="740" border="0" align="center" cellpadding="12" cellspacing="0" class="main">'; echo '<tr class="topper"><th>Date</th><th>Headliner</th><th>Venue</th><th>Opener</th></tr>'; while($rows=mysql_fetch_array($result)){ echo "<tr><td valign=\"top \">".date('Y > M d',strtotime($rows['dateofshow']))."</td><td valign=\"top \">".$rows['headliner']."</td><td valign=\"top \">".$rows['venue']."<br />".$rows['city'].", ".$rows['state']."</td><td valign=\"top \">".$rows['opener1']."<br />".$rows['opener2']."<br />".$rows['opener3']."<br />".$rows['opener4']."<br />".$rows['opener5']."<br />".$rows['opener6']."</td></tr>"; } echo '</table><br /></center></body>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624096 Share on other sites More sharing options...
outchy Posted August 24, 2008 Author Share Posted August 24, 2008 Still blank :-\ Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624099 Share on other sites More sharing options...
Hooker Posted August 24, 2008 Share Posted August 24, 2008 can you drop in a couple of echo's here: $sql = "SELECT * FROM shows ORDER BY dateofshow"; if((!isset($select))||($select == 1)){ $sql.=" DESC"; }else{ $sql.=" ASC"; } echo $sql; echo $select; $result = mysql_query($sql) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624103 Share on other sites More sharing options...
outchy Posted August 24, 2008 Author Share Posted August 24, 2008 That doesn't show anything either... actually, I've never been able to see any echos at all when having this blank page issue. In fact, I think I remember you helping me before when I was having a similar problem. I'm using MAMP on a Mac, not sure if that would have anything to do with it. Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-624107 Share on other sites More sharing options...
fenway Posted August 25, 2008 Share Posted August 25, 2008 That doesn't show anything either... actually, I've never been able to see any echos at all when having this blank page issue. First ,please stop posting all of your script code. Second, make sure you turn on all error reporting in php. Third, if you can't echo, then there's something else wrong. Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-625193 Share on other sites More sharing options...
outchy Posted August 25, 2008 Author Share Posted August 25, 2008 Sorry about that. I found the error log file and this is what it's telling me: PHP Parse error: syntax error, unexpected T_VARIABLE in /Applications/MAMP/htdocs/test2/sortby.php on line 16 This is line 16: $select= $_POST['droppy']; Is there something wrong with that syntax? Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-625425 Share on other sites More sharing options...
acidglitter Posted August 25, 2008 Share Posted August 25, 2008 line 16 looks okay, but a lot of the time the error will be the line before where php is telling you.. like maybe the error is on line 15? and btw, its usually easier to have all of the connection info in another file so you can just include the connection file instead of typing that all over again Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-625436 Share on other sites More sharing options...
outchy Posted August 25, 2008 Author Share Posted August 25, 2008 Yes, I finally put all the connection info in a separate file so I now know that's not the issue. I also took out a space between the equal sign and the dollar sign and that changed the error. Now it says: PHP Parse error: syntax error, unexpected T_VARIABLE in /Applications/MAMP/htdocs/test2/sortby.php on line 16 Which corresponds to: if(!isset($select) || $select == 1) { Quote Link to comment https://forums.phpfreaks.com/topic/121049-sort-by-help/#findComment-625442 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.