Jump to content

first() not working


onjegolders
 Share

Recommended Posts

I'm trying to switch from using jQuery cycle to the Twitter Bootstrap carousel and it requires the first item to have a class of "active". Sounds easy enough but doesn't seem to be working. My test is true every time and every item gets the "active" class appended to it.

Any ideas where I'm going wrong?

<div class="span4 offset1">
<div id="galleries" class="carousel slide">
 <!-- Carousel items -->
  <div class="carousel-inner">
 <?php
$galleries = $pages->find("template=gallery, sort=-created, limit=3");
$class = "item";
foreach ($galleries as $gallery) {
if ($gallery === $galleries->first()) {
$class .= " active";
}
?>
  <div class="<?php echo $class; ?>">
   <a href="<?php echo $gallery->url; ?>">
<img src="<?php echo $gallery->images->first()->url; ?>" alt="" />
<h6><?php echo $gallery->title; ?></h6>
</a>
  </div>
<?php } ?>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#galleries" data-slide="prev">‹</a>
<a class="carousel-control right" href="#galleries" data-slide="next">›</a>
</div>
</div>

Funny as when i try with last() the class is applied correctly...

Link to comment
Share on other sites

Try this, hope it helps:

foreach ($galleries as $gallery) {
//Try with "==" instead of "==="
if ($gallery == $galleries->first()) {
$class .= " active";
}
//Another solution
foreach ($galleries as $k => $gallery) {
if ($k == 0) {
$class .= " active";
}

Link to comment
Share on other sites

if ($gallery === $galleries->first()) {

This is correct code with ===!

Your problem is you have the "active" already in variable after first time. SO correct is:

foreach ($galleries as $gallery) {
 $class = '';
 if ($gallery === $galleries->first()) $class .= " active";
 ...

Problem solved. Easy if you think enough about it ;)

  • Like 1
Link to comment
Share on other sites

Hm, I guess it should work... have you tried with == instead of ===?

In despair, you can always do this:

$i = 1;
foreach ($galleries as $gallery) {
if ($i==1) {
$class .= " active";
$i++;
}

oops... Soma was faster and better :)

Link to comment
Share on other sites

Ops, ok that's a little different then but still same concept. Then you can do something like this?

$class = "item";
foreach ($galleries as $gallery) {
 $status = '';
 if ($gallery === $galleries->first()) {
   $status .= " active";
 }
 $class .= $status;
...

Don't worry we all do it wrong from time to time, even if you done it many times ;)

  • Like 1
Link to comment
Share on other sites

Closing php and opening php again many times, cluthers the readibility.

Think the code below is much more human readable:

<?php
$output = '';
$class = 'item';

foreach ($galleries as $gallery) {
   $status = '';

   if ($gallery === $galleries->first()) $status .= ' active';
   $class .= $status;

   $output .=
       "<div class='{$class}'>
           <a href='{$gallery->url}'>
               <img src='{$gallery->images->first()->url}' alt='' />
               <h6>{$gallery->title}</h6>
           </a>
       </div>";
}
echo $output;
?>

(ps did not test it)

  • Like 2
Link to comment
Share on other sites

Closing php and opening php again many times, cluthers the readibility.

Think the code below is much more human readable:

<?php
$output = '';
$class = 'item';

foreach ($galleries as $gallery) {
$status = '';

if ($gallery === $galleries->first()) $status .= ' active';
$class .= $status;

$output .=
"<div class='{$class}'>
<a href='{$gallery->url}'>
<img src='{$gallery->images->first()->url}' alt='' />
<h6>{$gallery->title}</h6>
</a>
</div>";
}
echo $output;
?>

(ps did not test it)

Thanks, I just find it easier to manipulate html outside of php. Brackets, single, double quotes etc. It is a pain going in and out but I just find it easier for now

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...