Retrieving values from database table to list box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Retrieving values from database table to list box

Post 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.
User avatar
gregsometimes
Forum Newbie
Posts: 11
Joined: Sun Oct 21, 2007 1:57 pm
Location: San Francisco, CA

problem (among others): $form is being overwritten

Post by gregsometimes »

feyd | Please use

Code: Select all

,

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

,

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]
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Re: problem (among others): $form is being overwritten

Post 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. :D

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.
User avatar
gregsometimes
Forum Newbie
Posts: 11
Joined: Sun Oct 21, 2007 1:57 pm
Location: San Francisco, CA

Post 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)
Post Reply