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" 

enter image description here

$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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -