Search code examples
twitter-bootstraptwitter-bootstrap-3osclass

Bootstrap carousel with OS-Class


I need to create dynamically bootstrap carousel like this:

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    etc...
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
    <div class="item active">
      <img src="someIMG.jpg" alt="...">

    </div>
    <div class="item">
      <img src="someIMG-nn.jpg" alt="...">

    </div>
    etc...
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
  </a>
</div>

Now I have a osclass functions which I call to create dynamically bootstrap carousel:

**<?php osc_run_hook('item_detail', osc_item() ) ; ?>
                    <?php if( osc_images_enabled_at_items() && (osc_count_item_resources() > 0) ) { ?>**

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
**<?php $i=0;?>**
                            **<?php while( osc_has_item_resources() ) { ?>**
    <li data-target="#carousel-example-generic" data-slide-to="**<?php echo $i; $i+1;?>**" class="active"></li>
    **<?php } ?>** 
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
**<?php while( osc_has_item_resources() ) { ?>**
    <div class="item">
      <img src="**<?php echo osc_resource_url(); ?>**" alt="...">


    </div>
    **<?php } ?>** 


  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
  </a>


</div>

I'm beginner so I don't know what can be a problem here. In my logic I think that I write good but what I write don't work... Two while is the problem or something else?

please help, sorry for my English. THANKS!


Solution

  • You're close. But you're using that loop twice and that may not work. I'm not sure what osc_has_item_resources() does with the records. But try this.

    Set a variable equal to whatever is in osc_count_item_resources() and then loop that many times to build the carousel indicators. After you do that, use while( osc_has_item_resources() ) to loop through and build the items.

    <?php osc_run_hook('item_detail', osc_item() ) ; ?>
    <?php if( osc_images_enabled_at_items() && (osc_count_item_resources() > 0) ) { ?>
    
    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
      <ol class="carousel-indicators">
    <?php $itemCount = osc_count_item_resources(); ?>
    <?php for($i = 0; $i < $itemCount; $i++) { ?>
        <li data-target="#carousel-example-generic" data-slide-to="<?php echo $i; ?>" class="active"></li>
    <?php } ?>
      </ol>
    
      <!-- Wrapper for slides -->
      <div class="carousel-inner">
    <?php $i = 0; ?>
    <?php while( osc_has_item_resources() ) { ?>
        <div class="item<?php echo ($i === 0) ? ' active': ''; ?>">
          <img src="<?php echo osc_resource_url(); ?>" alt="...">
        </div>
    <?php $i++; ?>
        <?php } ?>
      </div>
      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
      </a>
      <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
      </a>
    </div>