Forum Moderators: coopster
<?php global $db_list;
$database = "nightlife_review";
$conn = @mysql_connect("localhost","x","x")
or die ("Sorry");
@mysql_select_db($database) or die ("unable to select database!");
$query = "SELECT * FROM places WHERE location='Sedgley' ";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
echo("<b><h1><font face=\"Verdana\" size =\"2\"> Pubs in Sedgley:</h1><br> </b>");
$i=0;
while($i< $num){
$name=mysql_result($result,$i,"name");
$type=mysql_result($result,$i,"type");
if( file_exists( $name.".php" ) )
{
echo("<b><table><tr><td><a href=\"$name.php\">$name - $type </a><br></b></td>");
}
else { echo( "<b><table><tr><td>$name- $type<br></b></td>"); }
++$i;
}
?>
I want to sort the output,
Pubs:
Bars:
Restaurants:
However i'm new at this, when I tried to do 2 queries per page, It didn't work, and when I used if statements, it didn't display...
How should I go about this? As it looks unorganised at present.
Thanks all :)
Jon
$query = "SELECT * FROM places WHERE location='Sedgley' ORDER BY type";
This will sort the returned data from the table by the `type` field alphabetically. Tack a DESC onto the end to reverse the sort direction.
I would also move the mysql_close() to the end of your code. The PHP manual is not clear on this issue, but it is possible that the result set is invalidated by the mysql_close() function. In fact, as long as your script actually exits, it is not necessary to close the connection.
Next, your output HTML is malformed. You are not closing the 'table' tag, so you end up embedding a table inside a table in an invalid way. I would rewrite the output code like this:
$query = "SELECT * FROM places WHERE location='Sedgley' ORDER BY type DESC";$result = mysql_query($query);
echo("<b><h1><font face=\"Verdana\" size =\"2\"> Pubs in Sedgley:</h1><br> </b>");
echo "<table>";
while ($row = mysql_fetch_assoc($results)) {
$name = $row['name'];
$type = $row['type];
if( file_exists( $name.".php" ) ) {
echo("<tr><td><b><a href=\"{$name}.php\">{$name}</a></b></td><td>{$type}</td></tr>");
} else {
echo("<tr><td><b>{$name}</b></td><td>{$type}</td></tr>");
}
}
echo "</table>";mysql_close();
}
I took the liberty of reformatting the output table into two columns.
See: [php.net...]
for more details :)