Jump to content

Any way to create php variables from a for loop


fohanlon

Recommended Posts

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.
Link to comment
Share on other sites

[!--quoteo(post=353234:date=Mar 9 2006, 01:37 PM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 9 2006, 01:37 PM) [snapback]353234[/snapback][/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].


Link to comment
Share on other sites

[!--quoteo(post=353234:date=Mar 9 2006, 02:37 PM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 9 2006, 02:37 PM) [snapback]353234[/snapback][/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[code]<?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;
?>[/code]
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.