I know that are several questions similar to this one, and i have looked at them, but still i haven't managed to modify my function in order to work.
So i have this function getCar_2(); in functions.php page. The thing is that it should display more cars, because i have more cars in the database, but it displays only the last one. Same with getCar_1(), the two functions are the same.
Here is the function:
<?php
function getCar_2(){
global $connect;
$text = "";
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
$text = "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
return $text;
}
?>
and is displayed on another page like this:
<?php
$car_1 = getCar_1();
$car_2 = getCar_2();
if(!empty($car_1) || !empty($car_2)){
?>
<div class="block">
<div class="up">
<div class="title"><h4>Cars</h4></div>
</div>
<div class="down">
<div class="column_1"><b><?php echo $car_1; ?></b></div>
<div class="column_2"><b><?php echo $car_2; ?></b></div>
</div>
</div>
<?php
}
?>
You are not concatenating your text
variable, so on each loop pass it will contain only the last value. You should use concatenation operator .=
instead of assignment operator =
$text .= "