Jump to content


Photo

Any way to create php variables from a for loop


  • Please log in to reply
4 replies to this topic

#1 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 09 March 2006 - 01:37 PM

Hi

Does any one have any ideas how to create varaibles from a for loop that will be accessible on this page and can be posted to other pages

Heres what I have

for($i = 0; $i < 6; $i++)
{
$g = "gender".$i;
$d = "day".$i;
$m = "month".$i;
$y = "year".$i;
$e= "education".$i;

$gender."$i" = $_POST[$g];
$day."$i" = $_POST[$d];
$month."$i" = $_POST[$m];
$year."$i" = $_POST[$y];

$today = date("Y-m-d");
$t = explode("-", $today);

$person_age = calculate_age($day, $month, $year);
$education = $_POST[$e];

echo "Gender: " . $gender."$i" . " Day: " . $day."$i" . " Month: " . $month."$i" . " Year: " . $year."$i" . " Education: " . $education."$i" . '<br>';
}

I want to be able to create 6 sets of varaibles for gender, age, month, year, education and be able to access them on the page as separate variables.

many thanks,

Fergal.


#2 littlened

littlened
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 09 March 2006 - 01:41 PM

[!--quoteo(post=353234:date=Mar 9 2006, 01:37 PM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 9 2006, 01:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi

Does any one have any ideas how to create varaibles from a for loop that will be accessible on this page and can be posted to other pages

Heres what I have

for($i = 0; $i < 6; $i++)
{
$g = "gender".$i;
$d = "day".$i;
$m = "month".$i;
$y = "year".$i;
$e= "education".$i;

$gender."$i" = $_POST[$g];
$day."$i" = $_POST[$d];
$month."$i" = $_POST[$m];
$year."$i" = $_POST[$y];

$today = date("Y-m-d");
$t = explode("-", $today);

$person_age = calculate_age($day, $month, $year);
$education = $_POST[$e];

echo "Gender: " . $gender."$i" . " Day: " . $day."$i" . " Month: " . $month."$i" . " Year: " . $year."$i" . " Education: " . $education."$i" . '<br>';
}

I want to be able to create 6 sets of varaibles for gender, age, month, year, education and be able to access them on the page as separate variables.

many thanks,

Fergal.
[/quote]

I don't understand what you want, but maybe using an array would help? for example $year[$i], $day[$i].




#3 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 09 March 2006 - 02:34 PM

Never thought of an array I will try this thanks

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 09 March 2006 - 03:22 PM

[!--quoteo(post=353234:date=Mar 9 2006, 02:37 PM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 9 2006, 02:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi

Does any one have any ideas how to create varaibles from a for loop that will be accessible on this page and can be posted to other pages

Heres what I have

for($i = 0; $i < 6; $i++)
{
$g = "gender".$i;
$d = "day".$i;
$m = "month".$i;
$y = "year".$i;
$e= "education".$i;

$gender."$i" = $_POST[$g];
$day."$i" = $_POST[$d];
$month."$i" = $_POST[$m];
$year."$i" = $_POST[$y];

$today = date("Y-m-d");
$t = explode("-", $today);

$person_age = calculate_age($day, $month, $year);
$education = $_POST[$e];

echo "Gender: " . $gender."$i" . " Day: " . $day."$i" . " Month: " . $month."$i" . " Year: " . $year."$i" . " Education: " . $education."$i" . '<br>';
}

I want to be able to create 6 sets of varaibles for gender, age, month, year, education and be able to access them on the page as separate variables.

many thanks,

Fergal.
[/quote]
look this
<?php
$test1="one";
$a="test";
$b=1;
$c=2;
$d=3;
${$a.$c}="two"; //$test2='two'
${"test".$d}="ten"; //$test3='ten'
$e="test4";
$test4="xyz";
echo ${$a.$b}."</ br>\n";
echo ${"test".$c}."</ br>\n";
echo ${$a.$d}."</ br>\n";
echo $$e."</ br>\n";
$$e="abc"; // $test4='abc'
echo $test4;
?>


#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 09 March 2006 - 03:36 PM

you may also just want to check into the extract() function. it will pull all the contents of your $_POST variable into their own variables.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users