Jump to content


Photo

Concatenation of two variables (sort of)


  • Please log in to reply
6 replies to this topic

#1 MartynA

MartynA
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 July 2006 - 07:52 PM

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:

$variable = "7";
print "$row2->user" . "$variable";

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 July 2006 - 08:03 PM

one possible solution:
<?php
$variable = 7;
eval("print \"\$row2->user$variable;");
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 MartynA

MartynA
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 July 2006 - 08:31 PM

Thanks; close but no cigar.

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

eval("print \"\$row2->user$variable\";");

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



#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 08:36 PM



try this??
print ${'row2->user'.$variable};

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 July 2006 - 08:41 PM

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

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

#6 MartynA

MartynA
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 July 2006 - 09:27 PM

Try changing your mysql_fetch to mysql_fetch_assoc(), then you would reference the field with $row2['user']
and the code to print:

<?php print $row2['user'] . $variable; ?>

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


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.

#7 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 25 July 2006 - 11:34 PM

This will work:
print( $row2->{'user' .$variable} );
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users