Jump to content

Declare variable using other incr. variable.


johnyp

Recommended Posts

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();

?>

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>";

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;       
}

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.