Thursday 16 July 2015

Find the first & last element of an array while using a foreach loop in PHP

  • You could use a counter:

<?php

$i = 0;
$len = count($array);
foreach ($array as $item) {
    if ($i == 0) {
        // first
    } else if ($i == $len - 1) {
        // last
    }
    // …
    $i++;
}
?> 
  • Your method requires a database query for every single collection, category and subcategory. So if you have 10 collections, 100 categories and 1000 subcategories, you will have at least 1110 database queries.
    You should try to do it with just one query (see table joins) or at least one query per table.
  • A proper used list is always better than a div soup. So try to use ul and li.
 

No comments:

Post a Comment