Jump to content

how to show the option based on the value in the table?


Recommended Posts

hi,

 

Instead giving its default on "Choose Process", I would like to display its process based on the value in database. How can I do it?

 

<?php 
require_once '../../connectDB.php';
require_once '../include/functions.php';

$sql = " SELECT *
	 FROM tb_item
	 ORDER BY itm_id asc";
$result = mysql_query($sql) or die(mysql_error());
?>
<form>
<h1>Items</h1>
<table width="75%" border="1" align="left" cellpadding="5" cellspacing="1">
	<tr> 
   		<th width="100" align="left">Item ID</th>
   		<th width="100" align="left">Parts</th>
   		<th width="100" align="left">Sub-Process</th>
   		<th width="100" align="left">Confirmation</th>
   	</tr> 	
   	<tr>	
   	<?php 
   	while ($row = mysql_fetch_array($result)) 
	{
	   	extract($row);
	?>
		<td><?php echo $itm_id; ?></td>
	    <td><?php echo $itm_parts; ?></td>
	    <td> 
			<select name='cboSub' id='cbosub'>
				<option value='' selected>---- Choose Process ----</option>	
			    <?php createComboBox(); ?>			  
			</select>
	 	</td>
		<td>
			<a href='processItem.php?itm_id=<?php echo $itm_id; ?>'>
				<img src='../images/modify.png' width='20' height='20' alt='confirm'/>
			</a>
		</td>
	</tr>
	<?php 
	}; 
	?>				

</table>
</form>
<?php 
/**
* Create combo box for sub-process
*/
function createComboBox() 
{
$sql = " SELECT *
 		 FROM tb_sub_process
    		 ORDER BY sub_id asc";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result)) 
{						
	extract($row);

	echo "<option value='$sub_id' $select>$sub_name</option>";
}
}
?>

 

I've attached an example of what basically the output will looks like.

 

Any help are appreciated.

 

Thanks in advance.

 

 

Regards,

Juz

 

[attachment deleted by admin]

Try this, and let us know:

<?php 
require_once '../../connectDB.php';
require_once '../include/functions.php';

$sql = " SELECT *
	 FROM tb_item
	 ORDER BY itm_id asc";
$result = mysql_query($sql) or die(mysql_error());
?>
<form>
<h1>Items</h1>
<table width="75%" border="1" align="left" cellpadding="5" cellspacing="1">
	<tr> 
   		<th width="100" align="left">Item ID</th>
   		<th width="100" align="left">Parts</th>
   		<th width="100" align="left">Sub-Process</th>
   		<th width="100" align="left">Confirmation</th>
   	</tr> 	
   	<tr>	
   	<?php 
   	while ($row = mysql_fetch_array($result)) 
	{
	   	extract($row);
	?>
		<td><?php echo $itm_id; ?></td>
	    <td><?php echo $itm_parts; ?></td>
	    <td> 
			<select name='cboSub' id='cbosub'>
				<option value='' selected>---- Choose Process ----</option>	
			    <?php createComboBox($sub_id); ?>			  
			</select>
	 	</td>
		<td>
			<a href='processItem.php?itm_id=<?php echo $itm_id; ?>'>
				<img src='../images/modify.png' width='20' height='20' alt='confirm'/>
			</a>
		</td>
	</tr>
	<?php 
	}; 
	?>				

</table>
</form>
<?php 
/**
* Create combo box for sub-process
*/
function createComboBox($id) 
{
$sql = " SELECT *
 		 FROM tb_sub_process
    		 ORDER BY sub_id asc";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result)) 
{						
	extract($row);
	$select = ($id == $sub_id) ? 'selected="selected"' : NULL;
	echo "<option value='$sub_id' $select>$sub_name</option>";
}
}
?>

thanx a lot jcbones...it works...

 

I'm facing another problem. it wouldn't update into the database when i click on the confirmation link.

 

it might be $_POST['cboSub'], cause i m getting a null value. what can i do to fix it right?

 

here's my code on processItem.php:

<?php
$itm_id = isset($_GET['itm_id']) ? $_GET['itm_id'] : '';

$sub_id=$_POST['sub_id'];
echo $sub_id;
$sql = "UPDATE tb_item 
        SET sub_id = '$sub_id'
        WHERE itm_id = '$itm_id'";
           
$result = mysql_query($sql) or die('Cannot update sub-process ID into tb_item. ' . mysql_error());
?>

ermm...maybe i m not clear here.

 

here goes again.

 

1) the user will choose an option.

