简体   繁体   中英

merge two foreach in one layout

i got two arrays.

from results i want to make a table with foreach

but i dont know how to make it work in one table row..

this is what i got

<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px"  >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
</tr>
<?php endforeach;  ?>
</table>

<table>
<?php foreach ($applications as $application) : ?>
<tr><td><?php echo $application->name; ?></td></tr>
<?php endforeach; ?>  
</table>

so what i want is simply add to the first table another column with $application->name;

what i'm missing here?? thanks

<table>
<?php 
$count = count($appky);
for($i=0; $i< $count; $i++) { ?>
    <tr class="counter_apps" height="20px"  >
    <td width="40%"><?php echo $appky[$i]->name; ?></td>
    <td width="20%"><?php echo $appky[$i]->all_items;?></td>
    <td width="20%"><?php echo $appky[$i]->published; ?></td>
    <td width="20%"><?php echo $appky[$i]->unpublished; ?></td>
    <td width="20%"><?php echo $applications[$i]->name; ?></td>
</tr>
<?php }  ?>
</table>

use for instead of foreach

<table>
<?php for($i=0 ; $i<count($appky) ; $i++ ) { ?>
    <tr class="counter_apps" height="20px"  >
    <td width="40%"><?php echo $appka[$i]->name; ?></td>
    <td width="20%"><?php echo $appka[$i]->all_items;?></td>
    <td width="20%"><?php echo $appka[$i]->published; ?></td>
    <td width="20%"><?php echo $appka[$i]->unpublished; ?></td>
    <td width="20%"><?php echo isset($applications[$i]) ? $applications[$i]->name : '' ; ?></td>
    </tr>
<?php }  ?>
</table>

Assuming your two arrays aren't necessarily in order - If you have some data that matches between your $appka and $application you can try something like this below:

<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px"  >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
  <?php foreach ($applications as $application) : ?>
    <?php if ($appka->id == $application->id) { ?>
    <td><?php echo $application->name; ?></td>
    <?php } ?>
  <?php endforeach; ?> 
</tr>
<?php endforeach;  ?>
</table>

Otherwise you wont be able to spit out the correct application name. The above solution uses an id field, but you probably hove something else you can match on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM