Jump to content

complicated string/array/variable issue


Pyro4816

Recommended Posts

For this example, $row2['author'] = "1"

$user = 'user'.$row2['author'];
if (!isset($$user)){
      $result = $connector->query('SELECT * FROM users WHERE ID = '.$row2['author']);
      $$user = $connector->fetchArray($result);
}
echo '<a href="user.php?user='.($$user['ID']).'">'.$$user.'</a></p>';

The problem is it is a weird variable that also has an array, but when i use

echo $$user['ID'];

It errors out saying Notice:

Undefined variable: u

 

So I am wondering how to word it to set the variable up properly, and how to call items in the array. Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/223105-complicated-stringarrayvariable-issue/
Share on other sites

It wouldn't let me edit again, but an update on it is that

echo $user1['ID'];

Works, but since this is in a while loop, and i am trying to prevent too many server queries, im trying to set it so that if i need user 1's info again it wont query the database again, but it will query it if there is a user 2 who has not had his info retrieved.

I would use an array from the start

i.e.

$user = 'user'.$row2['author'];
$userArray = array();
if (!isset($$user)){
      $result = $connector->query('SELECT * FROM users WHERE ID = '.$row2['author']);
      $userArray[$user] = $connector->fetchArray($result);
}
echo '<a href="user.php?user='.$userArray[$user]['ID'].'">'.$user.'</a></p>';

 

play around with that, otherwise do print_r($$user); to ensure the variable variable array is setup as you wish.

Archived

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

×
×
  • 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.