简体   繁体   English

如何向回返孩子增加php限制和订购?

[英]How do I add php limit and order to the returning children?

Two things really. 真的有两件事。

  1. I'm trying to limit the returned children, as an example 8 articles. 我试图限制返回的孩子,例如8篇文章。 I've tried array_slice and other techniques though just can't get anything to work. 我已经尝试过array_slice和其他技术,尽管什么都无法工作。
  2. I'd also like to return these children in ascending order based on my 'modified_date' column in my database. 我还想根据数据库中的“ modified_date”列以升序返回这些子级。

Really appreciate any guidance on this, been working hard for nights, can't get anything to work :( 非常感谢任何指导,努力工作了整晚,什么都没用:(

<?php
    // get all pages
    $pages = new TFW_Navigation($pageNav);
    $pages = $pages->getNavigation();
      foreach ($pages as $index => $topPage)
    {
    $children = $topPage->getChildren(null,true);
    if (!empty($children)) {
    foreach($children as $child) {
  ?>

  <article class="<?=$child->ext_column_1 ?>">
     <a href="<?= $child->url ?>" class='article-border'>
        <img src="<?=$child->ext_column_2 ?>" alt="">
          <header>
              <h3>
                  <?= $child->ext_column_3 ?>
              </h3>
          </header>
          <footer>
              <p>
                <?php echo date("d F Y", strtotime($child->last_modified))."<span class='sprite article-link'></span>" ?>
              </p>
          </footer>
      </a>
  </article>

  <?php
        }
      }
    }
  ?>

If more information is needed, please let me know. 如果需要更多信息,请告诉我。

Thanks, Barry 谢谢,巴里

Add a break statement. 添加一个break语句。

foreach($children as $childIndex => $child) {
    if ($childIndex > 8) {
        break;
    }

    ...
}
  1. To limit your array , using array_slice is a good solution 为了限制数组,使用array_slice是一个很好的解决方案
 $firstEight = array_slice($children, 0, 8); // First 8 items 
  1. To return array in ascending order based on your modified_date column, you have to do that from sql query. 要根据您的Modifyed_date列以升序返回数组,您必须从sql查询中执行此操作。
 $sql = "SELECT * FROM table WHERE x = y ORDER BY last_modified ASC"; 

and also you can limit the results from sql . 也可以限制sql的结果。

 $sql = "SELECT * FROM table WHERE x = y ORDER BY last_modified ASC LIMIT 0,8"; 

// Using //使用

<?php
    // get all pages
    $pages = new TFW_Navigation($pageNav);
    $pages = $pages->getNavigation();
      foreach ($pages as $index => $topPage)
    {
    $children = $topPage->getChildren(null,true);
    $firstEight = array_slice($children, 0, 8);
    if (!empty($firstEight)) {
    foreach($firstEight as $child) {
  ?>

  <article class="<?=$child->ext_column_1 ?>">
     <a href="<?= $child->url ?>" class='article-border'>
        <img src="<?=$child->ext_column_2 ?>" alt="">
          <header>
              <h3>
                  <?= $child->ext_column_3 ?>
              </h3>
          </header>
          <footer>
              <p>
                <?php echo date("d F Y", strtotime($child->last_modified))."<span class='sprite article-link'></span>" ?>
              </p>
          </footer>
      </a>
  </article>

  <?php
        }
      }
    }
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM