php - Unable to skip ranking Post when there is no ranking information in CakePHP2
It's hard to explain it with words, but what I want to do is skip the ranking when there is no ranking information in CakePhp2. For example, I have the following data that contains 7 rows of ranking data. 2 out of the 7 row contains the empty body(2nd and the 5th row). So I made a script to skip the ranking post that is empty. However, Since the 2nd and the 5th rows that have been skipped. The data post that is displayed is 1,3,4,6,7. But I want to the display the ranking like 1,2,3,4,5. In other words, I want to show the 3rd ranking as 2nd and 4th as 3rd and so forth. Sorry for my poor explanation. Simply, I want to skip ranking when there is no ranking information and show the ranking number. (PS: I don't want to alter the DB) I will love to hear from you!
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 } } }
Trying to make a script to show the Popular ranking. I really love to hear about some great Hints and samples from you!
<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++; } ?>
Answer
Solution:
What was changed?
I moved
$k++;
inside ofif
, just beforeelse
Why?
What was your code doing:
$k
to 1.$k
by 1.$k
by 1.Let's stop there. Your
$k
was increased with every post, even those skipped, so the third position was 3rd instead of 2nd.What is my code doing:
$k
to 1.$k
by 1.I hope I helped.