Problem with displaying the correct Result

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

Problem with displaying the correct Result

Post by ashrafzia »

I am getting a value from the table of the field no_of_semester.
suppose the no_of_semester for the programe BCS are 8.

Now i have to put the no_of_semester value in a listbox but the no_of_semester will start from 1,2,3.....8, which will be defenetly done with the help of a loop.

I have the following logic applied but doesn't seems to work.

Code: Select all

<?php
include "connection.php";
$prog_name = $_POST['first'];


$sql = "SELECT	programmes.no_of_semesters
		FROM programmes
		WHERE	programmes.programe_name = '$prog_name' ";
		
$result = mysql_query($sql, $conn) or die (mysql_error());

	while ($row = mysql_fetch_array($result)){
		$sem = $row['no_of_semesters'];
	}

		$select =  "<select id=\"semester\" name=\"semester\">
			<option value=\"\">--Select--</option>";

	for ($i=1;$i<=$sem;$i++)
	{
		$add .= "<option value='$sem[$i]'>$sem[$i]</option>";
		echo "$add";	
	}
		$select .="</select>";
		echo "$select";

?>
Any idea!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's the problem?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I think you may want something like:

Code: Select all

echo '<select name="semester">';
   while ($row = mysql_fetch_assoc($result)){
      for ($i=1;$i<=8;$i++){
         echo '<option value="'.$i.'"';
         if($row['no_of_semesters']==$i) echo ' selected="selected"';
         echo '>'.$i.'</option>';
      }
   }
echo '</select>';
MySQL_fetch_array() returns a numeric array, whereas MySQL_fetch_assoc() returns an associative array.
Post Reply