duanjihe5180 2018-12-21 22:14 采纳率: 100%
浏览 39
已采纳

如何将else语句传递给foreach循环内的子数组

I wanna pass if,else condition with html data. The purpose is to check if session equals to "master" then "allow access" else "access blocked".

For this purpose i use subarray inside foreach.

Code are Below.

foreach ($list as $po_invoice) {
      $no++;
      $row = array();
      $row[] = $no;
      $row[] = $po_invoice->po_code;
      $row[] = $po_invoice->invoice_code;

      $row[]='if else here';

      $row[]='<ul class="dropdown-menu">
               <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
               <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
             </ul>



      $data[] = $row;
    }

i wanna use if-else Here

$row[]= 'if ($id->type=='super_user' OR $id->type=='user') 
    {
    echo "<span class='w3-text-red fa fa-warning '>  Access Forbidden</span>";
    } else {
    '*Some Html Here***'
    };

i used, but not working in this scenario. So please tell me how it's work in this scenario. Advance thanks who will solve my problem.

展开全部

  • 写回答

2条回答 默认 最新

  • dongmu1996 2018-12-21 22:17
    关注

    Make it simple

    <?php 
    $row = array();
    foreach ($list as $po_invoice) {
          $no++;
    
          $row[] = $no;
          $row[] = $po_invoice->po_code;
          $row[] = $po_invoice->invoice_code;
    
         if ($id->type=='super_user' OR $id->type=='user') 
         {
            $row[] = "<span class='w3-text-red fa fa-warning '>  Access Forbidden</span>";
         }else{
    
            $row[]='<ul class="dropdown-menu">
                   <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
                   <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
                 </ul>';
         }
    
          $data[] = $row;
    }
    

    And $row = array(); variable is written out of the scope so whenever loop run it grabs the old values

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部