Jump to content

Recommended Posts

Ok so I have a few check box fields that are dynamically populated from a database. What I need to do is check the box if it is a selected value(this is an edit form for a product) so here's what I have but it is not working.

 

<?php 
						include("../../../cart/includes/openDbConn.php");
						$sql = "SELECT Name FROM FabricType";
						$result = mysql_query($sql);
						$gettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";';
							$dogettype = mysql_query($gettype);
							$vtype = mysql_fetch_assoc($dogettype);
							$count = count($vtype);
							$i = 0;
						while($row = mysql_fetch_array($result)){

								if( $i % 3 == 0 ) {
								echo '</tr><tr>';   
  								}
							while($vtype = mysql_fetch_assoc($dogettype)){
								if($vtyp['Type'] == $row['Name']){
									echo '<td><label><input type="checkbox" name="typeGroup1[]" checked="checked" value="'.trim($row["Name"]).'">'.trim(ucfirst($row["Name"])).'</label</td>';
								}else{
									echo '<td><label><input type="checkbox" name="typeGroup1[]"   value="'.trim($row["Name"]).'">'.trim(ucfirst($row["Name"])).'</label</td>';
								}
							}
							$i++;
						}

					 ?>

 

When I do this it echoes nothing out.

 

Link to comment
https://forums.phpfreaks.com/topic/232775-form-filling-issue/
Share on other sites

slightly tidier, but not tested

<?php 
include("../../../cart/includes/openDbConn.php");
$sql = "SELECT Name FROM FabricType";
$dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";';
$result = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while($results = mysql_fetch_array($result))
{
  if( $i % 3 == 0 )
  {
echo '</tr><tr>';   
  }
  $result2 = mysql_db_query($dbname,$dogettype,$link) or die("Invalid Query<br>". mysql_error());
  while($results2 = mysql_fetch_array($result2))
  {
    if($results2['Type'] == $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="$value">
			'.$tn.'
		</label>
		</td>';
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197538
Share on other sites

Ok Maybe not solved. When $results2['Type']) has more than one value it will only check the last value in the array. The code I have now looks like this:

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

  {

// echo $results2['Type'];

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

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197695
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.