I am trying to fetch data from the WordPress database table using wpdb
and loop it using foreach statement so as to output results in select tag
. I created a custom WordPress template and this was to enable me to utilize the wpdb
function. This query $worktype =$wpdb->get_results( "SELECT * FROM cww_avid_worktype;", OBJECT );
works fine when dump using print_r($worktype );
, its return array results from the table hence means connection to the database is ok.
And the statement below works fine in pure PHP.
ISSUE
When I loop $worktype
using foreach it does not output any results or any error and I have turned on debug in wp-config.
Foreach Loop
<select name="worktype" id="worktype" class="form-control required" >
<?php
global $wpdb;
$worktype =$wpdb->get_results( "SELECT * FROM cww_avid_worktype;", OBJECT );
foreach ($worktype as $val) {
if ($val->idworkType == $post['worktype']) {
echo '<option selected="selected" id="' . $val->idworkType . 'wt" value="' . $val->idworkType . '" title="' . $val->amount_added . '">' . $val->name . '</option>';
}
else {
echo '<option id="' . $val->idworkType . 'wt" value="' . $val->idworkType . '" title="' . $val->amount_added . '">' . $val->name . '</option>';
}
}
?>
</select>