0

I'm trying to create a form in html which has a combobox with a list that's automatic created from my SQL table. I've managed to make an integrated combobox in my class where I use all my database statements. But how do I use the code on another location where my form is written? I'm still learning php and this is kinda new for me so I hope my question is clear to understand.

Code:

    class Lesdb
    {
        private static $lesdbInstantie = null;

        private $dbh;

        private function __construct($server, $username, $password, $database)
        {
            try
            {
                $this->dbh = new PDO("mysql:host=$server; dbname=$database", $username, $password);
                //Bij error: exception opwerpen
                $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch (PDOException $e)
            {
                die($e->getMessage());
            }
        }

        public static function getLesdbInstantie($server, $username, $password, $database)
        {
            if(is_null(self::$lesdbInstantie))
            {
                self::$lesdbInstantie = new Lesdb($server, $username, $password, $database);
            }
            return self::$lesdbInstantie;
        }
    public function testComboBox()
    {
        echo "<h1> ComboBox functienamen </h1>";

            $sql = "SELECT DISTINCT ftienaam FROM werknemers";
            $stmt = $this->dbh->prepare($sql);
            $stmt->execute();

        $dropdown = "<select name='ftienaam'>";
        foreach ($stmt as $row)
        {
            $dropdown .= "\r\n<option value='{$row['ftienaam']}'>{$row['ftienaam']}</option>";
        }
        $dropdown .= "\r\n</select>";

        echo $dropdown;

    }
}

?>

The function TestCombobox does work but it should be implemented into my form which isn't located in the class Lesdb. example :

function ShowForm()
{
    ?>
    <form action="Index.php?actie=zoekInLijst" method=post>
        <label for=ComboBox>ComboBox</label>
            <select name = "the_name">
                <option value="name">SQL STATEMENTS
            </select>
    </form>
<?php
}

So I simply want to call the function testComboBox from the class Lesdb in my form.

2 Answers 2

0

You have to make an new instance of the class. Then on that instance you have to load the function.

Don't forget to include the class in top of the file.

$theLesDB = new Lesdb();

$theLesDB->testComboBox();
Sign up to request clarification or add additional context in comments.

6 Comments

( ! ) Fatal error: Call to private Lesdb::__construct() from invalid context in C:\wamp\www\PHP\Oefenreeksen\OefenReeks 4 PDOZoekFunctie\ToonFuncties.php on line 98 <?php $db = new Lesdb(); $db->testComboBox(); ?>
The constuctor of the class is private.. When you try to create an instance of it, the constuctor will always be loaded first. Make it public.
__construct($server, $username, $password, $database) --> It needs params
Any tutorials about this chapter available ? I thought the params were automaticly generated since it gets generated in the class Lesdb?
Just declare the variables $server, $username, $password, $database in the class.. I don't think you will work with multiple databases at the moment. You are trying too much things in one time. I would split the code. First of all, just make a dbClass that makes a connection on the first construction, and returns it. You can do that with a static function: read this: stackoverflow.com/questions/20732897/php-oop-database-connect Then you can load that class and uses it in other classes (like werknemers).
|
0

Assuming your class is in one file, let's call it Lesdb.php and your form is in another file, e.g. Form.php, then you have to include your class in the form file (right at the top, before the other code),

include_once "<PATH_TO_YOUR_FILE>\Lesdb.php";

create an instance of your class (where you want to use that instance),

Lesdb db = new Lesdb();

and use the function in your ShowForm function.

db->testComboBox();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.