i am trying to populate html select option with values fetched from database using TWIG templating. i tried everything which came across me in google but i failed to get the solution so far i did in. controller: index.php
require_once 'includes/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('./');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('components/epayroll/new/empView.xhtml');
require_once 'components/epayroll/new/empClass.php';
$objEmp = new Employee();
$data=$objEmp::getAllCourt();
//print_r($data);
echo $template->render(array (
'data' => $data,
'msg' => 'courts not found',
));
in modal section: empClass.php
class Employee {
public function getAllCourt()
{
require_once 'components/konec2DB.php';
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$getcourt = "select ddo_court from cms_ddo";
$sql = "select ddo_court from cms_ddo";
$sth = $DBH->query($sql);
$data = array();
while ($row = $sth->fetchObject()){
$data[] = $row;
}
return $data;
}
}
in view section of TWIG: empView.xhtml
<td class='input'> <select id="slctcourt"> <option value="">--Select--</option> {% for court in data %} <option value=" ">{{ court }}</option> {% else %} <option value="">{{ msg }}</option> {% endfor %} </select></td>
please help me out i am trying from last four days to get it solved. so far i have tried
'data' => (string)$data,//casting
then i tried
'data' => $data->ddo_court,//this shows msg "courts not found"
then i tried
'data' => $data[0], // this populates select option box with value 'Array'
some where i found to implement the iterator interface to iterate the object and that is beyond my skills.
please help me out