johnyp Posted July 17, 2012 Share Posted July 17, 2012 Can anyone see the error here? What i am trying to do is declare 3 variables for 15 categories named $var_category number_1 up to 3. The error must be in the variables $features_$x_1 name but i am too new to find out. Thank you. // <!-- ---------------------------------------------------------------------------------------------------- --> $x=1; while($x <= 15) { $results = mysql_query("SELECT * FROM features where catid = $x"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_$x_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_$x_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_$x_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } $x=$x+1; } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/ Share on other sites More sharing options...
scootstah Posted July 17, 2012 Share Posted July 17, 2012 You would need variable variables here. Something along the lines of: if ($i > 00 && $i <= $ii){$var_name = "$features_{$x}_1"; $$var_name .= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} But, that looks hideous. Can't you just use arrays? $features[$x] = "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>"; Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362175 Share on other sites More sharing options...
johnyp Posted July 17, 2012 Author Share Posted July 17, 2012 Thank you scootstah. No i can not use array because the variable has the category number and the split number also. This does not work also: $x=1; while($x <= 15) { $results = mysql_query("SELECT * FROM features where catid = $x"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$var_name = "$features_{$x}_1"; $$var_name .= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$var_name = "$features_{$x}_2"; $$var_name .= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$var_name = "$features_{$x}_3"; $$var_name .= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } $x=$x+1; } Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362181 Share on other sites More sharing options...
scootstah Posted July 17, 2012 Share Posted July 17, 2012 What about, $features[$x][1], $features[$x][2], $features[$x][3] Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362184 Share on other sites More sharing options...
johnyp Posted July 17, 2012 Author Share Posted July 17, 2012 I must include the variable later and i can use code for explode that is why i need variables only. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362190 Share on other sites More sharing options...
johnyp Posted July 17, 2012 Author Share Posted July 17, 2012 If i use array how can i include the data when needed? I now use echo $var name. Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362237 Share on other sites More sharing options...
scootstah Posted July 17, 2012 Share Posted July 17, 2012 You can use the index, or loop through it. Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362302 Share on other sites More sharing options...
johnyp Posted July 17, 2012 Author Share Posted July 17, 2012 Can you give me a small example please? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362313 Share on other sites More sharing options...
scootstah Posted July 18, 2012 Share Posted July 18, 2012 http://us.php.net/manual/en/language.types.array.php Quote Link to comment https://forums.phpfreaks.com/topic/265828-declare-variable-using-other-incr-variable/#findComment-1362316 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.