tayys Posted October 17, 2011 Share Posted October 17, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/ Share on other sites More sharing options...
Buddski Posted October 17, 2011 Share Posted October 17, 2011 Is the process value stored in the tb_item table? Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1279889 Share on other sites More sharing options...
tayys Posted October 18, 2011 Author Share Posted October 18, 2011 yup...it's in one of the table Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280086 Share on other sites More sharing options...
jcbones Posted October 18, 2011 Share Posted October 18, 2011 Throw us a bone here, where is it located, and what is the column name. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280087 Share on other sites More sharing options...
tayys Posted October 18, 2011 Author Share Posted October 18, 2011 What I am trying to do is, initially it is set default - "Choose Process". After selecting and saved, it will display the option been selected on the next login. jcbones, the sub_id is stored in tb_item. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280090 Share on other sites More sharing options...
tayys Posted October 18, 2011 Author Share Posted October 18, 2011 here's my tables: *****tb_item***** itm_id | itm_qty | itm_cost | sub_id *****tb_process***** pro_id | pro_name *****tb_sub_process***** sub_id | sub_name | pro_name Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280092 Share on other sites More sharing options...
jcbones Posted October 18, 2011 Share Posted October 18, 2011 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280094 Share on other sites More sharing options...
tayys Posted October 18, 2011 Author Share Posted October 18, 2011 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280118 Share on other sites More sharing options...
jcbones Posted October 18, 2011 Share Posted October 18, 2011 Does the page source reveal that the sub_id is set in the select box? Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280375 Share on other sites More sharing options...
tayys Posted October 19, 2011 Author Share Posted October 19, 2011 Sorry my mistake. It should be $sub_id=$_POST['cboSub']; But it still doesn't pass the selected value from the earlier page so that It can be updated into database based on the itm_id. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280394 Share on other sites More sharing options...
jcbones Posted October 20, 2011 Share Posted October 20, 2011 What 'earlier page'? Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280678 Share on other sites More sharing options...
tayys Posted October 20, 2011 Author Share Posted October 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280695 Share on other sites More sharing options...
jcbones Posted October 20, 2011 Share Posted October 20, 2011 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1280944 Share on other sites More sharing options...
tayys Posted October 21, 2011 Author Share Posted October 21, 2011 Thanks for the response. I'll give it a try and let you know how it turns out! Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281029 Share on other sites More sharing options...
tayys Posted October 21, 2011 Author Share Posted October 21, 2011 I tried the codes but it is shows the error message as in the attachment. I never encounter this seen this error message before. Any idea what's the problem is? Thanx. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281069 Share on other sites More sharing options...
jcbones Posted October 21, 2011 Share Posted October 21, 2011 Sorry, I didn't even pay attention to the form element. Change: <form> To: <form action="" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281170 Share on other sites More sharing options...
tayys Posted October 24, 2011 Author Share Posted October 24, 2011 jcbones, thanks dude. it's working perfectly now. despite, there is a statement i can't get myself clear. Can u please briefly explain these? $itm_keys = array_keys($_POST['change']); Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281697 Share on other sites More sharing options...
jcbones Posted October 24, 2011 Share Posted October 24, 2011 array_keys just returns the keys of the array that is passed to it. This is done so that we can find what items were sent in the post field. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281910 Share on other sites More sharing options...
tayys Posted October 25, 2011 Author Share Posted October 25, 2011 noted. thank you very much for the guides and suggestions. I learnt a lot from you. *I would like to lift another issue regarding 2 chained combo boxes in jquery. r u able to give me some advises on that? Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1281972 Share on other sites More sharing options...
jcbones Posted October 25, 2011 Share Posted October 25, 2011 Just start another thread, so that anyone looking at this one will not confuse the two different issues. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1282201 Share on other sites More sharing options...
tayys Posted October 26, 2011 Author Share Posted October 26, 2011 sure. I've created under the forum of javascript helps sometime last week. Here's the link to the new thread: http://www.phpfreaks.com/forums/index.php?topic=346236.0 any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/249249-how-to-show-the-option-based-on-the-value-in-the-table/#findComment-1282302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.