我有以下foreach循环:
<?php if(!empty($strategy->assisting)):?>
<ul class="needs-padding">
<?php foreach($strategy->assisting as $asst): ?>
<li><?= $asst->full_name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
我不仅要在李的内部吐出所有的$ asst,而是要回应多个&#39;如果有多个(在显示之后显示列表,我可以用js隐藏/显示它)或者如果只有一个实例则回显$ asst。
我尝试了这个,但它显然是错误的,因为它只打印出多个&#39;对于每个$ asst实例。
<?php if(!empty($strategy->assisting)):?>
<ul class="needs-padding assisting-members">
<?php $count = 0; ?>
<?php foreach($strategy->assisting as $asst): ?>
<?php $count ++; ?>
<?php if($count > 1){
echo '<a href="#">Multiple</a>';
// echo '<li>' . $asst->full_name . '</li>';
} else {
echo '<li>' . $asst->full_name . '</li>';
} ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
答案 0 :(得分:1)
更改您的代码,如下所示:
public string ImpCode(List<string> file) //importing learning module code
{
try
{
return file[file.IndexOf("CODE") + 1];//looks for the section heading in the file, then gets the next line of the file which will have the contents and returns it.
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex), "Error"); //Displays the generated error message to the user
return null;
}
}
答案 1 :(得分:0)
由于$strategy->assisting
是一个数组,您应该能够使用count($strategy->assisting)
而不是$count
变量。类似于:(编辑为仅打印“多次”)
<?php if(!empty($strategy->assisting)):?>
<ul class="needs-padding assisting-members">
<?php printedMultiple = false; ?>
<?php foreach($strategy->assisting as $asst): ?>
<?php if(count($strategy->assisting) > 1 and !printedMultiple){
echo '<a href="#">Multiple</a>';
printedMultiple = true;
// echo '<li>' . $asst->full_name . '</li>';
} elseif (!printedMultiple) {
echo '<li>' . $asst->full_name . '</li>';
} ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>