Jump to content

Input box with DB value questions


Ingenious

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.