Hi all, I have a query that I want to sort alphabetically, but the trick is that I want the sorting to treat two columns equally. For instance, if the first row of first_col
equals apple
and the second row of second_col
equals aardvark
I want the value in the second row of second_col
to be listed before the value in the first row of first_col
. A value (not NULL
or ''
) will always exist in every row of second_col
, but the value in first_col
can be ''
. Hopefully I have explained this good enough. I don't care if I have to use MySQL or PHP for this, but once sorted, the array is read through and echoed into an HTML table. Any thoughts?
EDIT
This is what I have for code right now. In my MySQL query I need b_name
and l_name
to be equal. The column b_name
does not always have a value. When I put the values into the table it is based on the existence of b_name
. If b_name
does not exist the f_name
and l_name
are combined to replace b_name
.
$query = "SELECT * FROM customers ORDER BY b_name, l_name";
$result = mysql_query($query);
mysql_close($link);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($result);
$class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
if($row[b_name]!=''){
echo "<tr class=".$class.">";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[c_id]."</a></td>";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[b_name]."</a></td>";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[phone]."</a></td>";
echo "</tr>";
}
else{
echo "<tr class=".$class.">";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[c_id]."</a></td>";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[f_name]." ".$row[l_name]."</a></td>";
echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[phone]."</a></td>";
echo "</tr>";
}
}
?>
</table>