Harlequin Posted November 14, 2007 Share Posted November 14, 2007 Hi guys. Bit stuck here. I have a form that uses the database to display results, no issues there. However, I need to be able to acertain what the user has selected when the form has been submitted and I've been trying for about a week now to get the thing working. Here's where I'm at... <?php while($row = mysql_fetch_array($Result01)) { ?> <tr> <td><?php echo $row['Brand']; ?></td> <td><?php echo $row['Description']; ?></td> <td><?php echo $row['Retail']; ?></td> <td> <input type="hidden" name="Id[]" value="<?php echo $row['Id']; ?>" /> <input type="hidden" name="Retail[]" value="<?php echo $row['Retail']; ?>" /> <input type="hidden" name="Brand[]" value="<?php echo $row['Brand']; ?>" /> <input type="hidden" name="Description[]" value="<?php echo $row['Description']; ?>" /> <input type="text" name="Qty[]" size="2" maxlength="2" /> </td> </tr> <?php } ?> When the user submits I can see the arrays generates but need to do the following: 1. Ascertain which items were actually selected by the user. 2. How many of each item selected was chosen. I'm quite sure I'm close to finishing it but this last stumbling block has me beat and any help would be appreciated... Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 14, 2007 Share Posted November 14, 2007 So basically you need to work out which items the user has entered a value into the Qty field? Without knowing what you're doing with these results, its a little difficult to give something specific. But this would be the general idea: <?php foreach($_POST['Qty'] as $k => $v){ if(strlen($v) > 0 && $v!=0 && ctype_digit($v)){//if the field contains some value, which is not 0 but is an integer echo 'User ordered '.$v.' of item with ID no:'.$_POST['Id'][$k]; } } ?> Quote Link to comment Share on other sites More sharing options...
Harlequin Posted November 15, 2007 Author Share Posted November 15, 2007 GingerRobot What I need to do is code these results into some useable format so I can create session variables for the price and id so what you've given me allows me to do this. I'm very grateful, I should be able to code some useful results from this nowbut if I get stuck could I come back to you...? Quote Link to comment Share on other sites More sharing options...
axiom82 Posted November 15, 2007 Share Posted November 15, 2007 Here is a cleaner version: if (!empty ($_POST['Qty'])){ foreach ($_POST['Qty'] as $key => $quantity){ if (!empty ($quantity)){ echo "User ordered {$quantity} of item with ID no: {$_POST['Id'][$key]}"; } } Quote Link to comment Share on other sites More sharing options...
Harlequin Posted November 15, 2007 Author Share Posted November 15, 2007 Hi guys. Thanks very much for your help so far on this. The code as it stands does what it says on the tin: <?php // Get Post Data: $Items = $_POST['Id']; $Brands = $_POST['Brand']; $Descriptions = $_POST['Description']; $Prices = $_POST['Retail']; $Qtys = $_POST['Qty']; ?> <p>Items: <?php echo var_dump ($Items); ?></p> <p>Brands: <?php echo var_dump ($Brands); ?></p> <p>Descriptions: <?php echo var_dump ($Descriptions); ?></p> <p>Prices: <?php echo var_dump ($Prices); ?></p> <p>Qtys: <?php echo var_dump ($Qtys); ?></p> <?php if (!empty ($_POST['Qty'])) { foreach ($_POST['Qty'] as $key => $quantity) { if (!empty ($quantity)) { echo "User ordered {$quantity} of item with ID no: {$_POST['Id'][$key]}"; } } } ?> Which produces this: Items: array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" } Brands: array(5) { [0]=> string( "McVities" [1]=> string( "McVities" [2]=> string( "McVities" [3]=> string(5) "Carrs" [4]=> string(6) "Jacobs" } Descriptions: array(5) { [0]=> string(13) "Hob Nobs 250g" [1]=> string(30) "Milk Choc Digestive 250g tube" [2]=> string(13) "Rich Tea 200g" [3]=> string(28) "Water Biscuits Bite Size125g" [4]=> string(19) "Cream Crackers 200g" } Prices: array(5) { [0]=> string(4) "2.33" [1]=> string(4) "2.58" [2]=> string(4) "1.39" [3]=> string(4) "2.14" [4]=> string(4) "2.12" } Qtys: array(5) { [0]=> string(2) "11" [1]=> string(2) "12" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" } User ordered 11 of item with ID no: 1User ordered 12 of item with ID no: 2 What I actually need to do is the following: For each item that has a value for $_POST['Qty'] I need to get the price and ID. I know I'm probably explaining this badly but can you see what I mean...? Quote Link to comment 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.