tobeyt23 Posted March 28, 2017 Share Posted March 28, 2017 Ok I am having a brain fart and/or not enough coffee! See the below array: Array ( [0] => Array ( [breed_name] => Boxer [dog_name] => Fred ) [1] => Array ( [breed_name] => Labrador Retriever [dog_name] => Roxy ) [2] => Array ( [breed_name] => Labrador Retriever [dog_name] => Semper ) [3] => Array ( [breed_name] => Mixed Breed [dog_name] => Bronchito ) [4] => Array ( [breed_name] => Mixed Breed [dog_name] => Fonz ) [5] => Array ( [breed_name] => Mixed Breed [dog_name] => Triton ) ) I need to lay this out like so: Boxer - Fred Labrador Retriever - Roxy, Semper Mixed Breed - Bronchito, Fonz, Triton Any help would be appreciated as i am just banging my head currently! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 If you stored this into a dbms table you could write a query to organize the data in any fashion you might need today and in the future. If this is meant to be any kind of static data, I highly recommend that you re-think your storage plan. Sure you can do this, but the better approach is as I am suggesting. Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted March 28, 2017 Author Share Posted March 28, 2017 these are in tables Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Would have been nice if you mentioned that. Do your query to order the results by breed. Then loop thru the results echo'ing the breed and name on the first record. Then on the following records echo the name until you see a change in breed at which time you begin a new row with the breed name and the new dog name. And repeat. You'll have to save the 'last-breed' name after each record and use it to compare against the next record every time. A very common approach to your problem. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 (edited) Feeling magnanimous today: $q = 'select breed_name, dog_name from dogs_table order by breed_name'; $qrslts = $pdo->query($q); if (!$qrslts) { echo "Could not execute query"; exit(); } $last_breed = 'xxx'; while(list($breed, $name) = $qrslts->fetch(PDO::FETCH_NUM)) { if ($breed <> $last_breed) { if ($last_breed <> 'xxx') echo '<br>'; echo "$breed - $name"; } else echo ", $name"; $last_breed = $breed; } (too bad the forum doesn't recognize the tabs in this code.) Edited March 28, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted March 28, 2017 Author Share Posted March 28, 2017 Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.