Jump to content

Retrieving values from database table to list box


ashrafzia
 Share

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.