Home > Software design >  Printing multiple array items in one loop PHP
Printing multiple array items in one loop PHP

Time:09-28

So, I have this html table row saved in the variable hostingItem:

<?php ob_start(); ?>
            <tr>
                <td class="text-center"><?php echo $hosting; ?></td>
                <td class="text-center"><?php echo '<span class="badge badge-'. $hostingStatusColor . ' badge-pill">' . $hostingStatus . '</span>'?></td>
                <td class="text-center"><?php echo "€" . $hostingPrice; ?></td>
                <td class="text-center"><?php echo $hostingExp; ?></td>
                <td class="text-center"><?php echo $hostingPeriod; ?></td>
                <td class="text-center">
                    <div class="toolbar">
                        <div class="toolbar-toggle">...</div>
                        <ul class="toolbar-dropdown animated fadeInUp table-controls list-inline">
                            <li class="list-inline-item"><a href="https://<?php echo $website?>" target="_blank" class="bs-tooltip" data-original-title="View"><i class="flaticon-view-3"></i></a></li>
                            <li class="list-inline-item"><a href="services.php" class="bs-tooltip" data-original-title="Maintenance"><i class="flaticon-settings-4"></i></a></li>
                            <?php
                                if ($hostingStatus == "Active") {
                                    echo '<li class="list-inline-item"><a href="javascript:void(0);" class="bs-tooltip" data-original-title="Request Deactivation"><i class="flaticon-circle-cross"></i></a></li>';
                                } else {
                                    echo '<li class="list-inline-item"><a href="javascript:void(0);" class="bs-tooltip" data-original-title="Request Activation"><i class="flaticon-single-circle-tick"></i></a></li>';
                                }
                            ?>
                        </ul>
                    </div>
                </td>
            </tr>
<?php $hostingItem=ob_get_clean(); ?>

I also have arrays for each variable inside the row all filled with 3 values. Example:

  • Price Array ( [0] => price_1 [1] => price_2 [2] => price_3 )
  • Status Array ( [0] => status_1 [1] => status_2 [2] => status_3 )

etc.

I would like to echo the row 3 times (since the arrays have 3 values as well). In the first echo I want the first value from each array to be inserted to it's variable inside the html table row.

For example, in the first loop the first value in price array will go to the hostingPrice variable . In the third loop, the third value will go to the variable inside the row. This should happen for all items.

Since it sounds a bit complicated (and I do not know much about php) have a look at this concept:

consider this as a loop concept

thank you in advance for your time:)

CodePudding user response:

use a foreach loop and a counter

   $i=0;
   foreach($PriceArray as $val){
      
      $StatusArrayVal = $StatusArray[$i];

      /*
echo each row
       your code
      */

    $i  ;
   }

you will have one value in the $val variable and other in $StatusArrayVal you can echo all the html code or set between tags <? ? >

  • Related