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.