Njonjo Kiroga Njonjo Kiroga - 6 months ago 120
SQL Question

PHP, MySql drop down list only showing one record per selection

I am newbie to this and I am trying to create a dynamic drop down list that retrieves data from a database. The problem is, it only gives me one drop down item(on second list) per every selection on the first drop down. Somebody please help.
Here is the code.

<?php
require_once("dbcontroller.php");

$query ="SELECT * FROM campus";

?>
<html>
<head>
<TITLE>Campus and Faculty Select</TITLE>
<head>
<style>
body{width:610px;}
.frmDronpDown {border: 1px solid #F0F0F0;background-color:#C8EEFD;margin: 2px 0px;padding:40px;}
.demoInputBox {padding: 10px;border: #F0F0F0 1px solid;border-radius: 4px;background-color: #FFF;width: 50%;}
.row{padding-bottom:15px;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function getcampus_id(val) {
$.ajax({
type: "POST",
url: "get_faculty.php",
data:'campus_id='+val,
success: function(data){
$("#faculty-list").html(data);
}
});
}

function selectcampus_id(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<div class="frmDronpDown">
<div class="row">
<label>Country:</label><br/>
<select name="campus" id="campus-list" class="demoInputBox" onChange="getcampus_id(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM campus";
$results = mysqli_query($con, $query);
//loop
foreach ($results as $campus){
?>
<option value="<?php echo $campus["campus_id"]; ?>"> <?php echo $campus["name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>State:</label><br/>
<select name="faculty" id="faculty-list" class="demoInputBox">
<option value="">Select State</option>
</select>
</div>
</div>
</body>
</html>


The get_faculty.php

<?php
require_once("dbcontroller.php");

if(!empty($_POST["campus_id"])) {
$campus_id = $_POST["campus_id"];
$query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
$results = mysqli_query($con, $query);
?>
<option value="">Select Campus</option>
<?php
foreach($results as $faculty) {
?>
<option value="<?php echo $faculty["faculty_id"]; ?>"><?php echo $faculty["faculty_name"]; ?></option>
<?php
}
}
?>


and the dbcontroller.php

<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "registration";

$con = mysqli_connect($host, $username, $password) or die("Could not Connect");
mysqli_select_db($con, $dbname);
?>

Answer

foreach processes an array! The $result variable is not an array its a mysqli_result object

So you need to get each result row and then use that rows data. For this use a while loop.

Its also useful when you are coding PHP and HTML like this to use the

while () :
   . . .
endwhile;

syntax, as it makes it easier to see where loops etc start and finish. Otherwise you can get very lost in this sort of code.

<?php

error_reporting(E_ALL); 
ini_set('display_errors', 1);

require_once("dbcontroller.php");

if(!empty($_POST["campus_id"])) {
    $campus_id = $_POST["campus_id"];
    $query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
    $result = mysqli_query($con, $query);

    if ( $result === false ) {
        echo mysqli_error($con);
        exit;
    }

    echo '<option value="">Select Campus</option>';

    while ( $faculty = mysqli_fetch_assoc($result) ) :
        echo '<option value="' . $faculty['faculty_id'] . '">';
        echo $faculty['faculty_name']; 
        echo '</option>';
    endwhile;
}
?>

Of course I am assuming thatthe code you dod not show has the required <select....> and </select> tags before and after this code

Dont see anything wrong in dbcontroller but I would do

<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "registration";

$link = mysqli_connect($host, $username, $password, $dbname);

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

As mysqli_select_db(); is really for switching to a second database once a first database waqs successful connectioned to

Comments