Page 1 of 1

Problem with displaying the correct Result

Posted: Wed Oct 24, 2007 11:40 pm
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!!

Posted: Wed Oct 24, 2007 11:53 pm
by feyd
What's the problem?

Posted: Wed Oct 24, 2007 11:54 pm
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.