Search code examples
phpsub-array

how to pass if else statment to subarray inside foreach loop


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.


Solution

  • 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