首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CakePHP2中没有排名信息时无法跳过排名职位

问在CakePHP2中没有排名信息时无法跳过排名职位
EN

Stack Overflow用户
提问于 2017-04-08 09:27:17
回答 1查看 36关注 0票数 0

很难用文字来解释它,但我想要做的是在CakePhp2中没有排名信息时跳过排名。例如,我有以下数据,其中包含7行排名数据。2在第7行中包含空体(第2行和第5行)。所以我做了一个脚本来跳过空的排名。然而,自从第二行和第五行已经被跳过。显示的数据是1,3,4,6,7。但是我想显示的排名是1,2,3,4,5。换句话说,我想把第3位显示为第2位和第4位,第3位等等。很抱歉我解释得不太好。简单地说,当没有排名信息时,我想跳过排名,并显示排名编号。(PS:我不想改变DB)我很想听到你的消息!

代码语言:javascript
复制
   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 } } }

试图制作一个脚本来显示流行的排名。我真的很喜欢听你给我一些很棒的提示和样品!

代码语言:javascript
复制
 <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++; } ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-08 17:26:01

代码语言:javascript
复制
 <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。
  • Post 1不是空的,所以显示它(等级1)。
  • $k增加1。
  • 去下一篇文章
  • 2号柱是空的,所以不要显示它(等级2)。
  • $k 增加1.

我们到此为止吧。你的$k随着每一篇文章而增加,即使是那些跳过的,所以第三位是第三位而不是第二位。

我的代码在做什么:

  • 将$k设置为1。
  • Post 1不是空的,所以显示它(等级1)。
  • $k增加1。
  • 去下一篇文章
  • 2号柱是空的,所以不要显示它(等级2)。
  • 去下一篇文章
  • Post 3不是空的,所以显示它(仍然是2)。

希望我能帮上忙。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43292350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档