How would I add to a dropmenu using php?

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Tue Sep 05, 2017 10:46 am

Hello.

I'm looking to add items from a array to a dropmenu, however I'm only able to add the last item in the array.

What I'm trying to do is:-
1st, Load and display data from one database field, and display it on a web page.
2nd Write the data to an array for later use.
I'm able to achieve the above ok.

3rd Further down my web page, run the array, and place the array contense into a drop menu, however I can only get it to display the last entry, instead of all of the array.

Code: Select all

try {
                                    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                                    // set the PDO error mode to exception
                                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                    $results = $conn->query('SELECT * FROM central_site');
                                    while ($row = $results->fetch()) {
                                        print "<tr> <td width='5%'>";
                                            echo $row['site_url'];
                                        print "</td> <td width='5%'>";
                                            echo $row['site_header'];
                                        print "</td> <td width='5%' align='center'>";
                                            echo $row['status'];
                                        print "</td> <td width='5%' align='center'>";
//echo "<select name=$row['_id'] size='1'>";
                                            echo "<select name='com_x' size='1'>";
                                            echo "<option selected='selected'>Select</option>";
                                            echo "<option>Edit</option>";
                                            echo "<option>Pause</option>";
                                            echo "<option>Delete</option>";
                                            echo "</select>";
                                        print "</td></tr>";
                                        //$mysite[] = array($row['site_url']);
                                        $mysite = array($row['site_url']);  // Try this
                                        //$mysite[] = $row['site_url'];
                                        $domain_end++;

                                        //$drows[] = $row['site_url'];
                                }
                                $conn = null; // Close Connection
                                }catch(PDOException  $e ){ }
                                ?> 
The above code needs tidying up but It works.

Code: Select all

<select name="comdomain" size="1">
                                                        <option selected="selected">Select</option>
                                                    <?
                                                        $x=0;
                                                        // Working progress
                                                        for ($x = 0; $x <= $domain_end; $x++) {
                                                            echo "<option>" .  $mysite[$x] . "</option>";
                                                       //print "<option>" .  $x . "</option>";
                                                        //print "<option>" .  $mysite[$x] . "<br></option>";
                                                        }
                                                    ?>
                                                    </select>
The code above only adds the last entry of the array and not all the array.
Have I gone wrong, or how do I add the whole array to the drop menu please?

Thank You.

IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Tue Sep 05, 2017 2:26 pm

Hello Again!

Great News.
I've found my old notes, and managed to create a drop down menu in php and PDO. :)

Code: Select all

<select name="comdomain" size="1">
    <option selected="selected">Select</option>
     <?
      try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $results = $conn->query('SELECT * FROM my_site');
            while ($row = $results->fetch()) {
                echo "<option>" . $row['my_url'] . "</option>";
             }
            $conn = null; // Close Connection
            }catch(PDOException  $e ){ }
        ?>
</select>
Note: Not shown above, you'll also need to include :
$servername ="";
$username = "";
$password = "";
$dbname = "";

Kind Regards :)

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Thu Sep 14, 2017 2:35 pm

Here's how I go about doing something like this. I first try to keep the PDO and the HTML separated as much as possible.

First I grab all the records that I want by calling function:

Code: Select all

<?php

use Library\Database\Database as DB;

function getUserInfo() {
    $db = DB::getInstance();
    $pdo = $db->getConnection(); // I am assuming you know how to write a PDO connection string:
    $query = 'SELECT id, username, full_name, security_level, private FROM users';
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $data;
}
Then I call it from the top of the page that I want to use the Select element.

Code: Select all

$names = getUserInfo(); // Get ids and names of bloggers:
Then I insert it in the HTML portion like such:

Code: Select all

        <?php
        echo '<form action = "' . $basename . '" method = "post">' . "\n";
        echo '<select name = "user_id">';
        foreach ($names as $name) {
            echo '<option value="' . $name['id'] . '">' . $name['full_name'] . '</option>' . "\n";
        }
        echo '<input type = "submit" name = "submit" value = "submit">' . "\n";
        echo "</select>\n";
        echo "</form>\n";
        ?>
I stopped using try..catch for the most part after reading info on this website on PDO -> https://phpdelusions.net/pdo

as long as you do something like this

Code: Select all

        $db_options = array(
            /* important! use actual prepared statements (default: emulate prepared statements) */
            PDO::ATTR_EMULATE_PREPARES => false
            /* throw exceptions on errors (default: stay silent) */
            , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            /* fetch associative arrays (default: mixed arrays)    */
            , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        );
you should be fine.
Life is a fig newton of your imagination! https://www.pepster.com/index

Post Reply