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