Forum Moderators: coopster

Message Too Old, No Replies

Sorry! A problem

How can I sort my results?

         

phidentity

12:39 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



I've performed a query so it displays all results where place="sedgley" (my local town).

<?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

BitBanger

1:37 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



In order to sort your output add the 'ORDER BY' statement to your query.
$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.

phidentity

2:27 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Just what I needed :) Thanks :)

Can't get the code to work. Unexpected $end (end of the file, last number).

What have I forgot to do?

Jon

BitBanger

2:44 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Sorry, my mistake.

Change:

$type = $row['type];

To:
$type = $row['type'];

phidentity

3:09 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Gorgeous :)

I thought it was that (the problem), but I ended up editing other things and causing problems.

Your a star!

mysql_fetch_assoc - What exactly does this function do?

Jon

andylarks

3:13 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



mysql_fetch_assoc - returns results allowing you to reference them by their headings.

See: [php.net...]
for more details :)