ashrafzia Posted October 21, 2007 Share Posted October 21, 2007 How can i retrieve values from database table to a list box? I have tried something like this : $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ $progs = $row['programe_name']; $sems = $row['no_of_semesters']; $form = "<body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td><select name='programe'> <option value='$progs'>$progs</option> </select></td> </tr> <tr> <td>Semester:</td> <td><select name='semester'> <option value='$sems'>$sems</option> </select></td> </table></form>"; } Its working but only last record is showing from the table not all of them both in same programes and semester list box. Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/ Share on other sites More sharing options...
enoyhs Posted October 21, 2007 Share Posted October 21, 2007 Try this: <?php $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ $progs[] = $row['programe_name']; $sems[] = $row['no_of_semesters']; } $form = "<body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td><select name='programe'>"; foreach ($progs as $prog) echo "<option value='$prog'>$prog</option>"; echo "</select></td> </tr><tr> <td>Semester:</td> <td><select name='semester'>"; foreach ($sems as $sem) echo "<option value='$sem'>$sem</option>"; echo "</select></td> </table></form>"; ?> The problem with your code was: You repeated form many as many times as many records you had in database. I think you meant to repeat only option parts. Though that code looks really wrong with many errors so I'm not sure if it will even work... Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374927 Share on other sites More sharing options...
only one Posted October 21, 2007 Share Posted October 21, 2007 Try something like this: $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $add1 .= "<option value='$row[programe_name]'>$row[programe_name]</option>"; $add2 .= "<option value='$row[no_of_semesters]'>$row[no_of_semesters]</option>"; } $form = " <body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td> <select name='programe'> $add1 </select> </td> </tr> <tr> <td>Semester:</td> <td> <select name='semester'> $add2 </select> </td> </table> </form>"; Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374928 Share on other sites More sharing options...
chronister Posted October 21, 2007 Share Posted October 21, 2007 You have to get the <select> tag above the beginning of the while loop, and inside the while loop you have to have the <option> tags. enoyhs code looks like the best solution. You will need to set each item in an array and then loop through them inside the foreach. But the key thing is that the only thing inside the loop is the <option> tags. The way you had it it was repeating the <body> tag, and all the other tags. If you look at the html source upon loading the page it will look really screwed up Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374929 Share on other sites More sharing options...
ashrafzia Posted October 21, 2007 Author Share Posted October 21, 2007 Try this: <?php $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ $progs[] = $row['programe_name']; $sems[] = $row['no_of_semesters']; } $form = "<body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td><select name='programe'>"; foreach ($progs as $prog) echo "<option value='$prog'>$prog</option>"; echo "</select></td> </tr><tr> <td>Semester:</td> <td><select name='semester'>"; foreach ($sems as $sem) echo "<option value='$sem'>$sem</option>"; echo "</select></td> </table></form>"; ?> The problem with your code was: You repeated form many as many times as many records you had in database. I think you meant to repeat only option parts. Though that code looks really wrong with many errors so I'm not sure if it will even work... hm......i admit my fault. I was really repeating the form, which is a big mistake. i have tried your code but its only printing Array inside the listbox not the values. i don't know why? Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374938 Share on other sites More sharing options...
ashrafzia Posted October 21, 2007 Author Share Posted October 21, 2007 Try something like this: $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $add1 .= "<option value='$row[programe_name]'>$row[programe_name]</option>"; $add2 .= "<option value='$row[no_of_semesters]'>$row[no_of_semesters]</option>"; } $form = " <body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td> <select name='programe'> $add1 </select> </td> </tr> <tr> <td>Semester:</td> <td> <select name='semester'> $add2 </select> </td> </table> </form>"; Excellent logic! THis is what i want to do. Its working cool and fine enough to understand as well. Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374939 Share on other sites More sharing options...
ashrafzia Posted October 21, 2007 Author Share Posted October 21, 2007 Now lets make this a little complex..... I have got all my programe_names from my table inside a list box. Now i want whenever a progame_name is selected, the no of semesters for a programe which we are also retrieving from the table, let us say for the programe BBA the no of semesters are 8 which is stored inside a table. I want to show all the semester no's from 1,2,3.....8 inside the semester list box. How can i put this logic??? and remember whenever a different programe_name is selected, the no of semesters stored for it in the table should be retrieved and then a loop will be started from 1....upto that No which is retrieved and then those values will be printed inside the listbox. I want to apply all this logic on runtime. Any idea! THanx in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-374944 Share on other sites More sharing options...
enoyhs Posted October 22, 2007 Share Posted October 22, 2007 You will need to use either javascript with Ajax, already have predefined semesters stored in arrays or refresh page each time programe_name is selected. Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375377 Share on other sites More sharing options...
igor berger Posted October 22, 2007 Share Posted October 22, 2007 Sorry guys, this syntax is new to me, how is the $form echoed? Also what does period = means? $add1 .= Thank you. $sql = "SELECT * from programmes"; $result = mysql_query($sql, $conn) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $add1 .= "<option value='$row[programe_name]'>$row[programe_name]</option>"; $add2 .= "<option value='$row[no_of_semesters]'>$row[no_of_semesters]</option>"; } $form = " <body> <form action='studentregistrationform.php' method='post' enctype='multipart/form-data'> <td>Programe:</td> <td> <select name='programe'> $add1 </select> </td> </tr> <tr> <td>Semester:</td> <td> <select name='semester'> $add2 </select> </td> </table> </form>"; Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375389 Share on other sites More sharing options...
only one Posted October 22, 2007 Share Posted October 22, 2007 Sorry guys, this syntax is new to me, how is the $form echoed? Also what does period = means? $add1 .= Thank you. $add1 .= By using a . before the = you can extand the variable and too echo $form just echo "$form"; Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375403 Share on other sites More sharing options...
igor berger Posted October 22, 2007 Share Posted October 22, 2007 When you say extend you mean it ia added to the array of the variable? Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375406 Share on other sites More sharing options...
only one Posted October 22, 2007 Share Posted October 22, 2007 When you say extend you mean it ia added to the array of the variable? No, I'll give you an example: <?php $add1 = "This is the first bit in the variable"; $add1 .= "This will be added to the variable"; //Will output 'This is the first bit in the variableThis will be added to the variable' echo "$add1"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375412 Share on other sites More sharing options...
igor berger Posted October 22, 2007 Share Posted October 22, 2007 Okay got the logic. it joins bits in a variable. Thank you for the explanation. Always like to understand the logic, not just copy and paste... Quote Link to comment https://forums.phpfreaks.com/topic/74224-retrieving-values-from-database-table-to-list-box/#findComment-375429 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.