Page 1 of 1

Sorting MYSQL entries

Posted: Mon Jun 23, 2003 9:37 am
by polosport6699
I am wrinting a script to sort mysql entries. I have the correct table displayed but it seems its running my If statements without checking the variable. Im trying to get the table to be sorted based on user input

Heres the code

Code: Select all

<? include ("./config.php") ?> 
<table width="100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td> 
      <form name="form1" method="POST" action="">
        <input name="test" type="text" id="sort" name="sort">
        <input type="submit" name="submit" value="Submit">
      </form> 
      
    </td>
  </tr>
</table>

<table border="0" width="100%" cellspacing="1" cell padding="0" bordercolor="#000000" bgcolor="FFFFFF">
<tr>

<td width="5px" bgcolor="90A0C1"><p align="center"><small><font face="Verdana">id</font></small></td>
<td width="75px" bgcolor="90A0C1"><p align="center"><small><font face="Verdana">Job Title</font></small></td>
<td width="235px" bgcolor="90A0C1"><p align="center"><small><font face="Verdana">Details</font><center></small></td>
<td width="75px" bgcolor="90A0C1"><p align="center"><small><font face="Verdana">Location</font><center></small>
<td width="85px" bgcolor="90A0C1"><p align="center"><small><font face="Verdana">Date Posted<center></font></small>
</td>
<?
if($sort == title); {
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY designation";
} 
if($sort == city);
{
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY city";
}
$result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error());
if(mysql_num_rows($result)) {
  $rank = 1;
  while($row = mysql_fetch_row($result)) 
  {
    print("</tr><tr>");
    if($color == "#FFFFFF") {
     $color = "#DFDFDF";
    } else {
      $color = "#FFFFFF";
    }

   print("<td width="5px" bgcolor="$color"><center><small>");
   print("<font face="Verdana">$rank</font></small></center></td>");
   print("<td width="75px" bgcolor="$color"><left><small>");
   print("<font face="Verdana">$row[1]</font></small></center></td>");
   print("<td width="235px" bgcolor="$color"><small>");
   print("<font face="Verdana">$row[2]</font></small></center></td>");
   print("<td width="75px" bgcolor="$color"><left><small>");
   print("<font face="Verdana">$row[3]</font></small></left></td>");
   print("<td width="85px" bgcolor="$color"><left><small>");
   print("<font face="Verdana">$row[4]</font></small></left></td>");
  $rank++;
}
}
?>

Posted: Mon Jun 23, 2003 10:23 am
by Wayne

Code: Select all

if($sort == title); &#123; 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY designation"; 
&#125; 
if($sort == city); 
&#123; 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY city"; 
&#125;

Code: Select all

if($sort == 'title')&#123; 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY designation"; 
&#125; 
if($sort == 'city')&#123; 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY city"; 
&#125;

Posted: Mon Jun 23, 2003 10:42 am
by polosport6699
im still getting nothing....When i click submit to sort the results the page just blinks once and doest sort anything

Posted: Mon Jun 23, 2003 10:51 am
by twigletmac
Register_globals issue?
viewtopic.php?t=511

Mac

Posted: Mon Jun 23, 2003 11:05 am
by polosport6699
Nope, I can get the variables to display using echo $sort

but the code will not stop if ($sort=title) it just keeps executiing the next level of code

Posted: Mon Jun 23, 2003 11:18 am
by twigletmac
You should probably have if ... elseif statements instead of multiple if statements. You need to put your strings into quotes (turn up your error reporting to see the notices being generated).

You really should also start coding for reg_globals off as it is deprecated and could be excluded from the next major version of PHP.

That said, try changing this code:

Code: Select all

if($sort == title); { 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY designation"; 
} 
if($sort == city); 
{ 
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY city"; 
}
To:

Code: Select all

if ($sort == 'title') {
    $order_by = 'designation';
} elseif ($sort == 'city') {
    $order_by = 'city';
} else {
    $order_by = 'jcode';
}
$query = "SELECT jcode, designation, responsibilities, city, posted FROM listing ORDER BY $order_by";
Mac

Thanks

Posted: Mon Jun 23, 2003 11:21 am
by polosport6699
That last one did it, thanks a lot!!!!!