CakePHP2中没有排名信息时无法跳过排名帖子
很难用语言来解释,但我想做的是在CakePhp2中没有排名信息时跳过排名。例如,我有以下数据,其中包含7行排名数据。7行中有2行包含空主体(第2行和第5行)。所以我做了一个脚本来跳过这个空的排名。但是,从跳过的第2行和第5行开始。显示的数据柱为1,3,4,6,7。但是我想显示排名,比如1,2,3,4,5。换句话说,我想把第三名显示为第二名,第四名显示为第三名,以此类推。对不起,我解释得不好。简单地说,当没有排名信息时,我想跳过排名,并显示排名编号。(附言:我不想改变DB)我很想听到你的声音CakePHP2中没有排名信息时无法跳过排名帖子,php,cakephp-2.0,cakephp-2.3,cakephp-2.1,Php,Cakephp 2.0,Cakephp 2.3,Cakephp 2.1,很难用语言来解释,但我想做的是在CakePhp2中没有排名信息时跳过排名。例如,我有以下数据,其中包含7行排名数据。7行中有2行包含空主体(第2行和第5行)。所以我做了一个脚本来跳过这个空的排名。但是,从跳过的第2行和第5行开始。显示的数据柱为1,3,4,6,7。但是我想显示排名,比如1,2,3,4,5。换句话说,我想把第三名显示为第二名,第四名显示为第三名,以此类推。对不起,我解释得不好。简单地说,当没有排名信息时,我想跳过排名,并显示排名编号。(附言:我不想改变DB)我很想听到你的声音
array(7) { [0]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "1" ["title"]=> string(6) "title1" ["body"]=> string(5) "body1" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [1]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "2" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [2]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "3" ["title"]=> string(6) "title3" ["body"]=> string(5) "body3" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [3]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "4" ["title"]=> string(6) "title4" ["body"]=> string(5) "body4" ["created"]=> string(19) "2017-04-08 15:48:21" ["modified"]=> NULL } } [4]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "5" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [5]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "6" ["title"]=> string(6) "title6" ["body"]=> string(5) "body6" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [6]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "7" ["title"]=> string(6) "title7" ["body"]=> string(5) "body7" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } }
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php } else {
continue;
}?>
</ul>
<?php $k++; } ?>
试图制作一个脚本来显示流行的排名。我真的很高兴听到你的一些很好的提示和样品
array(7) { [0]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "1" ["title"]=> string(6) "title1" ["body"]=> string(5) "body1" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [1]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "2" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [2]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "3" ["title"]=> string(6) "title3" ["body"]=> string(5) "body3" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [3]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "4" ["title"]=> string(6) "title4" ["body"]=> string(5) "body4" ["created"]=> string(19) "2017-04-08 15:48:21" ["modified"]=> NULL } } [4]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "5" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [5]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "6" ["title"]=> string(6) "title6" ["body"]=> string(5) "body6" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [6]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "7" ["title"]=> string(6) "title7" ["body"]=> string(5) "body7" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } }
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php } else {
continue;
}?>
</ul>
<?php $k++; } ?>
热门排名帖子
-
//即使第二个正文数据丢失(当前为第三个数据),仍希望以$k显示数字2。
热门排名帖子
-
//即使第二个正文数据丢失(当前为第三个数据),仍希望以$k显示数字2。
改变了什么?
我移动了$k++代码>在if
的内部,就在else
为什么?
您的代码在做什么:
- 将
$k
设置为1
- 帖子1不是空的,所以显示它(排名1)
- 将
$k
增加1
- 下一站
- 帖子2是空的,所以不要显示它(排名2)
- 将
$k
增加1。
我们到此为止。你的$k
每篇帖子都会增加,甚至那些被跳过的帖子,所以第三位是第三位而不是第二位
我的代码在做什么:
- 将
$k
设置为1
- 帖子1不是空的,所以显示它(排名1)
- 将
$k
增加1
- 下一站
- 帖子2是空的,所以不要显示它(排名2)
- 下一站
- 帖子3不是空的,所以显示它(仍然排名2)
我希望我能帮上忙。热门排名帖子
-
//即使第二个正文数据丢失(当前为第三个数据),仍希望以$k显示数字2。
改变了什么?
我移动了$k++代码>在if
的内部,就在else
为什么?
您的代码在做什么:
- 将
$k
设置为1
- 帖子1不是空的,所以显示它(排名1)
- 将
$k
增加1
- 下一站
- 帖子2是空的,所以不要显示它(排名2)
- 将
$k
增加1。
我们到此为止。你的$k
每篇帖子都会增加,甚至那些被跳过的帖子,所以第三位是第三位而不是第二位
我的代码在做什么:
- 将
$k
设置为1
- 帖子1不是空的,所以显示它(排名1)
- 将
$k
增加1
- 下一站
- 帖子2是空的,所以不要显示它(排名2)
- 下一站
- 帖子3不是空的,所以显示它(仍然排名2)
我希望我能帮上忙。只要把$k
移到里面就行了?这将显示2,3,4,5,6,7,8,因此您还必须将$k
声明为0作为值,而不是1。我的解释可能会令人困惑,但例如,将有7个数据(1,2,3,4,5,6,7),但由于跳过了2和5,它将是(1,3,4,6,7),但我想将其显示为(1,2,3,4,5)。我知道。如果在If
中移动$k
增量,它将显示1,2,3,4,5只需在If
中移动$k
?这将显示2,3,4,5,6,7,8,因此您还必须将$k
声明为0作为值,而不是1。我的解释可能会令人困惑,但例如,将有7个数据(1,2,3,4,5,6,7),但由于跳过了2和5,它将是(1,3,4,6,7),但我想将其显示为(1,2,3,4,5)。我知道。如果在If
中移动$k
增量,它将显示1,2,3,4,5