0

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!

2
  • Then just store price individually for each car. It would be soooo much easier...
    – Shadow
    Commented Jun 22, 2016 at 12:30
  • try with explode(). Commented Jun 22, 2016 at 12:30

3 Answers 3

2

using explode()

while($row = $result->fetch_array()) {
      $output = '<tr>';
              $output .= '<td>'.$row['shopID'].'</td>';
              $car = explode(",",$row['cars']);
              $output .= '<td>';
              foreach($car as $c)
              {
              $output .= $c."</br>";
              }
              $output .='</td>';
              $output .= '<td>'.$row['price'].'</td>';
              $output .= '</tr>';
              echo $output;
 }

using str_replace()

 while($row = $result->fetch_array()) {
      $output = '<tr>';
              $output .= '<td>'.$row['shopID'].'</td>';
              $cars = str_replace(",", "<br/>", $row['cars']);
              $output .= '<td>'.$cars.'</td>';
              $output .= '<td>'.$row['price'].'</td>';
              $output .= '</tr>';
              echo $output;
 }
1
  • Hi Kevin...good answer..it is nice seeing multiple ways of achieving this. I tested both answers and they both work, but I have already accepted an answer before your answer was given. Commented Jun 22, 2016 at 12:55
1

Use str_replace() on your cars

$cars = str_replace(",", "<br/>", $row['cars']);
$output .= '<td>'.$cars.'</td>';
1
  • This worked out well...I should have thought of this idea. I was trying to think too complex, ha! Thanks. I will accept answer in a couple minutes when SO allows it Commented Jun 22, 2016 at 12:36
1

You can use str_replace to modify $row['cars'].

$carsMultiLine = str_replace(", ", "<br />", $row['cars']);

1
  • Thanks for the solution Ryan27...this did work, but I used the answer from @Ian because he had posted it first. Commented Jun 22, 2016 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.