Ingenious Posted February 9, 2012 Share Posted February 9, 2012 Good day, I have 2 questions about that. Here is the context. I have a list of items that i query from a database and insert in a table. The last field of the table is a input box to typpe in the quantity ("qty"). My first question, how can I associate the inputbox to product_id from the database for that item. //database connecting is working <? while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['product_code'] . "</td><td>" . $row['product_name'] . "</td><td><input type='text' maxlength = '3' value='0'></td></tr>"; } And my second question (any tutorial reference) about how to select only the items that the qty is not = 0 and pass it to another page either by sessions or other means. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/256710-input-box-with-db-value-questions/ Share on other sites More sharing options...
codebyren Posted February 9, 2012 Share Posted February 9, 2012 To associate a qty field to a product ID, you can name the qty fields as an array with the product ID as the key. Something like: <?php echo "<tr><td>" . $row['product_code'] . "</td><td>" . $row['product_name'] . "</td><td><input type='text' maxlength = '3' value='0' name='qty[". $row['product_id'] . "]'></td></tr>"; ?> Then once the form has been submitted (assuming data was submitted using the post method), you can loop through the qty fields that were submitted and determine which have values greater than 0: <?php $products = isset($_POST['qty']) AND is_array($_POST['qty']) ? $_POST['qty'] : array(); if ($products) { foreach ($products as $product_id => $qty) { $qty = (int) $qty; $product_id = (int) $product_id; if ($qty > 0) echo "User wants $qty of product with ID $product_id <br>"; } } ?> So basically, you need to be looking for tutorials on forms and processing forms with PHP - these will be quite easy to find via Google. Quote Link to comment https://forums.phpfreaks.com/topic/256710-input-box-with-db-value-questions/#findComment-1316026 Share on other sites More sharing options...
Ingenious Posted February 10, 2012 Author Share Posted February 10, 2012 Thank you I will try that. Quote Link to comment https://forums.phpfreaks.com/topic/256710-input-box-with-db-value-questions/#findComment-1316390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.