Jump to content

Comparison loop help.


vmicchia

Recommended Posts

I have a couple of loops that are supposed to 1) list all the values of one table as check boxes then 2)compare them to the values of another table. If they match that checkbox is supposed to be pre-checked. I have some code but it is not working just right yet and I was hoping someone could help me.

 

 <?php 
include("../../../cart/includes/openDbConn.php");
$sql = "SELECT Name FROM FabricType";
$dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";';
$result = mysql_query($sql);
while($results = mysql_fetch_array($result))
{
  if( $i % 3 == 0 )
  {
echo '</tr><tr>';   
  }
  $result2 = mysql_query($dogettype);
  while($results2 = mysql_fetch_array($result2))
  {
    if(trim($results2['Type']) == trim($results['Name']))
    {
  $ischecked = 'checked="checked"';
  $value = 1;
  	}
else
{
  $ischecked = '';
  $value = 0;
}
  }
$i++;
$tn = trim(ucfirst($results["Name"]));
    echo '<td>
		<label>
			<input type="checkbox" name="typeGroup1[]" '.$ischecked.' value="'.$results['Name'].'">
			'.$tn.'
		</label>
		</td>';
  
} 
?>

 

With this code it echoes the check boxes correctly but only checks it if it is the last value in $results2.

 

If I move the second while loop like so:

                         <?php 
include("../../../cart/includes/openDbConn.php");
$sql = "SELECT Name FROM FabricType";
$dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";';
$result = mysql_query($sql);
while($results = mysql_fetch_array($result))
{
  if( $i % 3 == 0 )
  {
echo '</tr><tr>';   
  }
  $result2 = mysql_query($dogettype);
  while($results2 = mysql_fetch_array($result2))
  {
    if(trim($results2['Type']) == trim($results['Name']))
    {
  $ischecked = 'checked="checked"';
  $value = 1;
  	}
else
{
  $ischecked = '';
  $value = 0;
}
  
$i++;
$tn = trim(ucfirst($results["Name"]));
    echo '<td>
		<label>
			<input type="checkbox" name="typeGroup1[]" '.$ischecked.' value="'.$results['Name'].'">
			'.$tn.'
		</label>
		</td>';
  }
} 
?>

 

It only echoes out the first results of $results for however many values there are in $results2.

 

Thanks for any help.

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/232964-comparison-loop-help/
Share on other sites

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.