Parsing one col of multiple rows php

后端 未结 3 465
野的像风
野的像风 2021-01-26 03:35

I have e.g. the following data returned from a query, each name is an item:

id      name      comment
1       FF        hey
1       FF        hey back!
2       L         


        
3条回答
  •  无人共我
    2021-01-26 03:59

    Don't confuse data access with output and you will have an easier time attacking this sort of problem.

    //Fetch and Sort
    $data = array();
    while ($row = mysql_fetch_array($result)){
       $item = $row['item'];
       if(!isset($data[$item]) {
         $data[$item] = array():
       }
       $data[ = $data[$item][] = $row['comment'];   
    }
    
    //Output
    foreach($data as $item => $comments) {
        echo '
  • '.$item.'
    '; foreach($comments as $comment) { echo '
    '.$comment.'
    '; } echo '
  • '; }

提交回复
热议问题