Jump to content

multiple checkbox + db selection HELPS


tayys

Recommended Posts

Hi,

 

I m trying to add items from a master list to each sub process. But, I failed to proceed on what i intend to do.

 

1) i need to select an process from the combo box.

2) from the master list, i hv to check the item which belong to the process.

3) upon submitting, save into database.

4) the it will show those  are already checked or unchecked.

 

here is the codes i hv done:

<?php 
require_once '../../connectDB.php';
require_once '../include/paginator.class.php';

$sql="SELECT * FROM tb_item";
$result = mysql_query($sql) or die(mysql_error());

$pages = new Paginator;
$pages->items_total = mysql_num_rows($result);
$pages->mid_range = 5;
$pages->items_per_page = 100;
$pages->paginate();

$sql="SELECT * FROM tb_item ORDER BY itm_tcpd ASC $pages->limit";
$result = mysql_query($sql) or die(mysql_error());

function createComboBox()
{
$sql = "SELECT tb_process.pro_name, tb_process.pro_id, tb_sub_process.sub_id, tb_sub_process.sub_name  
		FROM tb_process INNER JOIN tb_sub_process 
		ON tb_process.pro_id = tb_sub_process.pro_id 
		ORDER BY pro_name";
$result = mysql_query($sql) or die('Cannot get Process. ' . mysql_error());
$group = array();

while ($row = mysql_fetch_assoc($result)) 
{     
	$group[$row['pro_name']][] = $row; 
}

echo "<table>
		<tr>
			<td>Add item into :</td>
			<td>
				<select name='cboSub' id='cboSub'>
	  				<option value='0' selected='selected'>----- Selection -----</option>";
			foreach ($group as $key => $values) 
			{     
echo "				<optgroup label='$key'>";     
					foreach ($values as $value)      
					{    
						$select = ($id == $value['sub_id']) ? 'selected="selected"' : NULL;     
						echo '<option value="'.$value['sub_id'].'"'.$select.'>'.$value['sub_name'].'</option>';     
					}     
echo "				</optgroup>"; 
			}
echo "			</select>
			</td>
		  </tr>
		</table>";
}

if(isset($_POST['add'])) 
{ 
$id = $_POST['checkbox'];
foreach($id as $key=>$val)
{		
	$sql = "INSERT INTO tb_record (rec_tpcd, sub_id) 
            VALUES ('$key', '$sub_id')";	
	$result = mysql_query($sql);
}
echo("	<SCRIPT LANGUAGE='JavaScript'>
		window.alert('Items has been added into the database.');
		window.location='addItem.php';
		</SCRIPT>");
}
?>
<!DOCTYPE div PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
$().ready(function() {
$("#cboSub").change("get_tpcd.php", {

});
});
</script>
</head>
<body>
<div>
<table width="100%">
	<tr>
		<td width="65%"><?php createComboBox(); ?></td>
		<td width="30%"><?php echo $pages->display_pages(); ?></td>
		<td width="5%"><?php echo $pages->display_jump_menu(); ?></td>
	</tr>
</table>
</div>
<div>
<form method='post' action=''>
  	<table width='100%' border='1' align='left' cellpadding='5' cellspacing='1'>
  		<tr>	
		<?php 	
		$numCol = 5;
		$count = 0;   	
		for($i=0; $i<$numCol; $i++)
		{
			echo "<th align='center'>TPCD No.</th>
			   	  <th align='center'>Check / Uncheck</th>";	
		}

		while ($row = mysql_fetch_array($result)) 
		{
			extract($row);
			  
			if($count % $numCol == 0) 
			{      
				echo '</tr><tr>';      
			}          

			echo "	<td align='center'>".$itm_tcpd."</td>
					<td align='right'><input type='checkbox' name='checkbox[".$itm_tcpd."]' id='checkbox' ".$checked."></td>";  
			$count++;     
		}
		?>
		</tr>
		<tr>
			<td colspan='10' align='right'>
				<input type='submit' name='add' id='add' value='Add TPCD'>
			</td>
		</tr>
	</table>
</form>
</div>
</body>
</html>

 

the output is something similar as in the attachment.

 

any helps are vy welcome and appreciated.

 

 

regards,

juz

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/249893-multiple-checkbox-db-selection-helps/
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.