php - Simple SELECT, GROUP, ORDER BY query not behaving as expected -
i want output cities in db , display logo of agents in each city (company_name name of image)
in screenshot can see there 1 agent in head office , 3 in leighton buzzard. select query below gives me 2 lines don't need. have changed query group remove 2nd , 3rd results displays 2 results want, 2 arpad logos. want 3 leighton buzzard logos display under single leighton buzzard heading.
i have tried using distinct on 'city' seems stop query altogether.
any appreciated.
"select city, company_name memberlogin_members order city asc"
$sql = "select city, company_name memberlogin_members group city asc "; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { $id = $row["id"]; $office = $row["city"]; $agent = $row["company_name"]; $coverage.='<div id="officecoverage" class="fluid "><h3>' . $office . '</h3></div> <div id="imagecontainer" class="fluid "> <div id="imagecoverage" class="fluid "><img src="http://arpad.property/app/logos/' . $agent . '.jpg" alt="' . $agent . '"></div> </div>';
you try this:
$sql = "select city, company_name memberlogin_members order city asc"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $currentcity = null; while($row = mysqli_fetch_assoc($result)) { $id = $row["id"]; $office = $row["city"]; $agent = $row["company_name"]; if($row["city"] !== $currentcity) { $currentcity = $row["city"]; $coverage .= ' <div id="officecoverage" class="fluid "> <h3>' . $office . '</h3> </div> '; } $coverage .= ' <div id="imagecontainer" class="fluid "> <div id="imagecoverage" class="fluid "> <img src="http://arpad.property/app/logos/' . $agent . '.jpg" alt="' . $agent . '"> </div> </div> '; } }
you loop through results, add city headline if city changes. if not, add image.
ps: not use "id" attributes divs exists more once in html code.
Comments
Post a Comment