-2

I have tried to solve it and look around but not even sure what I should be searching for.

I have made a product grid through a while loop, in the loop with each product and input-tag has been used for users to mark how many items of each product is wanted.

The product grid

However I have trouble distinguishing the value of each input field and what "name" it should be stored under to be able to retrieve it when running the second script of processing the order? I also need to be able to connect an id with each value.

The code for the grid: I know I need to make a unique name in the input name, however how and which makes sense?

<div id="content">
    <h1>Products</h1>

    <form action="processorder.php" method="post">
    <table align="center">
        <?php

        $db = include "connect2db.php"; 

        mysqli_set_charset($db,"utf8");

        $query = 'SELECT * FROM products_josie';

        $result = $db->query($query);


        $count = 0;
        while($res=$result->fetch_assoc())
        {
            if($count==3)
            {
               echo '</tr>';
               $count = 0;
            }
            if($count==0)
               echo '<tr>';
               echo '<td>';
               ?>

               <a href="productsdetails.php?clickedid=<?php echo $res['product_id']?>">
               <img src="products/<?php echo $res['photo']; ?>" width="200" height="150"/>
               </a>
               <br/>
            <?php
            echo '<p>';
            echo $res['product_name'];
            echo '</br>';
            echo 'DKK ';
            echo $res['price'];
            echo '</p>';
            echo '<p>';
            echo '<input type="number" name="amount" min="0"';
            echo '</p>';
            $count++;
            print '</td>';
        }
        if($count>0)
            print '</tr>';
        ?>
    </table>

    <input type="submit" value="Submit Order">
    </form>
</div>
3
  • This looks like ugly code, if you know php then you should make an empty variable. then concentrate it like this $var .= new thing then you can maybe more able to play with variable
    – user8713979
    Dec 8, 2017 at 14:30
  • I know it's not beautiful but when just trying to see if you could get the function to work then this was the temporary code. As I'm learning I do not know everything and ask for help. I have tried searching some about the method you mentions but have a hard time finding more about it so I actually will be able to use it? Dec 8, 2017 at 14:37
  • Animesh means "concatenate" not "concentrate"
    – ADyson
    Dec 8, 2017 at 14:38

1 Answer 1

1

I think what you are looking for is how to get the amounts for different products. If so, something like this might do it:

$prod = $res['product_name'];
echo '<input type="number" name="amount[$prod]" min="0"';
1
  • Exactly! And of course it seems like the obvious solution now. Thanks Dec 8, 2017 at 14:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.