【问题标题】:How to correctly print the repeated rows?如何正确打印重复的行?
【发布时间】:2016-10-11 05:10:53
【问题描述】:

我已经将一个旧 SO 问题的 mysql 结果模拟为以下数组:

<?php
$arr = array(
           array(
            'title'=>'Test',
            'name'=>'ABC',
            'cat_desc'=>'ABC_DESC',
            'parent'=>0,
            'parent_menu'=>1
            ),
            array(
            'title'=>'Test2',
            'name'=>'DEF',
            'cat_desc'=>'DEF_DESC',
            'parent'=>0,
            'parent_menu'=>2
            ),
            array(
            'title'=>'Test2',
            'name'=>'GHI',
            'cat_desc'=>'GHI_DESC',
            'parent'=>1,
            'parent_menu'=>0
            ),
            array(
            'title'=>null,
            'name'=>'JKL',
            'cat_desc'=>'JKL_DESC',
            'parent'=>2,
            'parent_menu'=>0
            )
      );
    //print_r($arr);
    ?>

现在我想知道我是否可以以这种格式打印结果:

<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
     </tr>

      <tr>            
         <td> Menu Title Test </td>
         <td> Main Category ABC</td>
      </tr>
        <tr>
          <td>GHI</td>
          <td>GHI_DESC</td>
      </tr>
       <tr>            
         <td> Menu Title Test2 </td>
         <td> Main Category DEF</td>
      </tr>
      <tr>
          <td>JKL</td>
          <td>JKL_DESC</td>
      </tr>
   </table>

我正在尝试使用以下 php 进行打印,但它无法给出预期的结果:

<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
     </tr>
  <?php 
  $r = 0;
  while(list($key, $val)=each($arr)) {
    if($val['parent']==0):            
     if($r % 1==0): ?>                      
      <tr>            
         <td> Menu Title <?php echo $val['title'];?> </td>
         <td> Main Category <?php echo $val['name'];?></td>
      </tr>
  <?php endif;        
    endif;                         
     if($val['parent']!=0){ ?>
      <tr>
          <td><?php echo $val['name'];?></td>
          <td><?php echo $val['cat_desc'];?></td>
      </tr>
<?php 
    }  
  $r++; 
 } ?>
   </table>

非常欢迎您的帮助和建议。

【问题讨论】:

    标签: php arrays html-table


    【解决方案1】:

    我发现很难从数组结构中弄清楚你到底想要什么。但我注意到数组元素 0 与元素 2 配对,元素 1 与元素 3 配对。因此我的代码如下所示。它会为您提供您想要的表格。

    print "<table border=1>";
    
    $numArray = count($arr) / 2;
    
    for($i=0;$i<$numArray;$i++) {
        $element = $arr[$i];
        print "<tr><td>Menu Title {$element['title']}</td><td>Main Category {$element['name']}</td></tr>";
        $element = $arr[$i+2];
        print "<tr><td>{$element['name']}</td><td>{$element['cat_desc']} </td></tr>";
    }
    
    print "</table>";
    

    【讨论】:

    • 我接受你的回答,因为它是这里最短的。谢谢。
    【解决方案2】:

    你可以在我评论做什么和为什么的地方使用它

    <table>
        <tr>
            <th>Name</th>
            <th>Description</th>
         </tr>
    <?php 
    // Store the array into temporary array 
    $temp_array = $arr;
    foreach ($arr as $akey => $sArr) 
    {         
       // Check that prrent is 0 
       if ( $sArr['parent'] ==0 ) 
       {  
          // Store parent_menu id 
          $parent_menu = $sArr['parent_menu'];
       ?>
          <tr>            
             <td> Menu Title <?php echo $sArr['title'];?> </td>
             <td> Main Category <?php echo $sArr['name'];?></td>
          </tr>
          <?php 
          // Unset or remove current index from array 
          unset($temp_array[$akey]); 
          // Iterate all rows    
          foreach ($temp_array as $skey => $aval) 
          {          
             // Check that it has parent for the current menu
             if ( $aval['parent'] == $parent_menu ) 
             {
             ?>
                <tr>
                    <td><?php echo $aval['name'];?></td>
                    <td><?php echo $aval['cat_desc'];?></td>
                </tr>         
             <?php 
             }
          }
          // End of foreach
       }
       // End of if 
    } 
    // End of foreach 
    ?>
    </table>
    

    【讨论】:

      【解决方案3】:

      我发现来自关系数据库的数据并不适合直接使用,需要进行一些处理以降低代码复杂性。因此,如果您的目标是制作带有子菜单的动态菜单,我建议将数据结构重构为更多的树形结构,然后在每个级别上循环。

      更好的数据结构是:

      $menus = array(   
        1 => array( // original array item index 0
          'title'=>'Test',
          'name'=>'ABC',
          'cat_desc'=>'ABC_DESC',
          'parent'=>0,
          'parent_menu'=>1
          'children'=> array( // new array containing submenus
            array( // original array item index 2
             'title'=>'Test2',
             'name'=>'GHI',
             'cat_desc'=>'GHI_DESC',
             'parent'=>1,
             'parent_menu'=>0
            ),
          ),
        ),
        2 => array( // original array item index 1
          'title'=>'Test2',
          'name'=>'DEF',
          'cat_desc'=>'DEF_DESC',
          'parent'=>0,
          'parent_menu'=>2
          'children'=> array( // array containing original item index 2
            array(
              'title'=>null,
              'name'=>'JKL',
              'cat_desc'=>'JKL_DESC',
              'parent'=>2,
              'parent_menu'=>0
            ),
          ),   
        ),
      );
      

      与其只是以这种更方便的形式输入数据,不如进行转换。因此,要从原始数据获取此结构,您可以使用简单的单遍 foreach 循环:

      $menus = [];
      foreach($arr as $item) {
        if($item['parent'] == 0) {
          $item['children'] = array();
          $menus[$item['parent_menu']] = $item;
        }
        else {
          $menus[$item['parent']]['children'][] = $item;
        }
      }
      

      一旦您以一种方便的形式获得数据,您就可以使用简单的嵌套 foreach 循环对其进行迭代并以如下清晰的方式输出表格:

      <table>
          <tr>
              <th>Name</th>
              <th>Description</th>
          </tr>
          <?php foreach($menus as $menu) { ?>
              <tr>            
                  <td> Menu Title <?php echo $menu['title'];?> </td>
                  <td> Main Category <?php echo $menu['name'];?></td>
              </tr>
              <?php  foreach($menu['children'] as $child) { ?>
                  <tr>
                      <td><?php echo $child['name'];?></td>
                      <td><?php echo $child['cat_desc'];?></td>
                  </tr>
          <?php } } ?> 
      </table>
      

      【讨论】:

        【解决方案4】:

        据我所见,我认为 $r % 2 比 $r % 1 更有意义

        如果 $r 是整数,$r % 1 将始终返回零。看起来在您的情况下,您使用它来拆分奇数和偶数的逻辑,这对您的数据集很有意义。

        确保数据集没有改变。也许最好使用更可靠的方法。向数据集添加索引也不会有害:)

        【讨论】:

        • $r%2 不返回重复行的第二个循环。即&lt;tr&gt; &lt;td&gt; Menu Title Test2 &lt;/td&gt; &lt;td&gt; Main Category DEF&lt;/td&gt; &lt;/tr&gt;
        • 可能是因为您从错误的值开始。尝试在循环中回显 $r 的值以遵循逻辑。或者尝试从 $r=1
        猜你喜欢
        • 2015-05-28
        • 2020-12-10
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-16
        • 1970-01-01
        相关资源
        最近更新 更多