Sorting MYSQL entries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Sorting MYSQL entries

Post 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++;
}
}
?>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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;
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Post by polosport6699 »

im still getting nothing....When i click submit to sort the results the page just blinks once and doest sort anything
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Register_globals issue?
viewtopic.php?t=511

Mac
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Thanks

Post by polosport6699 »

That last one did it, thanks a lot!!!!!
Post Reply