foreach循环,需要根据数组中的内容停止和启动 [英] foreach loop that requires to stop and start based on whats in a array
问题描述
我现在在我的代码中有一个循环,
< table width =100%cellpadding = 0cellspacing =0border =0>
<?php foreach($ credit as $ credit):?>
<?php if($ credit ['credit_type'] ==short):?>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< / tr>
< tr>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td>< a href =>编辑< / a>< / td>
< / tr>
<?php endif; ?>
<?php if($ credit ['credit_type'] ==long):?>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< td>< strong>标题< / strong>< / td>
< td>< strong>角色< / strong>< / td>
< td>< strong> Director< / strong>< / td>
< / tr>
< tr>
< td><?php echo $ credit ['credit_position']; ?>< / TD>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td><?php echo $ credit ['credit_title']; ?>< / TD>
< td><?php echo $ credit ['credit_role']; ?>< / TD>
< td><?php echo $ credit ['credit_director']; ?>< / TD>
< / tr>
<?php endif; ?>
<?php endforeach; ?>
< / table>
然而这并不是我所希望的,
$ b $我想要做的是每次 $ credit ['category_title']
改变它的值我想开始一个新表,这是可能的吗?好吧,所以这个工作,我得到一个新的表,每一个新的类别标题,但它不是显示所有类别的名称,例如我不得不商业信用,但它只是显示一个,
新代码
<?php foreach($ credit as $ credit):?>
<?php if($ credit ['credit_type'] ==short):?>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< / tr>
< tr>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td>< a href =>编辑< / a>< / td>
< / tr>
< / table>
<?php endif; ?>
<?php if($ credit ['credit_type'] ==long):?>
< table width =100%cellpadding =0cellspacing =0border =0>
<?php echo $ oldvalue。 变成 。 $信贷[ category_title]; ?>
<?php if($ credit ['category_title']!= $ oldvalue):?>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< td>< strong>标题< / strong>< / td>
< td>< strong>角色< / strong>< / td>
< td>< strong> Director< / strong>< / td>
< / tr>
< tr>
< td><?php echo $ credit ['credit_position']; ?>< / TD>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td><?php echo $ credit ['credit_title']; ?>< / TD>
< td><?php echo $ credit ['credit_role']; ?>< / TD>
< td><?php echo $ credit ['credit_director']; ?>< / TD>
< / tr>
<?php $ oldvalue = $ credit ['category_title']; ?>
<?php endif; ?>
< / table>
<?php endif; ?>
<?php endforeach; ?>
>
<?php foreach($ credit as $ credit):?>
<?php if($ credit ['credit_type'] ==short):?>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< / tr>
< tr>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td>< a href =>编辑< / a>< / td>
< / tr>
< / table>
<?php endif; ?>
<?php if($ credit ['credit_type'] ==long):?>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td><?php echo $ credit ['category_position']; ?>< / TD>
< td><?php echo $ credit ['category_title']; ?>< / TD>
< td>< strong>标题< / strong>< / td>
< td>< strong>角色< / strong>< / td>
< td>< strong> Director< / strong>< / td>
< / tr>
< tr>
< td><?php echo $ credit ['credit_position']; ?>< / TD>
< td><?php echo $ credit ['credit_heading']; ?>< / TD>
< td><?php echo $ credit ['credit_title']; ?>< / TD>
< td><?php echo $ credit ['credit_role']; ?>< / TD>
< td><?php echo $ credit ['credit_director']; ?>< / TD>
< / tr>
< / table>
<?php endif; ?>
<?php endforeach; ?>
注意只有当您只有2 credit_type
值
I currently have this as a loop in my code,
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
<td><a href="">Edit</a></td>
</tr>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_position']; ?></td>
<td><?php echo $credit['credit_heading']; ?></td>
<td><?php echo $credit['credit_title']; ?></td>
<td><?php echo $credit['credit_role']; ?></td>
<td><?php echo $credit['credit_director']; ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
However this is not doing what I had hoped,
What I wanting to do is that everytime, $credit['category_title']
changes it value I want to start a new table, is this possible?
======
OK, so this has worked and I am getting a new table for every new category title, however it is not showing all the credits with that category title, for example I have to commercial credits but it is only showing one,
new code
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
<td><a href="">Edit</a></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<?php echo $oldvalue . " changed to ". $credit['category_title']; ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_position']; ?></td>
<td><?php echo $credit['credit_heading']; ?></td>
<td><?php echo $credit['credit_title']; ?></td>
<td><?php echo $credit['credit_role']; ?></td>
<td><?php echo $credit['credit_director']; ?></td>
</tr>
<?php $oldvalue = $credit['category_title']; ?>
<?php endif; ?>
</table>
<?php endif; ?>
<?php endforeach; ?>
Move the table tags inside the IF statements
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
<td><a href="">Edit</a></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><?php echo $credit['category_position']; ?></td>
<td><?php echo $credit['category_title']; ?></td>
<td><strong>Title</strong></td>
<td><strong>Role</strong></td>
<td><strong>Director</strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_position']; ?></td>
<td><?php echo $credit['credit_heading']; ?></td>
<td><?php echo $credit['credit_title']; ?></td>
<td><?php echo $credit['credit_role']; ?></td>
<td><?php echo $credit['credit_director']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php endforeach; ?>
Note this is only going to work if you only ever have 2 credit_type
values
这篇关于foreach循环,需要根据数组中的内容停止和启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!