doumen5087 2015-03-21 21:18
浏览 58

在foreach循环内增量

How do I loop through and output all the values in the array? The code I have stops after the first output.

$select_top = array($wpdb->get_results($wpdb->prepare( "SELECT * FROM `wp_top_voted` WHERE `todays_date`= CURDATE() ORDER BY `number_votes` DESC LIMIT 10", OBJECT )));

    if ($select_top){
    $i = 1; 
        foreach($select_top as $select_top_one){
        $select_top_one->post_id;
        $post_id_top[$i] = $select_top_one[$i]->post_id;    
            $number_votes[$i] = $select_top_one[$i]->number_votes;
            $size = array(380,220); 
?> 
<div class="top-layout">
  <div class="top-layout-row">
    <div class="top-layout-cell">
      <?php //remove background color. Make box look like widget. 
       ?> 
  <div class="top-layout-cell"> 
    <h4 class="top-video-title">
      <a href="<?php the_permalink(); ?>">
        <?php echo get_the_title( $post_id_top[$i] ); ?>
      </a>
    </h4>
    <a href="<?php the_permalink(); ?>">
      <?php echo get_the_post_thumbnail( $post_id_top[$i], $size ); ?>
    </a>
    <?php $i++; echo $i++; ?>
  </div>
</div>
  • 写回答

1条回答 默认 最新

  • dtpf76658 2015-03-21 21:33
    关注

    You just loop all the results and print them out. You don't need to auto increment the loop as the foreach runs for as long as there are results. This seems to be Wordpress, so take a look at the get_posts function https://codex.wordpress.org/Template_Tags/get_posts. This function works as what you're trying to do but you only need to insert your $args and then execute You'll have your array and you can loop that.

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测