Page 1 of 1

Formatting Sort Results?

Posted: Mon Apr 18, 2005 5:46 am
by jjfletch
The select statement in the following code queries two tables, returns the results from both, and sorts them so that those records with notes associated with them are displayed first:

Code: Select all

<?php
 
require_once ('../files/mysql_connect.php');
 
$cy = $_POST['city'];
 
   if (!empty($_POST['city'])) {
      $cy = escape_data($_POST['city']);
   } else {
      $cy = FALSE;
      echo '<p><font color="red">Please enter a City</font></p>';
   }
 
  if ($cy) {
     
     $query = "SELECT Table_1.id  
         , Table_1.LastName
         , Table_1.FirstName
         , Table_1.Phone1
         , Table_1.city1 
         , Table_1.city2  
         , Table_1.city3  
         , Table_1.phone1 
         , Table_1.phone2
         , Table_1.phone3  
         , Table_1.lastname 
         , COALESCE(Table_2.Notes, '') AS Notes   
FROM Table_1  
LEFT JOIN Table_2 ON (Table_2.id = Table_1.id)
WHERE (Table_1.city1 LIKE ('$cy%') OR Table_1.city2 LIKE ('$cy%') OR Table_1.city3 LIKE ('$cy%'))  
ORDER BY Notes DESC, Table_1.lastname DESC";
  }
 
     $result = mysql_query ($query);
 
     if (mysql_num_rows($result) == 0) {
     echo "<tr><td colspan=\"5\">Currently, we do not feature that in our database.\n";
     } else {
     while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
 
     echo "
         <tr>
         <td>{$row['id']}</td>
         <td>{$row['LastName']}</td>
         <td>{$row['FirstName']}</td>
         <td>{$row['Phone1']}</td>
         </tr>
         \n";
}
}
 
echo '</table>';
 
mysql_close();
?>
Note that the "Notes" aren't actually printed, but are used as a means to sort the data.

What I'd like to be able to do, is to print those records with notes in bold, while keeping those without notes non-bold. Is that possible? Anyone know how to do it?

(Should mention I'm still newbish, so detailed help is appreciated!)

Posted: Mon Apr 18, 2005 7:29 am
by feyd
since you have 'Notes' in your selection, you should be able to check if it's NULL or an empty string. That'd tell you it doesn't have any notes.