Ridge Robinson Ridge Robinson - 5 months ago 8
MySQL Question

PHP MySQL array inside fetch_array

I have a form where a user can input a ShopID, multiselect from a list of cars, and a price for the group. The list of cars inserts into the database such as "audi, saab, volvo".

I am now trying to display a table that shows all the ShopID, selected cars and group price. Ideally, this is what the table would look like:

Shop ID Cars Price
audi
1 saab 100
volvo
--------------------------- (this is a table border)
saab
2 bmw 200
ford
honda


This table shows the shop ID that is associated with all cars and the group price (rather than a shopID and a price for each car).

Right now, however, I am getting all the cars to only show on one line such as:

Shop ID Cars Price
1 audi, saab, volvo 100


Does anybody know how I can loop through the array in "Cars" and show each one on it's own line...while keeping the "Shop ID" and "Price" only showing once and all this information in one ?
Currently, my code for creating this table is as follows:

<table class="table table-striped">
<tr>
<th>ShopID</th>
<th>Cars</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['shopID'].'</td>';
$output .= '<td>'.$row['cars'].'</td>';
$output .= '<td>'.$row['price'].'</td>';
$output .= '</tr>';

echo $output;
}
?>
</table>


Thanks!

EDIT
I did try to use explode(), but I ran into the same problem where I didn't know how to get them onto their own line!

Ian Ian
Answer

Use str_replace() on your cars

$cars = str_replace(",", "<br/>", $row['cars']);
$output .= '<td>'.$cars.'</td>';
Comments