获取父行和子行的所有数据

时间:2015-12-04 19:21:53

标签: php mysql arrays

我想使用这个脚本检索所有mysql表数据:

<?php
//WHERE id_post = '$id_post'
$sq = mysql_query("SELECT * FROM article_comments  ") or die(mysql_error());;
connect();
 while($row = mysql_fetch_assoc($sq)){
$result[$row["parent_id"]][] = $row["id"];
 }



$rowIsOpened = false; //Indicates whether a row is currently opened.

  //I'm using $rowIsOpened because the row immediately after the rowspanned cell shouldn't be closed.

echo <<<HTML
<table style="border-color: black;border-style: solid;">
<thead>
    <tr>
        <th>Parent</th>
        <th>Children</th>
    </tr>
</thead>
<tbody>
HTML;
//Echo a bunch of HTML before actually looping

foreach ($result as $parent => $children) {
echo "<tr style='border-color: black;border-style: solid;vertical-align:top;'>";
 echo "<td rowspan=";
 echo count($children); //Span over <how many children are> rows
 echo " style='border-color: black;border-style: solid;' >$parent</td>";
 $rowIsOpened = true; //Row is opened
 foreach ($children as $child) {
    if (!$rowIsOpened) {
        echo "<tr>";
    } //Only open a row if row is not opened
    echo "<td style='border-color: black;border-style: solid;'>$child</td>";
    echo "</tr>";
    $rowIsOpened = false; //Row is now closed. Ready for next iteration.
  }

}
//Close the table tags etc.
  echo <<<HTML
  </tbody>
 </table>
  HTML;
?>

此脚本创建一个类似

的输出表
Parent  Children
0          1
           3
           4
           5
1          1
           2

我的问题是当我尝试从mysql表中添加其他数据时:评论,姓名,日期等。

我必须用以下代码替换这些数字:

 <div class="cmt-cnt">
    <img src="<?php echo $grav_url; ?>" />
    <div class="thecom">
        <h5><?php echo $name; ?></h5><span data-utime="1371248446"    class="com-dt"><?php echo $date; ?></span>
        <br/>
        <p>
            <?php echo $comment; ?>
        </p>
    </div>
    </div><!-- end "cmt-cnt" -->

我尝试了这个,但它不起作用,我没有太多关于数组.... 一点点解释可以帮助我更好地理解这个概念:)

 $id_post = "1";    
 $name = array();
 $email = array();
 $comment = array();
 $date = array();

    //WHERE id_post = '$id_post'
  $sq = mysql_query("SELECT * FROM article_comments WHERE id_post = '$id_post' ") or die(mysql_error());;
connect();
 while($row = mysql_fetch_assoc($sq)){
 $result[$row["parent_id"]][] = $row["id"];

  $comment[$row['id']] = $row['comment'];



    // Get gravatar Image 
    // https://fr.gravatar.com/site/implement/images/php/
    $default = "mm";
    $size = 35;
     //    $grav_url =  "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;

    }



$rowIsOpened = false; //Indicates whether a row is currently opened.

       //I'm using $rowIsOpened because the row immediately after the rowspanned cell shouldn't be closed.

   echo <<<HTML
   <table style="border-color: black;border-style: solid;">
   <thead>
    <tr>
        <th>Parent</th>
        <th>Children</th>
    </tr>
  </thead>
   <tbody>
  HTML;
     //Echo a bunch of HTML before actually looping

      foreach ($result as $parent => $children) {
   echo "<tr style='border-color: black;border-style: solid;vertical-align: top;'>";
   echo "<td rowspan=";
  echo count($children); //Span over <how many children are> rows
     echo " style='border-color: black;border-style: solid;width:400px;' >$parent

    <div class='cmt-cnt'>
    <img src='".$grav_url."' />
    <div class='thecom'>
        <h5>".$name."</h5><span data-utime='1371248446' class='com-dt'>".$date."</span>
        <br/>
        <p>
          ".$comment."
        </p>
    </div>
   </div>

    </td>";


   $rowIsOpened = true; //Row is opened
   foreach ($children as $child) {
    if (!$rowIsOpened) {
        echo "<tr>";
    } //Only open a row if row is not opened
    echo "<td style='border-color: black;border-style: solid;width:400px;'>$child</td>";
    echo "</tr>";
    $rowIsOpened = false; //Row is now closed. Ready for next iteration.
      }

  }
 //Close the table tags etc.
 echo <<<HTML
</tbody>
 </table>
 HTML;
 ?>

1 个答案:

答案 0 :(得分:0)

您可以将整个结果数组添加到父数组中,如下所示:

// I like to declare my variables and arrays
$results = array();

while ($row = mysql_fetch_assoc($sq)){
    // I don't like to assume that I can append to array indexes that don't exist
    if (!array_key_exists($row['parent_id'], $results)){
        $results[$row['parent_id']] = array();
    }
    $results[$row['parent_id']][] = $row;
}

然后当你输出时,你可以这样做:

foreach ($results as $parent_id => $children){

    // I'm leaving out the table because this is just for an example
    echo $parent_id . '<br>';

    foreach ($children as $child){
        echo ' - ' . $child['commment'] . '<br>';
        echo ' - ' . $child['date'] . '<br>';
    }
}