gpt4 book ai didi

php - 从数据库中循环记录

转载 作者:行者123 更新时间:2023-11-29 23:25:59 24 4
gpt4 key购买 nike

我有一个表recipe包含3条记录

-----------------------------
| recipe_code | recipe_name |
-----------------------------
| 32 | tomato puree|
| 16 | potato puree|
| 98 | banana puree|
-----------------------------

我想像这样将记录显示为 7 行

-----------------------------
| recipe_code | recipe_name |
-----------------------------
| 32 | tomato puree|
| 16 | potato puree|
| 98 | banana puree|
| 32 | tomato puree|
| 16 | potato puree|
| 98 | banana puree|
| 32 | tomato puree|
-----------------------------

我正在使用 CODEIGNITER,我的 View 如下:

<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>

<th>Day</th>
<th>Menu</th>

</tr>
</thead>
<?php if (empty($query)) { echo "<tr><td colspan=\"6\ "> Data are unavailable </td></tr>"; } else { foreach ($query as $r) { ?>
<tbody>
<tr>
<?php for ($a=1 ; $a <=7 ; $a++) { ?>
<td>
<?php echo $a; ?>
</td>
<?php ?>
<td class="center">
<a href="<?php echo base_url() . " index.php/home/view_recipe/ " . $r->code_recipe; ?>">
<?php echo $r->recipe_name; ?></a>
</td>

</tr>
</tbody>
<?php } } ?>
</table>
<?php }

结果:

---------------------
| Day | recipe_name |
---------------------
| 1 | tomato puree|
| 2 | tomato puree|
| 3 | tomato puree|
| 4 | tomato puree|
| 5 | tomato puree|
| 6 | tomato puree|
| 7 | tomato puree|
| 1 | potato puree|
| 2 | potato puree|
| 3 | potato puree|
| 4 | potato puree|
| 5 | potato puree|
| 6 | potato puree|
| 7 | potato puree|
| 1 | banana puree|
| 2 | banana puree|
| 3 | banana puree|
| 4 | banana puree|
| 5 | banana puree|
| 6 | banana puree|
| 7 | banana puree|
---------------------

请帮助我

最佳答案

您需要将 foreach() 循环放置在 7 循环中。尝试这样的事情,使用 while 循环。

<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>

<th>Day</th>
<th>Menu</th>

</tr>
</thead>
<tbody>
<?php
if (empty($query)) { echo "<tr><td colspan=\"6\"> Data are unavailable </td></tr>"; }
else {
$a=1;
while($a <=7){
foreach ($query as $r){
if($a>7)break; // if over 7 break out of loop ?>
<tr>
<td>
<?php echo $a; ?>
</td>
<td class="center">
<a href="<?php echo base_url() . " index.php/home/view_recipe/ " . $r->code_recipe; ?>">
<?php echo $r->recipe_name; ?></a>
</td>

</tr>
<?php
$a++; // increase $a
} } ?>
</tbody>
</table>
<?php }

关于php - 从数据库中循环记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com