Вопрос

I have pulled some information from a site API and it is stored in $result. I would like to create a table with this information that loops x number of times (constant). Here is how I currently have it repeating by hand and it works. I just imagine there must be a better/easier way to do this then manually repeating it each time.

<tr>
    <td><?php echo strtoupper($result['weapons']['0']['stat']['id']); ?></td>
    <td><?php echo round($result['weapons']['0']['extra']['accuracy'],2); ?>%</td>
    <td><?php echo round($result['weapons']['0']['extra']['kpm'],2); ?></td>
    <td><?php echo number_format($result['weapons']['0']['stat']['shots']); ?></td>
    <td><?php echo number_format($result['weapons']['0']['stat']['hits']); ?></td>
    <td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
    <td><?php echo number_format($result['weapons']['0']['stat']['hs']); ?></td>
    <td><?php echo round($result['weapons']['0']['extra']['hkp'],2); ?>%</td>
</tr>
<tr>
    <td><?php echo strtoupper($result['weapons']['1']['stat']['id']); ?></td>
    <td><?php echo round($result['weapons']['1']['extra']['accuracy'],2); ?>%</td>
    <td><?php echo round($result['weapons']['1']['extra']['kpm'],2); ?></td>
    <td><?php echo number_format($result['weapons']['1']['stat']['shots']); ?></td>
    <td><?php echo number_format($result['weapons']['1']['stat']['hits']); ?></td>
    <td><?php echo number_format($result['weapons']['1']['stat']['kills']); ?></td>
    <td><?php echo number_format($result['weapons']['1']['stat']['hs']); ?></td>
    <td><?php echo round($result['weapons']['1']['extra']['hkp'],2); ?>%</td>
</tr>

I just can't wrap my head around how I would loop this? I have searched and looked at many articles, I just can't seem to get anything to work.

Это было полезно?

Решение

If you'd like to run through every result the easiest way would be to use foreach

e.g.

foreach($result['weapons'] as $tr){ ?>
<tr>
    <td><?= strtoupper($tr['stat']['id']) ?></td>
    <td><?= round($tr['extra']['accuracy'],2) ?>%</td>
    //... etc.
</tr>
<?php } //...

If you'd prefer to define the amount of loops just use "for"

for ($i = 0; $i <= 10; $i++) { ?>
<tr>
    <td><?= strtoupper($result['weapons'][$i]['stat']['id']) ?></td>
    <td><?= round($result['weapons'][$i]['extra']['accuracy'],2) ?>%</td>
    <td><?= round($result['weapons'][$i]['extra']['kpm'],2) ?></td>
    //.... etc.
</tr>
<?php }

Другие советы

it's very easy to use foreach loop lets assume you got your value in variable $result_api then loop through this array $i=0;

foreach($result_api as $result)
{ ?>
<tr>
    <td><?php echo strtoupper($result['weapons'][$i]['stat']['id']); ?></td>
    <td><?php echo round($result['weapons'][$i]['extra']['accuracy'],2); ?>%</td>
    <td><?php echo round($result['weapons'][$i]['extra']['kpm'],2); ?></td>
    <td><?php echo number_format($result['weapons'][$i]['stat']['shots']); ?></td>
    <td><?php echo number_format($result['weapons'][$i]['stat']['hits']); ?></td>
    <td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
    <td><?php echo number_format($result['weapons'][$i]['stat']['hs']); ?></td>
    <td><?php echo round($result['weapons'][$i]['extra']['hkp'],2); ?>%</td>
</tr>
<?php 
$i++;

}

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top