dougua4836 2017-04-08 09:27
浏览 9
已采纳

当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++; } ?>
  • 写回答

1条回答 默认 最新

  • doufan9395 2017-04-08 17:26
    关注
     <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>
    

    What was changed?

    I moved $k++; inside of if, just before else

    Why?

    What was your code doing:

    • Set $k to 1.
    • Post 1 isn't empty, so show it (rank 1).
    • Increase $k by 1.
    • Go to next post
    • Post 2 is empty, so don't show it (rank 2).
    • Increase $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:

    • Set $k to 1.
    • Post 1 isn't empty, so show it (rank 1).
    • Increase $k by 1.
    • Go to next post
    • Post 2 is empty, so don't show it (rank 2).
    • Go to next post
    • Post 3 isn't empty, so show it (still rank 2).

    I hope I helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