I'm developing a webapplication where there is a page that you can see the listed items that you placed on the website.
When I fetch some results from the DB I can end up with two cars that he/she listed. Which will end up something like this:
carID: 1
carID: 2
When I try use the function while (mysqli_stmt_fetch($stmt)
it will overwrite the last carID
and it will never end up with two cars. Here is my code:
while(mysqli_stmt_fetch($stmt)){
$car_id_array = $carid;
$final_array = array($car_id_array => array(
"brand" => $brand,
"color" => $color,
"type" => $type,
"price" => $price,
"mileage" => $mileage,
"model" => $model,
"year" => $year
)
);
}
foreach($final_array as $final_array_key => $final_array_value){
foreach($final_array_value as $car_id_array_key => $car_id_array_value){
echo $final_array_key . " ----> " . $car_id_array_key . ":" . $car_id_array_value;
echo "<br>";
}
}
I would like to end up with the two carID
into the final_array
.
Why are you using two arrays when you can end up using only indexes?
Try this code:
$i++;
while(mysqli_stmt_fetch($stmt)){
$final_array[$i] = array(
"carid" => $carid,
"brand" => $brand,
"color" => $color,
"type" => $type,
"price" => $price,
"mileage" => $mileage,
"model" => $model,
"year" => $year
);
$i++;
}
foreach ($final_array as $key => $value){
echo $key . ":" . $value;
echo "<br>";
}
echo "<br>";