Ridge Robinson Ridge Robinson - 1 year ago 68
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
1 saab 100
--------------------------- (this is a table border)
2 bmw 200

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">
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;


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 Source

Use str_replace() on your cars

$cars = str_replace(",", "<br/>", $row['cars']);
$output .= '<td>'.$cars.'</td>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download