ahs10 Posted April 22, 2009 Share Posted April 22, 2009 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; Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2009 Share Posted April 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
ahs10 Posted April 22, 2009 Author Share Posted April 22, 2009 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.