10

I'm doing an assignment in school where we should simulate a web retail store that sends products from a database via PHP to the end user. The user should then be able to add the products to the cart by a checkbox and press a button that takes them to the cart, where only the selected items from the product table should show.

I've made some great progress, but I'm stuck on this teeny tiny problem, which is: I can't figure out how to present the selected items in a table. Let me show you how my product table looks like right now:

The product table

And the code:

<form action="selected_products.php" method="POST">
<table>
    <tr>
        <th>Select</th>
        <th>Product-ID</th>
        <th>Product Name</th>
        <th>Price SEK</th>
    <tr>

<?php
// Print out all entries in a table
foreach ($rows as $value) {
    extract($value);

    echo "<tr><td><input type='checkbox' name='SelectedData[]' alt='Checkbox' value='$id&$name&$price'></td>";
    echo "</td><td>".$id.'';
    echo "</td><td>".$name.'';
    echo "</td><td>".$price.'';
    echo "</td></tr>";
}
?>
</table>
<br>
<input type="submit" value="Add to cart" />
</form>

When the user selects the products with the checkbox and presses the button, it takes them to the selected_products.php page, which would look like this:

The selected products

And the code:

<table>
    <tr>
        <th>Product-ID</th>
        <th>Product Name</th>
        <th>Price SEK</th>
    <tr>
<?php
if(!empty($_POST['SelectedData'])) {
    foreach($_POST['SelectedData'] as $selected) {
            echo "<tr><td>".$selected.'';
            echo "</td></tr>";
    }
}
?>

So, my question is: how can I make the selected products fill out the entire table, instead of being mushed up in one column like they are now? I do pass it with value='$id&$name&$price', but how can I make this value string to put every entry in its corresponding column on the selected_products.php page?

4
  • You need to split string by explode function, but what if your product name is L&T or H&P ? Commented Nov 28, 2016 at 9:05
  • value='$id|$name|$price' Commented Nov 28, 2016 at 9:08
  • Instead of using "&", just like @mplungjan said, use pipe "|" for imploding set of values. Commented Nov 28, 2016 at 9:13
  • 2
    I'd suggest the usage of a custom delimiter instead of single characters like &, | etc. for e.g ~!~ Commented Nov 28, 2016 at 9:14

2 Answers 2

5

As already answered in the comments you could use explode() to solve this problem.

foreach($_POST['SelectedData'] as $selected) {
    list($id, $name, $price) = explode("&", $selected);
    echo "<tr><td>".$id.'';
    echo "</td><td>".$name.'';
    echo "</td><td>".$price.'';
    echo "</td></tr>";
}

but this is a rather bad idea because it allows everybody to fill it with whatever they want. This is because everybody can send a post request with any content they want. A better way would be to set selected to the id of the item and then read the data you need from the database again using the id of the item. This way only items that are in the database can be displayed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you everyone! I have read about explode before, but I wasn't sure on how to use it, or if it would work on my project. Thank you all once again!
3

Split your string at &.

explode("&",$selected);

This creates an array which holds the values in seperate.

[0] => 3
[1] => Samsung 960 Pro
[2] => 3150

Good look with your project ;)

UPDATE: Like @mplungjan said: use pipe for seperating the values. Then go

explode("|", $selected);

3 Comments

unless he has an AT&T computer - do note I commented this 10 minutes ago
That´s right. He should care about this – but this is the answer to what he asked for.
So mark it as duplicate of the link in the comment I gave

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.