Php Foreach内部if()| if()关闭问题
我将foreach语句放在if语句中,在结束if语句时遇到了一个问题。if检查一个“类型”,如果条目类型与所选的“类型”匹配,它将用foreach输出所有具有该“类型”的条目 这里有一个例子Php Foreach内部if()| if()关闭问题,php,codeigniter,if-statement,foreach,Php,Codeigniter,If Statement,Foreach,我将foreach语句放在if语句中,在结束if语句时遇到了一个问题。if检查一个“类型”,如果条目类型与所选的“类型”匹配,它将用foreach输出所有具有该“类型”的条目 这里有一个例子 <?php if($row->type == 'HTML') { foreach($content as $row): ?> <h2><?= $row->title ?></h2> <span
<?php
if($row->type == 'HTML')
{
foreach($content as $row): ?>
<h2><?= $row->title ?></h2>
<span class="rating"> <?= $row->rating?>/5</span>
<p class="desc"><?= $row->description ?> </p>
<span class="date"><?= $row->created ?> </span>
<a href="<?= $row->url ?>">View Tutorial</a>
<span class="diff"><?= $row->difficulty ?></span>
<span class="auth">Added by </span>
<?php } else {
echo 'No results found';
}
endforeach; ?>
/5
由添加
您将在循环中找到$row
,因此在foreach中保留if()else
,以便获取$row->type
<?php
foreach($content as $row):
if($row->type == 'HTML')
{?>
<h2><?= $row->title ?></h2>
<span class="rating"> <?= $row->rating?>/5</span>
<p class="desc"><?= $row->description ?> </p>
<span class="date"><?= $row->created ?> </span>
<a href="<?= $row->url ?>">View Tutorial</a>
<span class="diff"><?= $row->difficulty ?></span>
<span class="auth">Added by </span>
<?php } else {
echo 'No results found';
}
endforeach; ?>
/5
由添加
您的条件必须是:
<?php
foreach($content as $row): ?>
if($row->type == 'HTML')
{
<h2><?= $row->title ?></h2>
<span class="rating"> <?= $row->rating?>/5</span>
<p class="desc"><?= $row->description ?> </p>
<span class="date"><?= $row->created ?> </span>
<a href="<?= $row->url ?>">View Tutorial</a>
<span class="diff"><?= $row->difficulty ?></span>
<span class="auth">Added by </span>
<?php } else {
echo 'No results found';
}
?>
<?php endforeach; ?>
如果($row->type=='HTML')
{
/5
由添加
您的foreach条件应高于if-else条件。
<?php
foreach($content as $row):
if($row->type == 'HTML') { ?>
<h2><?= $row->title ?></h2> <span class="rating"> <?= $row->rating?>/5</span>
<p class="desc"><?= $row->description ?> </p>
<span class="date"><?= $row->created ?> </span>
<a href="<?= $row->url ?>">View Tutorial</a>
<span class="diff"><?= $row->difficulty ?></span>
<span class="auth">Added by </span>
<?php
} else {
echo 'No results found';
}
endforeach; ?>
/5
由添加
您的结构应该是:
- 如果
- 弗雷奇
- 内容
- 端部foreach
- 弗雷奇
- 否则
- 其他内容
- 如果结束
- 如果
- 弗雷奇
- 内容
- 弗雷奇
- 否则
- 其他内容
- 如果结束
- 结束foreach
<?php
$found = FALSE; // used to track whether something has been found.
foreach($content as $row){
if($row->type == 'HTML')
{
$found = TRUE; // there is at least one row with ->type == 'HTML'
?>
<h2><?= $row->title ?></h2>
<span class="rating"> <?= $row->rating?>/5</span>
<p class="desc"><?= $row->description ?> </p>
<span class="date"><?= $row->created ?> </span>
<a href="<?= $row->url ?>">View Tutorial</a>
<span class="diff"><?= $row->difficulty ?></span>
<span class="auth">Added by </span>
<?php
}
}; // moved to the {} closing
// test whether any valid rows have been found (set above)
if(!$found){echo 'No results found';}
?>
/5
由添加
对这个问题的理解非常好。+1感谢你的帮助,上面的那个家伙比你快到了。如果你觉得这个答案有用,请标记为答案