5

Easy one, but I can't find an answer.

I wanna to list sub-categories to create tabs in code below. This one works fine in my output. But the first tab needs a <li class= "selected" > to show the content on page load .

How to force the first echo result to include a class on <li> element ?

.

.

.

[ Output ]

Actual: .......... Category 1 | Category 2 | Category 3

Needed: .......... Category 1* | Category 2 | Category 3

*class="selected"

  <ul class="cd-tabs-navigation">

  <?php
  $argumentos = array(
  'hide_empty' => 1,
  'child_of' => 44,
  );
  $categories = get_categories($argumentos);
  foreach($categories as $category) {
  echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
  }
  ?>

  </ul>

.

.

[ EDIT ]

The solution by Lal. Thank you everyone!

  foreach($categories as $category) {
  if(++$i==1)
  echo '<li class="selected"><a class="selected" data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
  else
  echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
  }
rdllngr
  • 223
  • 3
  • 10
  • why do you want the class to be added? is it for applying styles? – Lal Oct 01 '15 at 18:07
  • I need the class to active one script. I'm using this code system. http://codyhouse.co/gem/responsive-tabbed-navigation/ – rdllngr Oct 01 '15 at 18:09

3 Answers3

3

I would suggest you to use a counter.. Not sure if this is the right way to do it..

Change your foreach as follows

$i=0;
foreach($categories as $category) {
  if($i==0)
    echo '<li class="selected"><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
  else
    echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
  $i++;
  }

OR

As suggested in the answer here, you could use array_shift() to process the first item in the array separatley.

That is, do it as below

$first = array_shift($categories);
echo '<li class="selected"><a data-content="' . $first ->slug . '" href="#' . $first ->slug . '">' . $first ->name . '</a></li>';
foreach($categories as $category) {
  echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}

Read more about array_shift() in the docs

Community
  • 1
  • 1
Lal
  • 14,726
  • 4
  • 45
  • 70
  • Almost! I just need the value (++$i == 1) instead 0 to work! I included the final code in question. Simple and clean. Thank you very much, pal! – rdllngr Oct 01 '15 at 18:30
  • 2
    @Lal You're not incrementing `$i` in your first example, so `$i == 0` will always be true. – Drown Oct 01 '15 at 19:13
1

Lal's solution will work fine (as long as you increment $i), but here's a simpler way to do it without an extra variable for the counter :

foreach($categories as $index => $category){
   if(!$index) echo "This is the first element";
   else echo "This is not the first element";
}

(not sure if this should be an edit or an answer since it's basically the same method but with a slightly simpler synthax, I'll post as an answer for now)

Drown
  • 5,852
  • 1
  • 30
  • 49
0

You can use this syntax for foreach. Using this syntax you have in $k the index of the iteration, while in $v the value of the array you're iterating. So if $k == 0 you're in the first iteration.

$a = array("first", "second", "third", "fourth");

foreach ($a as $k=>$v) {
    echo $k . "--->" . $v ;
    if($k == 0){
        echo "First iteration!";
        //Do your stuff 
    }
    echo "<br>";
}
gmast
  • 192
  • 8