PDA

View Full Version : Generating A Variable From Other Variables


ideffect
07-20-2005, 07:11 PM
Hi,

I hope I can explain what I am trying to do. Upon completion of a script a bunch of things with while loops, database inserts and a few e-mails. I now have the need of inserting a few diffrent variables that were pulled from the database and display them in a while loop using the loop number as a variable. I don't want to over complicate what I am trying to do since I am only stuck on one small part. First, here is a sample I mad to test out what I am trying to do.

<?
$is_1pc = "YES!";
$z = 1;
echo is_.$z.pc;
?>


Now what I am trying to do is have the echo statment display YES! Right now it will display is_1pc. I'm sure it's easy enough to see what I am trying to do so I won't try to explain it. I'll just confuse you and me... :p

So what do I need to do?
Thank you SOOOOO much for the help!

chump2877
07-20-2005, 07:26 PM
echo $is_1pc;

OR

echo 'YES!';

ideffect
07-20-2005, 07:36 PM
That would work but that is not my question.

This is more like what I am trying to do

<?
$is_1pc = "YES! -1";
$is_2pc = "YES! -2";
$is_3pc = "YES! -3";
$i = 0;
$z = 1;

while($i < 3)
{
$j = $i+1;
echo is_.$j.pc;
echo "<br>";
$i++;
}
?>


The above would output
is_1pc
is_2pc
is_3pc

What I want it to do is output
YES! -1
YES! -2
YES! -3

Does this make more sense?

chump2877
07-20-2005, 07:47 PM
<?

$i = 0;

while($i < 3)
{
$j = $i+1;
echo 'Yes!' . ' -' . $j;
echo "<br>";
$i++;
}
?>

ideffect
07-20-2005, 07:53 PM
This part of the script is just a sample.
$is_1pc = "YES! -1";
$is_2pc = "YES! -2";
$is_3pc = "YES! -3";
The value of each item will be diffrent and unknown. It pulls the data out of a database. I need a way to display the variable's value using something like this is_.$z.pc;. But instead of listing the variable, I need it to display the value of the variable.
Using the statment 'Yes!' . ' -' . $j; hard codes part of the variable which is the problem.

chump2877
07-20-2005, 07:57 PM
<?

$is_1pc = "YES! -1";
$is_2pc = "YES! -2";
$is_3pc = "YES! -3";

$i = 0;


while($i < 3)
{
$j = $i+1;

if ($j == 1)
{
echo "YES! -1" . "br";
}
elseif ($j == 2)
{
echo "YES! -2" . "br";
}
else
{
echo "YES! -3" . "br";
}

$i++;
}

?>

chump2877
07-20-2005, 08:03 PM
or change your variable names:

<?
$is_pc[1] = "YES! -1";
$is_pc[2] = "YES! -2";
$is_pc[3] = "YES! -3";
$i = 0;
$z = 1;

while($i < 3)
{
$j = $i+1;
echo is_pc[$j];
echo "<br>";
$i++;
}
?>

ideffect
07-20-2005, 08:13 PM
I tried the last one and got this
Parse error: parse error, unexpected '[', expecting ',' or ';' on line 12

It doesn't like is_pc[$j] and the second code won't work for this..

chump2877
07-20-2005, 08:17 PM
oopssss

chump2877
07-20-2005, 08:18 PM
sorry forgot a $....

<?
$is_pc[1] = "YES! -1";
$is_pc[2] = "YES! -2";
$is_pc[3] = "YES! -3";
$i = 0;

while($i < 3)
{
$j = $i+1;
echo $is_pc[$j] . "<br>";
$i++;
}
?>

singedpiper
07-20-2005, 08:22 PM
yes, it is absolutely definite that you want an array... google can tell you the basic idea behind them, and give you a more succinct tutorial. they are a basic cornerstone of any programming.

ideffect
07-20-2005, 08:31 PM
Thanks chump2877!

That should work good!

chump2877
07-20-2005, 08:33 PM
glad to be of service.... ;)

Kurashu
07-20-2005, 08:35 PM
<?

$is_1pc = "YES! -1";
$is_2pc = "YES! -2";
$is_3pc = "YES! -3";

$i = 0;


while($i < 3)
{
$j = $i+1;

if ($j == 1)
{
echo "YES! -1" . "br";
}
elseif ($j == 2)
{
echo "YES! -2" . "br";
}
else
{
echo "YES! -3" . "br";
}

$i++;
}

?>

What in the name of?! A simple echo 'YES! -' . $j . 'br'; would have done that.

As for the original problem:

$z = 1;
${'is_' . $z . 'pc'} = 'YES!'


That'll assign $is_1pc with YES!

But I will say that an array would probably be better. But traverse it with foreach:


$is_pc[] = "YES! -1";
$is_pc[] = "YES! -2";
$is_pc[] = "YES! -3";

foreach ($is_pc AS $val)
{
echo $val . '<br />'
}

singedpiper
07-20-2005, 08:37 PM
yes, it is absolutely definite that you want an array... google can tell you the basic idea behind them, and give you a more succinct tutorial. they are a basic cornerstone of any programming.

chump2877
07-20-2005, 08:45 PM
What in the name of?! A simple echo 'YES! -' . $j . 'br'; would have done that.

I haven;t slept in over a day, that could be part of the problem....i'm surprised I answered the question as quick as i did... :D

ideffect
07-20-2005, 08:57 PM
No offense to singedpiper and Kurashu but this script is very unique script with a lot of abilities. You can't say you should use an array since you don't know the script. I thought of using an array but that would have seriously over complicated it. I'll be the first one to admit that I am new to PHP so I may be wrong.. Once again, thank you Chump for your help! Get some rest...

Oh and by the way it works perfect in my script!