Jump to content

Retrieve Data from DB to Drop down box


chrishutagalung

Recommended Posts

 

If you were to look at the HTML source code that is created from the above it would become obvious what the problem is. The options are getting created something like this

<option value="Bob Smith>"Bob Smith</option>

Note the closing double quote and '>' at the end of the opening OPTION tag. This is why I use double quotes to define my strings and put the variables inside the string instead of concatenating them - it makes it easier to see what the output would be.

 

Also, you should be using the employee_id as the VALUE of the options and the name as the label/text. Plus, I don't know why you would order the values based on the ID since that would not be of any value to the user. Lastly, it's best to put the logic (i.e. PHP at the top of the page [or in a separate file]) and then only output the results in the HTML code. Makes it much easier to maintain.

<?php

## All of this code should be in the top of the script, not in the output of the page.

// Connect to the db.
require ('mysqli_connect.php');
// Create and run the query:
$query = "SELECT employee_id, employee_name FROM employee WHERE dept_id=3 ORDER BY employee_name ASC";
$result = mysqli_query ($dbc, $query);

if(!$result)
{
    //Query failed - add some error handling here
}
else
{
    //Create the options for the select list
    $staffOptions = '';
    while ($row = mysqli_fetch_array($result))
    {
        $staffOptions .= "<option value='{$row['employee_id']}'>{$row['employee_name']}</option>\n";
    }
}

mysqli_free_result ($r); // Free up the resources.
mysqli_close($dbc); // Close the database connection.
?>

<!-- this goes down in the HTML code for your page -->
<select name="employee_name">
<?php echo $staffOptions; ?>
</select>

Hi What i have is this.. what am i doing wrong here ?? Im not getting any output from the dropdown list

// Assume its connected to the DB

<?php
    $paint_parse = oci_parse($conn, 'SELECT HEAD_MARK FROM FABRICATION');
    oci_execute($paint_parse, OCI_DEFAULT);
    
    // Count the number of occurence inside the table
    $result = array();
    $numrows = oci_fetch_all($paint_parse, $result, null, null, OCI_FETCHSTATEMENT_BY_ROW);
    
    $value = '';
    
    while ($row = oci_fetch_array($paint_parse)!= false){
        $value .= "<option value = '{$row['HEAD_MARK']}'>{$row['HEAD_MARK']}</option>\n";
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>
            PT.Weltes Energi Nusantara
        </title>
        <header>
            
        </header>
    </head>
    <body>
        <select>
            <?php echo $value; ?>
            
        </select>
        
            <?php echo $numrows; ?>
    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/287268-retrieve-data-from-db-to-drop-down-box/
Share on other sites

I have no idea. I am unfamiliar with the oci_ functions and what would be expected as return values. Have you added code to debug the problem and, if so, what were the results? For example, are you getting a value displayed for $numrows? Did you look at the HTML source code to see if any of the code for the options is there?

 

I'm not 100% certain, but I think this may be invalid code

 

while ($row = oci_fetch_array($paint_parse)!= false)

 

I would wrap the assignment portion within parens to ensure the order of operations is as I would expect.

 

while (($row = oci_fetch_array($paint_parse))!= false)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.