user3655857
user3655857

Reputation: 55

organize output from query php

I am trying to output informations from query with mysqli_fetch_assoc but i can´t solve my problem with the while loop . it will just print out title to every word instead once to every group of words.

$show="";
$showtitles = mysql_query("SELECT * FROM testcheck ORDER BY title")or die();
while($row = mysql_fetch_assoc($showtitles )){
    $show_item= $row['item'];   
    $show_title= $row['title'];
    $show.='<div><b>'.$title.'</b> '.$show_item.'</div><br />';
}

Result :

<div>Fruits apple</div>

<div>Fruits bannan</div>

<div>words Common</div>

<div>words quality</div>

<div>words Enviroment</div>

<div>words Safety</div>

Currently this will output it like this. instead of title being displayed everytime i just want it once like.

<div>Fruits
<li>apple</li>
<li>bannan</<li></div>

<div>words
<li>Common</li>
<li>quality</li>
<li>Enviroment</li>
<li>Safety</li></div>

i have tried many diffrent versions of while loop but this example describes best the situation im in and what im trying to achive.

Upvotes: 0

Views: 43

Answers (1)

Arko Elsenaar
Arko Elsenaar

Reputation: 1729

Try grouping them in an array first. For example:

while($row = mysql_fetch_assoc($showtitles )){
    $array[$row['item']][] = $row['title'];
}

And then loop this one out:

foreach ($array as $key => $value) {
    echo "<strong>".$key."</strong";
    foreach ($value as $key2 => $value2) {
       echo "<div>".$value2."</div>";
    }
}

Upvotes: 1

Related Questions