Jump to content

[SOLVED] For loops


jeeves245

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895513
Share on other sites

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).

 

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895528
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-895535
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896002
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/169744-solved-for-loops/#findComment-896038
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.