homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
variable inside array
dkin




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

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




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

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




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

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




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


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




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

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




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

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




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

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




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

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




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

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




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

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! ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved