Reputation: 55
Why does my dropdown list seperate in the table to diff columns. Example I want 3 options to be displayed in the dropdown list. These are data from my database.
Eg: company a, company b
to be displayed on the drop down list.
Instead now only one option (company a)
is available on the drop list and (company b)
is displayed on the next row of the table instead of together on a single drop down list.
<?
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details;");
$row2 = mysqli_fetch_assoc($result2);
while($row = mysqli_fetch_assoc($result))
{
while ($row2 = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'><option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option></td>";
echo "</tr>";
}
}
echo "</table>";
?>
</form>
Upvotes: 0
Views: 101
Reputation: 22721
Try this, You have missed to close </select>
tag, I have rewritten the code i.e moved <option>
tag generation above table generation section. So that to avoid unneccessary looping.
<?php
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details");
$row2 = mysqli_fetch_assoc($result2);
/*options sections start*/
$options= '';
while ($row2 = mysqli_fetch_array($result2))
{
$options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
}
/*options sections end*/
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>".$options."</select></td>";
echo "</tr>";
}
echo "</table>";
?>
Upvotes: 1
Reputation: 1011
Try this:
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>";
while ($row2 = mysqli_fetch_array($result2))
{
echo "<option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option>";
}
echo "</select></td>";
echo "</tr>";
}
Upvotes: 1