Page 1 of 1
Retrieving values from database table to list box
Posted: Sun Oct 21, 2007 12:54 pm
by ashrafzia
How can i retrieve values from database table to a list box?
I have tried something like this :
Code: Select all
$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.
problem (among others): $form is being overwritten
Posted: Sun Oct 21, 2007 4:12 pm
by gregsometimes
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Of course it is only giving you the last row. Because this is what you're asking it to do. So what's the problem here? Well, your code logic is a bit faulty here. You keep assigning a value to the same variable $form within the while loop. So what happens here is the $form var keeps getting overwritten again and again, until your while loop reaches the last row. (Surely enough, last time something is written to $form, it is the last row entry).
This is still not the best way to do this, but I'm working with your code logic, so
I propose following code:
Code: Select all
<?php // start php
<!--
a proper way to populate the select menu with rows //-->
$sql = "SELECT * from programmes";
$result = mysql_query($sql, $conn) or die(mysql_error());
$sp = "<select name='programe'>";
$ss = "<select name='semester'>";
while ($row = mysql_fetch_array($result)){
$progs = $row['programe_name'];
$sems = $row['no_of_semesters'];
$sp .= "<option value='$progs'>$progs</option>";
$ss .= "<option value='$sems'>$sems</option>";
} $sp .= "</select>";
$ss .= "</select>";
$form = "<form action='studentregistrationform.php' method='post' enctype='multipart/form-data'><table><tr><td>Programe:</td><td>".$sp."</td></tr><tr><td>Semester:</td> <td>".$ss."</td></tr></table></form>";
// end php and start html ?>
<!--
I'm not sure why you had a <body> tag in $form, but also had a closing </table> tag, and a missing </tr> tag.
I could be wrong but I assume you meant <table> instead of <body>
So, now use $form anywhere you need it, example: //-->
<html>
<body>
<?php print $form; ?>
</body>
</html>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Re: problem (among others): $form is being overwritten
Posted: Mon Oct 22, 2007 12:11 am
by ashrafzia
Bingo! its working!
I admit my fault, i was really repeating form inside the while loop which is a big mistake.
I am amazed to see how things work in php like this.
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.
Posted: Mon Oct 22, 2007 12:39 am
by gregsometimes
I don't clearly understand what you are trying to accomplish, but it seems like you may need to learn AJAX.
There isn't a simple way to do "real time" MySQL functionality (without refreshing the page)