Forum Moderators: coopster

Message Too Old, No Replies

Showing multiple clients under a city

Showing multiple clients under a city

         

jhardwick

4:52 pm on Sep 9, 2007 (gmt 0)

10+ Year Member



I am trying to display information out of mysql
These records:
01 Client #1 Client #1 City
02 Client #2 Client #2 City
03 Client #3 Client #3 City
04 Client #4 Client #4 City

so it liiks like this:

Boston
- client #1
- client #3

Miami
- client #4

New York
- client #2

i thought this would work but nothing is displayed, any ideas on what is wrong?

<?
$DBhost = "$DBhost";
$DBuser = "$DBuser";
$DBpass = "$DBpass";
$DBName = "$DBName";
$table = "$table";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sql = "SELECT *
FROM $table
ORDER BY city
ASC, name";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

echo "<TABLE align=\"left\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<ul>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($current_city!= $row['city']){ echo '<li>'.$row['city'].'<ul>'; }
echo '<li>'.$row['name'].'</li>'; // whatever here
if ($current_city!= $row['city']){ echo '</ul></li>'; $current_city = $row['city']; }
}
echo "</ul>\n";

echo "</TABLE>";

mysql_free_result($result);

?>

cameraman

6:52 pm on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try viewing the HTML source of the blank page? Since you've got a <table> but no rows or cells in it, your browser may just not be showing you anything.

If that doesn't yield any results, try setting error_reporting(E_ALL); at the top of the script and remove the @ from your select_db line to see what all your errors are - you should get at least one notice about $current_city not being defined on the first run through your loop.

After you've got wrinkles ironed out, you might want to experiment with GROUP BY city.