dpx86402 2015-12-04 19:21
浏览 26
已采纳

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

I want to retrieve all mysql table data using this script:

<?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;
?>

This script creates an output table like that

Parent  Children
0          1
           3
           4
           5
1          1
           2

My problem is when i try to add the other data from the mysql table: comment, name, date etc.

I have to replace these numbers with:

 <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" -->

I tried this but it does not work and i didn't got much about arrays.... A little explaining would help me understand the concept better :)

 $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条回答 默认 最新

  • doutang9037 2015-12-04 20:11
    关注

    You can add the entire result array to your parent array like this:

    // 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;
    }
    

    Then when you output, you can do this:

    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>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据