简体   繁体   中英

Populating Dropdown box from mysql using php

Student looking for help.

Trying to populate a dropdown menu from a mysql database, no data being showed in dropdown. Using getCategory function to get data from tables categories . Database is connecting and the particular table is being accessed through another function. I'm lost on this one, been looking and googling for the answer for last number of nights, still no luck. Maybe I've been looking at it too long that my brains fried?

Function Code

public function getCategory() 
{
echo $query = "SELECT * FROM `categories`";
$results = mysqli_query($this->_con, $query)  or die(mysqli_error()); 
$categories = array();
foreach ($results as $result){
$categories[$result['id']] = $result['category_name'];
}
mysqli_close($this->_con);


return $categories;
           echo "<pre>"; print_r($categories); echo "</pre>";
}

Dropdown Code

<select class="form-control" name="category" id="category">
<option value="">Choose your category</option>
<?php  foreach ($categories as $key=>$category){ ?>
<option value="<?php echo $key; ?>"><?php echo $category; ?></option>
<?php } ?>
</select> 

Table called categories

Columns are id & category_name

 please try the below code in your code in your function

$nums=mysql_num_rows($results);

for($i=0;$i<$nums;$i++)
{
    $id=mysql_result($results,$i,'id');
    $category=mysql_result($results,$i,'category_name');

$categories[$id] = $category;
}

$categories =getCategory() ;

I am not sure what you are struggling with, your code is missing a function call to getCategory (should be called getCategories because you are getting plural of objects, not one. Just a sidenote).

Following code is tested and works (save as index.php in your documentRoot to test), please be aware that I cannot declare a function as public when not in a class context, also I could not use $this->_con without a class with _con var, so I used $con instead. You need to adapt it to your context and needs:

index.php:

<?php
$category = filter_input(INPUT_POST,'category');
if($category!=FALSE && $category!=NULL){
    $selectedCategory = $category;
}

function getCategory() {
    $con = mysqli_connect("localhost","root","","categories") or die("Error " . mysqli_error($con)); 
    echo $query = "SELECT * FROM `categories`";
    $results = mysqli_query($con, $query)  or die(mysqli_error()); 
    $categories = array();
    foreach ($results as $result){
        $categories[$result['id']] = $result['category_name'];
    }
    mysqli_close($con);

    return $categories;
}
?>

<html>
<head>
<title>Categories Test</title>
</head>
<body>
<?php
if(isset($selectedCategory)){
    echo "last selected category ID: ".$selectedCategory;
}
?>
<form action="index.php" method="POST">
    <select class="form-control" name="category" id="category">
        <option value="">Choose your category</option>
        <?php
        $categories = getCategory();  
        foreach ($categories as $key=>$category): ?>
            <option value="<?php echo $key; ?>"><?php echo $category; ?></option>
        <?php endforeach; ?>
    </select> 

    <input type="submit" value="send" />
</form>

</body>

Here is the example i did it in this way. Hope! it will helps you.

public function GetStates()
{
    $tempgetstates = mysql_query("SELECT * FROM `states`") or die(mysql_error());
    echo '<select name="state" class="sel_reg_form" required />';
    echo '<option value="">...Select...</option>';
    while($getstates = mysql_fetch_object($tempgetstates))
    {
        echo '<option value="'.$getstates->state_code.'">'.$getstates->state.'</option>';
    }
    echo '</select>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM