jeeves245 Posted August 11, 2009 Share Posted August 11, 2009 Quick question For loops are confusing me a bit... can anyone tell me what I would need to do to the for loop in the code below to make it loop until the $date runs out of values? $id = $_POST['id']; $date = $_POST['estimatedDelivery']; foreach () { $sql2 = "UPDATE smail SET estimatedDelivery = '".$date."' WHERE estimatedDelivery = '".$id."'"; mysql_query($sql2); } Cheers Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/ Share on other sites More sharing options...
Mark Baker Posted August 11, 2009 Share Posted August 11, 2009 For loops are confusing me a bit... can anyone tell me what I would need to do to the for loop in the code below to make it loop until the $date runs out of values?Does date have more than one value? Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895489 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 For loops are confusing me a bit... can anyone tell me what I would need to do to the for loop in the code below to make it loop until the $date runs out of values?Does date have more than one value? Yes it is pulled from the database and has a dynamic number of values. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895513 Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 Please provide the data that the loop needs to loop through. A foreach loop may not be the best choice. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895514 Share on other sites More sharing options...
thebadbad Posted August 11, 2009 Share Posted August 11, 2009 Yea, show us some sample values of $id and $date. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895515 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Well.. i'll post the rest of the script (well the parts that matter). I don't know if it will work yet, i'm still fumbling my way through it with help from you guys Here's the bit that should be of interest: echo "<td> <input type='text' name='estimatedDelivery' value=".$new_date2."></td>"; echo "<td>" . $row['psNumber'] . "</td>"; echo "<input type='hidden' name='id' value=".$row['psNumber'].">"; echo "<td>" . $row['customerName'] . "</td>"; $new_date2 is just a date pulled from the database (how many values depends how many DB rows there are at the time). Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895528 Share on other sites More sharing options...
Mark Baker Posted August 11, 2009 Share Posted August 11, 2009 You'll need to modify your form to pass the dates and ids through to PHP either as arrays (the better option) or as uniquely named values Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895531 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Hmm so I assume if I modify the form to pass the values through as arrays they'll stay in the right order so when the form is being processed it will be possible to save them back to the DB in the same order? Any tips on how to modify the form to do this? I haven't done it before.. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895535 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Anyone? Still struggling with this.. it's proving to be a real pain :-\ Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895997 Share on other sites More sharing options...
thebadbad Posted August 11, 2009 Share Posted August 11, 2009 Well, you didn't provide us with some sample values for $id and $date especially. If $date e.g. looked like 2009-01-01 2008-12-12 2000-05-23 you could explode() the string and then loop through each date: <?php foreach (explode(' ', $date) as $d) { //handle each date here, updating the database or whatever } ?> But to make sense the $id should also hold several IDs. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896002 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 $id = $_POST['id']; $date = $_POST['estimatedDelivery']; $date holds one date (0000-00-00) $id holds a 5 digit number. But as the form input was in a while loop, they both hold an unknown number of the above values in the arrays. Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896017 Share on other sites More sharing options...
thebadbad Posted August 11, 2009 Share Posted August 11, 2009 Then you'll need to name your input fields estimatedDelivery[] and id[]. That'll give you two arrays ($_POST['estimatedDelivery'] and $_POST['id']) in the processing script. You can loop through them like this: <?php $count = count($_POST['estimatedDelivery']); for ($i = 0; $i < $count; $i++) { $sql2 = "UPDATE `smail` SET `estimatedDelivery` = '" . mysql_real_escape_string($_POST['estimatedDelivery'][$i]) . "' WHERE `estimatedDelivery` = '" . mysql_real_escape_string($_POST['id'][$i]) . "'"; mysql_query($sql2); } ?> But your WHERE clause is odd; shouldn't you check another field than estimatedDelivery? Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896038 Share on other sites More sharing options...
jeeves245 Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks for that As for the WHERE clause.. hmm.. well, each estimatedDelivery value matches a psNumber value. Will the for loop cycle through the array in the same order as they are put into the array? If so, then it should work. Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896122 Share on other sites More sharing options...
jeeves245 Posted August 12, 2009 Author Share Posted August 12, 2009 Never mind, works perfectly when psNumber is used in the where clause!! Thanks so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896124 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.