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
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.