Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort result set

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
0
0
GB
Howdy folks am new to php but my boss has asked me to have a quick look at something. At the moment i connect to a mysql db do a select statement and print the result set. My boss has asked me though if it is possible to sort on price with minimum first. I would imagine it is i just dont know how. Any ideas would be welcome


cheers


while($row=mysql_fetch_row($answer))
{$k=1;
$price=$row[0];
$gas=$row[1];
$web=$row[2];
print"<tr><td>$price</td><td>$gas</td><td>$web</td></tr>";
}
print"</table>";
if($k==0){print "Sorry no results returned";}
 
A few suggestions:

1. use the "ORDER BY" in your SQL statement to have the ordering done.
Code:
#sql = "SELECT * FROM table ORDER BY whatevercolumn";

2. Use mysql_fetch_assoc(). It will return an associative array. You then have a clearer idea in the code what is what. Instead of $row[0] you would have e.g. $row['id'], $row[2] could be anything, with assoc. you refer clearly to the column name: $row['columnname'].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top