2) update to database by clicking a confirmation link next to it.

3) continue with other items with step 1&2.

 

codes in list.php:

<?php 
require_once '../../connectDB.php';
require_once '../include/functions.php';

$sql = " SELECT *
	 FROM tb_item
	 ORDER BY itm_id asc";
$result = mysql_query($sql) or die(mysql_error());
?>
<form>
<h1>Items</h1>
<table width="75%" border="1" align="left" cellpadding="5" cellspacing="1">
	<tr> 
   		<th width="100" align="left">Item ID</th>
   		<th width="100" align="left">Parts</th>
   		<th width="100" align="left">Sub-Process</th>
   		<th width="100" align="left">Confirmation</th>
   	</tr> 	
   	<tr>	
   	<?php 
   	while ($row = mysql_fetch_array($result)) 
	{
	   	extract($row);
	?>
		<td><?php echo $itm_id; ?></td>
	    <td><?php echo $itm_parts; ?></td>
	    <td> 
			<select name='cboSub' id='cbosub'>
				<option value='' selected>---- Choose Process ----</option>	
			    <?php createComboBox($sub_id); ?>			  
			</select>
	 	</td>
		<td>
			<a href='processItem.php?itm_id=<?php echo $itm_id; ?>'>
				<img src='../images/modify.png' width='20' height='20' alt='confirm'/>
			</a>
		</td>
	</tr>
	<?php 
	}; 
	?>				

</table>
</form>
<?php 
/**
* Create combo box for sub-process
*/
function createComboBox($id) 
{
$sql = " SELECT *
 		 FROM tb_sub_process
    		 ORDER BY sub_id asc";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result)) 
{						
	extract($row);
	$select = ($id == $sub_id) ? 'selected="selected"' : NULL;
	echo "<option value='$sub_id' $select>$sub_name</option>";
}
}
?>

 

Codes in processItem.php:

<?php
$itm_id = isset($_GET['itm_id']) ? $_GET['itm_id'] : '';

$sub_id=$_POST['cboSub'];
$sql = "UPDATE tb_item 
        SET sub_id = '$sub_id'
        WHERE itm_id = '$itm_id'";
           
$result = mysql_query($sql) or die('Cannot update sub-process ID into tb_item. ' . mysql_error());
?>

 

how can I link these two sets of code together correctly?

 

 

See if this works.  Un-tested:

<?php 
require_once '../../connectDB.php';
require_once '../include/functions.php';
if(isset($_POST['change'])) { 
$itm_keys = array_keys($_POST['change']);
foreach($itm_keys as $id) {
	if(!empty($_POST['cboSub'][$id])) {
		$sub_id = (int) $_POST['cboSub'][$id];
			$sql = "UPDATE tb_item SET sub_id = '$sub_id' WHERE itm_id = '$id'";
			mysql_query($sql) or trigger_error($sql . ' has an error: ' . mysql_error());
	}
}
}
$sql = " SELECT *
	 FROM tb_item
	 ORDER BY itm_id asc";
$result = mysql_query($sql) or die(mysql_error());
?>
<form>
<h1>Items</h1>
<table width="75%" border="1" align="left" cellpadding="5" cellspacing="1">
	<tr> 
   		<th width="100" align="left">Item ID</th>
   		<th width="100" align="left">Parts</th>
   		<th width="100" align="left">Sub-Process</th>
   		<th width="100" align="left">Confirmation</th>
   	</tr> 	
   	<tr>	
   	<?php 
   	while ($row = mysql_fetch_array($result)) 
	{
	   	extract($row);
	?>
		<td><?php echo $itm_id; ?></td>
	    <td><?php echo $itm_parts; ?></td>
	    <td> 
			<select name='cboSub[<?php echo $itm_id; ?>]' id='cbosub'>
				<option value='' selected>---- Choose Process ----</option>	
			    <?php createComboBox($sub_id); ?>			  
			</select>
	 	</td>
		<td>
			<input type="submit" name="change[<?php echo $itm_id; ?>]" value="Modify" />
			</a>
		</td>
	</tr>
	<?php 
	}; 
	?>				

</table>
</form>
<?php 
/**
* Create combo box for sub-process
*/
function createComboBox($id) 
{
$sql = " SELECT *
 		 FROM tb_sub_process
    		 ORDER BY sub_id asc";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result)) 
{						
	extract($row);
	$select = ($id == $sub_id) ? 'selected="selected"' : NULL;
	echo "<option value='$sub_id' $select>$sub_name</option>";
}
}
?>

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.