Jump to content

[SOLVED] using variable variables


ahs10

Recommended Posts

so i understand the definition of variable variables as described in the manual (http://us.php.net/language.variables.variable), but i can't apply it to much of anything.  now often times i find the need for dynamically named variables... is that the same thing essentially?  is there a way that variable variables could improve this...

		
$q = mysql_query("SELECT * FROM myDB.myTbl) or connectGlitch(__FILE__, __LINE__, mysql_error());
$count = mysql_num_rows($q);
$i = 1;
while ($row = mysql_fetch_array($q)) {
${'icon' . $i} = array();
${'icon' . $i}['title'] = $row['icon_title'];
${'icon' . $i}['var_name'] = $row['var_name'];
${'icon' . $i}['active'] = $row['active'];
$i++;
}
$content = '<select>';
for ($i = 1; $i <= $count; $i++) {
$t = ${'icon' . $i}['title'];
$v = ${'icon' . $i}['var_name'];
$content .= '<option value="' . $v . '">' . $t . '</option>';
}
$content .= '</select>';

echo $content;

 

 

Link to comment
Share on other sites

That code is already using variable variables. But guess what, variable variables are three time slower than using an array and in the posted code you are using them twice, once to form the sequence of variables then again when you access the sequence of variables to form the content. Also, using variable variables usually results in code that is more complicated than using an array.

 

If that is all the code that uses the result of the query, just form the content in the while loop using the $row[...] variables and get rid of all the slow code using variable variables. And if that is not all the code that uses the result of the query, either just reuse the result set that the query returns or directly put it into an array variable and not a sequence of named variables.

Link to comment
Share on other sites

thanks for the reply.  the data is used again and this is a much smaller scaled version of my working code.  here's what i'll use instead, after you advice.  cheers!

 

$q = mysql_query("SELECT * FROM myDB.myTbl) or connectGlitch(__FILE__, __LINE__, mysql_error());
$count = mysql_num_rows($q);
$i = 1;
$arr = array();
while ($row = mysql_fetch_array($q)) {
$arr[$i];
$arr[$i]['title'] = $row['icon_title'];
$arr[$i]['var_name'] = $row['var_name'];
$arr[$i]['active'] = $row['active'];
$i++;
}
$content = '<select>';
for ($i = 1; $i <= $count; $i++) {
$content .= '<option value="' . $arr[$i]['var_name'] . '">' . $arr[$i]['title'] . '</option>';
}
$content .= '</select>';

echo $content;

 

 

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.