mysqli_fetch_array how to get index number

P_Star77 :

I'm fetching data from mysql table with mysqli fetch array and after the while of fetching i want to the carousel item showing what his index number from the array so i can identify them using the index number for giving active property

<?php
                                    $qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");
                                    while($row = mysqli_fetch_array($qry, MYSQLI_ASSOC)){
                                        $qrygbr = mysqli_query($koneksi, "SELECT * FROM t_gambar WHERE id_berita = '".$row['id']."'");;
                                        $rowcount=mysqli_num_rows($qry);
                                        while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) { 
                                            # code...
                                ?>
                                <div class="carousel-item active">
                                    <div class="card">
                                        <div class="card-body" style="padding: 0;">
                                            <div class="row">
                                                <div class="col-lg-6"><img class="img-fluid" style="width: 100%;height: 100%;background-image: url(data:image/jpeg;base64,<?php echo base64_encode($rowgbr['gambar']);?>);background-position: center;background-size: cover;background-repeat: no-repeat;" /></div>
                                                <div class="col-lg-6">
                                                    <div class="d-md-flex d-lg-flex flex-column justify-content-md-center align-items-md-center justify-content-lg-center align-items-lg-start" style="padding-top: 20px;padding-left: 10px;padding-right: 10px;height: 100%;margin-bottom: 20px;">
                                                        <h4><?php
                                                            echo $row['judul'];
                                                        ?></h4>
                                                        <h6 class="text-muted card-subtitle mb-2">
                                                            <?php
                                                                echo $row['tanggal'].$rowgbr[];
                                                            ?>
                                                        </h6>
                                                        <p>
                                                            <?php
                                                                echo $row['artikel'];
                                                            ?>
                                                        </p>
                                                        <p>
                                                        </p>
                                                        <div class="d-flex"></div><a class="btn btn-primary" role="button" href="bacaberita.php?id=<?php echo $row['id'] ?>">Read More</a></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php
                                        }
                                    }
                                    mysqli_close($koneksi);
                                ?>

Mech :

The following should help you understand what you need to do. I've applied a $count variable within the loop to increase as the loop continues. I've added the variable to a

to show you how it works.

<?php
                                    $koneksi = mysqli_connect('localhost', 'admbumm_DedSec', 'omnimon786.,', 'admbumm_selipin');
                                    $qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");
                                    while($row = mysqli_fetch_array($qry, MYSQLI_ASSOC)){
                                        $qrygbr = mysqli_query($koneksi, "SELECT * FROM t_gambar WHERE id_berita = '".$row['id']."'");
                                        $rowcount=mysqli_num_rows($qry);
                                        $count = 1;
                                        while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) { 
                                            # code...

                                ?>
                                <div class="carousel-item active">
                                    <div class="card">
                                        <div class="card-body" style="padding: 0;">
                                            <div class="row">
                                                <div class="col-lg-6"><img class="img-fluid" style="width: 100%;height: 100%;background-image: url(data:image/jpeg;base64,<?php echo base64_encode($rowgbr['gambar']);?>);background-position: center;background-size: cover;background-repeat: no-repeat;" /></div>
                                                <div class="col-lg-6">
                                                    <div class="d-md-flex d-lg-flex flex-column justify-content-md-center align-items-md-center justify-content-lg-center align-items-lg-start" style="padding-top: 20px;padding-left: 10px;padding-right: 10px;height: 100%;margin-bottom: 20px;">
                                                        <h4><?php
                                                            echo $row['judul'];
                                                        ?></h4>
                                                        <h6 class="text-muted card-subtitle mb-2">
                                                            <?php
                                                                echo $row['tanggal'].$rowgbr[];
                                                            ?>
                                                        </h6>
                                                        <p>
                                                            <?php
                                                                echo $row['artikel'];
                                                            ?>
                                                        </p>
                                                        <p>
                                                        <p>
                                                            <?php
                                                                echo $count;
                                                            ?>
                                                        </p>
                                                        </p>
                                                        <div class="d-flex"></div><a class="btn btn-primary" role="button" href="bacaberita.php?id=<?php echo $row['id'] ?>">Read More</a></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php
                                $count = $count + 1;
                                        }
                                    }
                                    mysqli_close($koneksi);
                                ?>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=8118&siteId=1