Hi, Does anyone know a better way to print these listings? I'm trying to print all the Cities with Listings, ordered by State. My script echos the state name with every row and it should only do so the first time the state value changes?! Thanks in advance for any help.
<?php
include("header.inc");
$sql = "SELECT S.state_name, S.state_id, C.city_name, C.city_id, COUNT(*) AS cnt
FROM listings L
LEFT JOIN cities C ON L.city_id=C.city_id
LEFT JOIN states S ON C.state_id=S.state_id
GROUP BY C.city_id
ORDER BY S.state_name ASC, C.city_name ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$this = "foo";
if($this != $row[state_name]) {
echo "</select>";
echo "\n<p>$row[state_name]<br>";
echo "\n<select name=\"cityselect\" >";
echo "\n<option selected value=\"\">Select City</option>";
$this = $row[state_name];
}
echo "\n<option value=\"\">$row[city_name] ($row[cnt])</option>";
}
?>