Jump to content

Archived

This topic is now archived and is closed to further replies.

MartynA

Concatenation of two variables (sort of)

Recommended Posts

Hi,

I hope one of you guys can help, I have a slight problem involving arrays and variables:

I have an object array, $row2 populated with data pulled from mysql. Hence $row2->name grabs the name part of the query.

Now, i want to do a foreach loop to display some of these, specifically user2, user7 etc.

Long story short, how do I concatenate $row2->user with the array key - or any thing else for that matter:

[code]$variable = "7";
print "$row2->user" . "$variable";
[/code]

This doesn't work, and only outputs 7, not the contents of $row2->user7.

I suspect I'm missing something obvious here, anyone wanna fill me in?

Thanks,

Mart

Share this post


Link to post
Share on other sites
one possible solution:
[code]
<?php
$variable = 7;
eval("print \"\$row2->user$variable;");
?>
[/code]

hope this helps

Share this post


Link to post
Share on other sites
Thanks; close but no cigar.

Your code gave me an error so I looked up the eval syntax and came up with

[code]eval("print \"\$row2->user$variable\";");[/code]

However, that doesn't output anything. At all. What I am doing wrong?

Share this post


Link to post
Share on other sites
Try changing your mysql_fetch to mysql_fetch_assoc(), then you would reference the field with $row2['user']
and the code to print:
[code]<?php print $row2['user'] . $variable; ?>[/code]

The next question is ... why do you want to do this? Maybe if you explain what you want to do, we can come up with an easier way of doing it.

Ken

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=101836.msg403418#msg403418 date=1153860104]
Try changing your mysql_fetch to mysql_fetch_assoc(), then you would reference the field with $row2['user']
and the code to print:
[code]<?php print $row2['user'] . $variable; ?>[/code]

The next question is ... why do you want to do this? Maybe if you explain what you want to do, we can come up with an easier way of doing it.

Ken
[/quote]

Thanks Ken, that did it. For any future reference, I was wanting to foreach through an array containing values such as 5,7,6 and then echo out the equivalent, e.g. $row2->user$valueinarray. That way I could define what information I wanted printing just by passing an array to the function.

Thanks, and sorry for not explaining myself so well.

Share this post


Link to post
Share on other sites

×

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.