Jump to content

Using "while" when assigning variables?


Theora

Recommended Posts

I'm relatively new to php so I may not have been using the correct terminology when searching for an answer to this question. Any help you can give or pointers in the right direction would be greatly appreciated!

 

I have a form that's created using the variable "$days". The code for the table rows withing the form looks something like this:

 

$i = 1;
while ($i <= $days):
    echo "
<tr>
      	<td><input name=\"date$i\" type=\"text\" size=\"10\" /></td>
      	<td><input name=\"location$i\" type=\"text\" size=\"25\" /></td>
      	<td><input name=\"hours$i\" type=\"text\" size=\"5\" /></td>
    	</tr>
";
    $i++;
endwhile;

 

The form gets sent to a php file that uses the mail() function. If it were to just send one row of data, it would look like this:

<?php
@extract($_POST);
$date = stripslashes($date);
$location = stripslashes($location);
$hours = stripslashes($hours);
mail('address@wherever.com',"subject line","$date: $location ($hours hours)");
header("location:whereit'sredirected.php");
?>

 

What I want it to do -- and I don't know if it's even possible -- is to have it get the value of $days (let's say 3 for now) and create variables $date1, $date2, $date3, $location1, $location2, $location3, etc. And, even if it's possible to do that, how would you then work the text area of the mail to let it know how many variables there were?

 

Thanks in advance!

Link to comment
Share on other sites

You can do what you want:

<?php
  // These will create: $days0, $days1, $days2, $days3, $days4, ...
  for($i = 0; $i < 10; $i++){
    ${"days" . $i} = $i;
  }
?>

 

However, I think you will find your life easier if you store each row of data as an element in an array:

<?php
  $rows = Array();
  for($i = 0; $i < 10; $i++){
    $rows[] = Array( "date" => $date_val,
                           "location" => $location_val,
                           "hours" => $hours_val
                  );
  }
?>

 

This will allow you to use a foreach while building your table:

<?php
foreach($rows as $row){
  echo "
<tr>
      	<td><input name=\"{$row['date']}\" type=\"text\" size=\"10\" /></td>
       	<td><input name=\"{$row['location']}\" type=\"text\" size=\"25\" /></td>
       	<td><input name=\"{$row['hours']\" type=\"text\" size=\"5\" /></td>
      	</tr>
  	";}
?>

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.