Welcome to WebmasterWorld Guest from 54.196.238.210

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

variable inside array

     

dkin

2:04 am on Apr 26, 2006 (gmt 0)

10+ Year Member



Im having a little trouble with something fairly small.

basically I have 3 rows named

row1
row2
row3

now, my code is in a loop so I am not sure how to tell it to call row 1 then 2 then 3, the loop only runs 3 times.

I was trying something like this

$row['row$x']

Which obviously didnt work so I am confused anyone be of any help?

Thanks

Turk

eelixduppy

2:09 am on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Hello...

Do you mean something like this:

$row1 = "some text";
$row2 = "some more text";
$row3 = "some random text of doom";
$row = array($row1, $row2, $row3);
for($i = 0; $i < 3; $i++)
{
echo $row[$i]."<br>";
}

//instead of echoing this you can use it for something else

Other than this im really not sure what you are asking

Hope this helps

eelix

dkin

2:24 am on Apr 26, 2006 (gmt 0)

10+ Year Member



you put me on the right track so thank you, I ended up getting it like this

. '<option value="'.$row[row.''.$x].'">'.$row[row.''.$x].'</option>'

Thanks Again

Turk

hakre

11:16 am on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I was trying something like this

$row['row$x']

Which obviously didnt work so I am confused anyone be of any help?

it will work while using double quotation marks:

$row["row$x"]
, because only within double quoted string variables will be expanded. it's called variable parsing [php.net].

volatilegx

4:28 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a problem with the for loop above, because it starts with '0', and should start with '1'... It should be:

$row1 = "some text";
$row2 = "some more text";
$row3 = "some random text of doom";
$row = array($row1, $row2, $row3);
for($i = 1; $i <= count($row); $i++)
{
echo "<option value=\"$row[$i]\">$row[$i]</option>";
}

eelixduppy

10:54 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Hello volatilegx!

Not to be rude or anything, but i don't understand why you would start the index variable ($i) at 1 because then the first value in the array won't be used. If i am looking at this entirely the wrong way please let me know.

Thank you in advance...

eelix

whoisgregg

11:48 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Variable variables [us2.php.net] to the rescue!*

$row1 = "some text<br>";
$row2 = "some more text<br>";
$row3 = "some random text of doom<br>";
for($i=1; $i <= 3; $i++){
echo ${row.$i};
}

*At least, if I understood the problem correctly. ;)

grandpa

5:34 am on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i don't understand why you would start the index variable ($i) at 1

You didn't define a $row0. Your loop incrementor(?) begins at zero. for($i = 0; $i < 3; $i++). The first iteration will produce an empty result (possibly an error, I haven't checked). You will never echo $row3.

echo $row[0];
echo $row[1];
echo $row[2];

eelixduppy

5:59 am on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



I think im still confused...

Quoted from [us2.php.net...]

"When index is omitted, an integer index is automatically generated, starting at 0."

That would mean that the index should start at 0 and not 1, and going up to 3 will give an error. It would have to start at 1 if the array was defined like

$row = array(1 => $text1, $text2, $text3);

hmmm....oh well...

eelix

hakre

6:53 am on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would have to start at 1 if the array was defined like
$row = array(1 => $text1, $text2, $text3);

just in case you really define an array like this, then a print_r($row); [php.net] will tell you the truth:


Array (
[1] => some text
[2] => some more text
[3] => some random text of doom
)

you're right! ;)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month