用文字解释它很难,但我想做的是在CakePhp2中没有排名信息时跳过排名。例如,我有以下数据包含7行排名数据。 7行中的2个包含空体(第2行和第5行)。所以我制作了一个脚本来跳过空的排名帖子。但是,由于已跳过第2行和第5行。显示的数据帖子是1,3,4,6,7。但我希望显示排名像1,2,3,4,5。换句话说,我想将第3排名显示为第2和第4排名第3等等。对不起,我的解释很糟糕。简单地说,我想在没有排名信息时跳过排名并显示排名。 (PS:我不想改变数据库)我很乐意听取你的意见!
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++; } ?>
答案 0 :(得分:3)
<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 $k++; } ?>
<?php } else {
continue;
}?>
</ul>
我在$k++;
之前移动了if
,else
$k
设为1。$k
增加1。$k
提高1。 让我们停在那里。每个帖子都会增加$k
,即使是跳过的帖子也是如此,所以第三个位置是第3位而不是第2位。
$k
设为1。$k
增加1。我希望我能帮忙。