vozzek Posted November 29, 2007 Share Posted November 29, 2007 Okay, new problem: I've got a shopping cart page that's now a form and I'm submitting one text and two hidden inputs for use on the update_cart.php page. This page gets called when the user posts the form. I'm using the variable $itemct to determine how many items are in the user's cart. I pass this along as a hidden input, and that's no problem. However, I have to also pass along the CartId and Quantity for each row of the person's shopping cart. Within my php code, I'm echoing html inputs. I'm appending $itemct to the end of the input id tags/names like so: <input type='hidden' name='itemct' id='itemct' value='<?php echo $itemct ?>' /> // No problem here <?php echo "<input type='text' size='1' maxlength='2' id=qty".$itemct." name=qty".$itemct." value=".$row['ct_qty']." />"; echo "<input type='hidden' id='hidCartId".$itemct."' name='hidCartId".$itemct."' value=".$row['ct_id']." />"; ?> So in a three-item shopping cart, the id's for these inputs would be: hidCartId1, hidCartId2, and hidCartId3 and qty1, qty2, qty3. So far so good. NOW... on the next page, I'm going to loop through these inputs and do an SQL update for each hidCartId. I'm using a FOR loop, ending at the total $itemct. <?php for ($counter = 1; $counter <= $itemct; $counter += 1) { $newqty = "$qty".$counter; // This isn't working... $cartid = "$hidCartId".$counter; // This isn't working... $sql = "UPDATE tbl_cart SET ct_qty = $newqty WHERE ct_id = $cartid"; $result = mysql_query($sql) or die(mysql_error()); } ?> My problem lies in somehow recognizing or calling the POST variables. Since they've got numbers at the ends of them, how do I somehow append the loop $counter to them so I can access them? I'm not sure what to do here, but I'm sure it involves nested quotes and apostrophies! Please help? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/79475-solved-incremented-variable-problems-big-ones/ Share on other sites More sharing options...
roopurt18 Posted November 29, 2007 Share Posted November 29, 2007 With your current set up, you could do this: <?php foreach($_POST as $key => $val){ if(substr($key, 0, 3) == 'qty'){ // quantity field, get the id $id = str_replace('qty', '', $key); // get the matching hidden id field $hidden = $_POST['hidCartId' . $id]; // Now $hidden is the hidden field and $val is the qty field // Do whatever } } ?> The better approach is to name your inputs so that you can do away with this altogether: <?php echo "<input type='text' size='1' maxlength='2' id=qty".$itemct." name='qty[{$row['ct_id']}]' value=".$row['ct_qty']." />"; ?> This will cause $_POST to have an array named 'qty'. Each entry in $_POST['qty'] will be of the form 'ct_id' => 'qty'. Basically you are putting the id field directly into the input's name attribute, so you no longer need a hidden field and you no longer need to match them up. <?php // processing foreach($_POST['qty'] as $id => $qty){ // $id is the id and $qty is the quantity, much easier! } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79475-solved-incremented-variable-problems-big-ones/#findComment-402431 Share on other sites More sharing options...
vozzek Posted November 29, 2007 Author Share Posted November 29, 2007 WOW... That 2nd method with the array... totally awesome. I wasn't quite sure how to do that, I need to learn more about the higher-level php features. But the way you explained it - I'm doing it that way. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/79475-solved-incremented-variable-problems-big-ones/#findComment-402433 Share on other sites More sharing options...
roopurt18 Posted November 29, 2007 Share Posted November 29, 2007 That 2nd method with the array... totally awesome. I wasn't quite sure how to do that, I need to learn more about the higher-level php features. But the way you explained it - I'm doing it that way. I used to do things the way you were doing them, by appending the ID or whatever to the end of the name. I did it that way for a very long time. Then one day while working with a select with multiple="yes" I realized something, even though I'd used them before: $_POST can contain arrays! So unbelievably simple and yet so hard to miss. Anyways, since then I always write my name attributes in such a way that the incoming data in $_POST is already attached where ever it needs to be. At least as much as possible; sometimes it's just simpler to do it the other way. Also, I notice you have an id attribute on your inputs. Are you using any Javascript? If you aren't, then you can safely drop the id attributes. If you are, I can show you another "trick." Quote Link to comment https://forums.phpfreaks.com/topic/79475-solved-incremented-variable-problems-big-ones/#findComment-402437 Share on other sites More sharing options...
vozzek Posted November 29, 2007 Author Share Posted November 29, 2007 I can totally relate. It's so easy to do something the hard way for a long, long time... then all of a sudden you realize you could've saved yourself a lot of time and energy if you'd sat down to learn it the RIGHT way. In the last two months I went from knowing nothing but a few basic html commands to learning php, javascript, and mySQL. My code is a cluster of all three right now. I wasn't sure whether I needed the id's or not, but kept them in there anyway. On some pages of my site I call some javascript scripts to validate the inputs before going to POST... gotta idiot-proof the user as much as possible. But of course, you can never anticipate every idiotic thing an idiot will do. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/79475-solved-incremented-variable-problems-big-ones/#findComment-402474 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.