Welcome Guest, Not a member yet? Register   Sign In
Determining last result in a loop
#1

[eluser]mikegeorgeff[/eluser]
I have a navigation bar that is being generated dynamically from my database. Between each link there is a divider line and I have a css class that removes the divider line from the last result. How can I determine what the last result is and echo the css class only on that result?
#2

[eluser]Mauricio de Abreu Antunes[/eluser]
Post your code (between code tag), please.
#3

[eluser]mikegeorgeff[/eluser]
view code:

Code:
<ul>
   &lt;?php foreach ($nav as $n): ?&gt;
   <li><a href="&lt;?php echo base_url();?&gt;&lt;?php echo $n['slug'];?&gt;">&lt;?php echo $n['title'];?&gt;</a></li>
   &lt;?php endforeach; ?&gt;
</ul>

model code:

Code:
function nav()
{
  $data = array();
  $this->db->select('id, title, slug')
     ->from('pages')
     ->where('parent_id =', 0)
     ->order_by('id', 'asc');
  $q = $this->db->get();
  if ($q->num_rows() > 0)
  {
   foreach ($q->result_array() as $row)
   {
    $data[] = $row;
   }
  }
  $q->free_result();
  return $data;
}
#4

[eluser]CroNiX[/eluser]
Code:
$array = array('first', 'second', 'third');
  
//get the last element of the array
$end = end($array);
  
echo '<ul>';
foreach($array as $value)
{
//check to see if this element is the last one, if so, add the class
$class = ($value == $end) ? ' class="last"' : '';
echo '<li' . $class . '>' . $value . '</li>';
}
echo '</ul>';
#5

[eluser]Mauricio de Abreu Antunes[/eluser]
Code:
$total_el = count($nav);
$cont     = 0;

foreach () {
if ($cont == $total_el) //Last index
$cont++;
}

Or you can combine:
end() advances array 's internal pointer to the last element, and returns its value.
key() returns the index element of the current array position.
#6

[eluser]Mauricio de Abreu Antunes[/eluser]
Nice tip, Cronix.
#7

[eluser]Aken[/eluser]
Keep in mind that if you have duplicate values in your array, end() may not function correctly. If your last element is equal to other elements in the array, that comparison will return true for the duplicates. Run this and you'll see what I mean:

Code:
$array = array('first', 'middle', 'last', 'middle', 'last');

$end = end($array);

foreach ($array as $a)
{
echo $a;
if ($a == $end) echo ' END';
echo '<br>';
}

I prefer the method Mauricio suggested, except his implementation has a small bug. In his example, you'd want to either do $count++ before the comparison, or start $i at 1 instead of 0.

Here's how I would do it:

Code:
$array = array('first', 'middle', 'last', 'middle', 'last');

$count = count($array);
$i = 0;

foreach ($array as $a)
{
echo $a;
if (++$i == $count) echo ' END';
echo '<br>';
}
#8

[eluser]PhilTem[/eluser]
If you consider speed and have a lot of elements within your menu, you don't want any if-statement within the foreach loop. So you could just get the last element with

Code:
$array = array('first', 'middle', 'last', 'middle', 'last');

$last_element = array_pop($array);

echo $last_element; // echos 'last' (the second last ;) )

and then loop over every other element and append the last-element manually.
#9

[eluser]Mauricio de Abreu Antunes[/eluser]
Thanks for your reply, Aken.




Theme © iAndrew 2016 - Forum software by © MyBB