Home > database >  How to get active class only in first div of carousel PHP
How to get active class only in first div of carousel PHP

Time:01-10

Friends thumb in the picture when the picture was selected, I could not do the Active incident.

<div id="carousel-product"  data-ride="carousel">
    <div  role="listbox">
<?php

$alt_img = '';

$query = $db->query("SELECT * 
                    FROM urun_img   
                    WHERE urun_id = '{$urun['id']}'", PDO::FETCH_ASSOC);

if($query->rowCount()){
    foreach($query as $row){
        echo '
            <div >
                <img  src="upload/'.$row['img'].'"></div>';
        $alt_img.= '
            <li data-target="#carousel-product" data-slide-to="1" >
                <img src="upload/'.$row['img'].'" alt="'.$urun['baslik'].'">
            </li>';
    }
}
?>

</div>
<ol >
<?php echo $alt_img; ?>
</ol>
</div>

CodePudding user response:

Inside your if statement, you could simply do this:

$row_count = 1;
foreach($query as $row)
{
    echo '<div ><img  src="upload/'.$row['img'].'"></div>';
    $alt_img.= '<li data-target="#carousel-product" data-slide-to="1" ><img src="upload/'.$row['img'].'" alt="'.$urun['baslik'].'"></li>';
    $row_count  ;
}

To explain this:

  • You create a $row_count variable with 1 at the value (this is the first row you're going to treat in your loop)
  • You create a condition to say "if this is the first row only, then we add the class"
  • You increment $row_count at the end of the loop, so for each new row, it will change its value to 2, then 3, then 4, etc... which then won't match the if($row_count == 1) condition.
  • Related