简体   繁体   中英

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. I've tried array_slice and other techniques though just can't get anything to work.
  2. I'd also like to return these children in ascending order based on my 'modified_date' column in my database.

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.

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

    ...
}
  1. To limit your array , using array_slice is a good solution
 $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.
 $sql = "SELECT * FROM table WHERE x = y ORDER BY last_modified ASC"; 

and also you can limit the results from 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
        }
      }
    }
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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