Jump to content

Concatenation of two variables (sort of)


MartynA

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
Link to comment
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?

Link to comment
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
Link to comment
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.
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.