1

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);
                                ?>

1
  • Either you create a counter variable and increment that on each loop or swtich the while loop for a foreach. In that way, you can use the foreach($array as $key => $value) statement Commented Feb 26, 2020 at 2:09

3 Answers 3

1

change the 2nd query

$qrygbr = mysqli_query($koneksi, "SELECT  @a:=@a+1 serial_number,t_gambar.* FROM t_gambar, (SELECT @a:= 0) AS a WHERE t_gambar.id_berita = '".$row['id']."'");

while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) {
    echo $rowgbr['serial_number']; //this is you your needed serial number
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);
                                ?>

Comments

0

you might want to add another variable inside your SQL Query like so $qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");

to $qry = mysqli_query($koneksi, "SELECT COUNT(*) as c,* FROM t_berita ORDER BY tanggal DESC LIMIT 3");

basically, SQL will count the total rows available and put it c. So you can just do some simple loop with c inside your PHP code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.