Jump to content

"Sort By" help


outchy

Recommended Posts

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);
?>

 

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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) {

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.