How to set first record as active from a query result in php [closed]

4 weeks ago 26
ARTICLE AD BOX

I'm building a carousel for testimonials fetching records from a mysql database with php and showing them in a bootstrap carousel. The bootstrap carousel requires that the first record (testimonial) is set as 'active'

What I am trying to achive is setting a variable if the index of the array is 0

IE is the first record in the array. so I can set the variable thus..

############### IF THE INDEX OF THE FIRST ITEM FROM THE ARRAY IS '0'###

##if ($array==['0']){$activated='active';}else{$activated='';}

################ THE above attempt does not appear to work ###########

<div class="carousel-item active">

Here is the code I am using to get the records from the database.

<?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); include_once("site/config/config.php"); include_once("site/config/functions.php"); $connection = mysqli_connect($host,$usr,$pwd,$db); if ($connection->connect_errno) { error_log("Connection failed: " . $connection->connect_error); die("Database connection error."); } //MySqli Select Query $results = $connection->query("SELECT id, sender_name, pagelink, submission_date,comments,source FROM testimonials WHERE is_live='checked' order by RAND() DESC LIMIT 8"); if($results->num_rows > 0) { while($row = $results->fetch_assoc()) { $sender_name = $row["sender_name"]; $sender_name = stripslashes($sender_name); $comments = $row["comments"]; $comments = stripslashes($comments); $source = $row["source"]; $source = stripslashes($source); $source = strtolower($source); $pagelink = $row["pagelink"]; $pub_datenice = date("F Y",strtotime($row['submission_date'])); #TRUNCATE NEWS STORY $comments = strip_tags($comments); $comments= truncate($comments, 200); ############### IF THE INDEX OF THE FIRST ITEM FROM THE ARRAY IS '0'### ##if ($array==['0']){$activated='active';}else{$activated='';} ################ THE above attempt does not appear to work ########### print '<div class="item .'$activated">'; print '<div class="testimonial-6">'; print '<div class="testimonial-text bg-white quote-left quote-right">'; print '<p>'. $comments.'</p>'; print '</div>'; print '<div class="testimonial-detail clearfix bg-primary text-white">'; print '<h3 class="testimonial-name m-tb0">'.$sender_name.'</h3>'; print '<div class="testimonial-pic radius"><img src="../../images/testimonials/'.$source.'.png" alt="'.$source.'" width="100" height="100" ></div>'; print '</div>'; print '</div>'; print '</div>'; } } else { echo 'Nothing to show'; } // close connection $connection->close(); ?>

The above works fine, but I need the first record printed to page to be set as 'active' and the rest set to:

<div class="carousel-item">

So assuming the first record result in the result HAS AN INDEX OF [0] what I am after I guess is something like below but I don't know how to write the following...

if (array value =='0'){$activated='active';}else{$activated='';}

Then I can output

print '<div class="item .'$activated'.">';
Read Entire Article