duanpan7011
2018-10-23 15:18
浏览 63
已采纳

PHP MySQL / WordPress功能/忍者表格

I am trying to populate a drop down list of supplier names with a WordPress plugin "Ninja Forms" using a query to a external database connection and custom code in functions.php.

The drop down list on the form appears to be populating with the correct quantity of results (i.e. 800+ items) but instead showing [object Object] instead of the suppliers name.

The code below is where I got up to, any support on adding the queried supplier name (SUP_Name) instead of [object Object] would be of great use.

add_filter('ninja_forms_render_options','my_pre_population_callback', 10, 2);
function my_pre_population_callback($options, $settings) {

if( $settings['key'] == 'supplier_list' ) {

$connection = mysqli_connect( "IP:port", "username", "password", "db" );
if (!$connection) {  die('Not connected : ' . mysqli_error());}

$db_selected = mysqli_select_db($connection,"db");
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error());
}

$query = "SELECT SUP_Name FROM db.sup_supplier";
$results = mysqli_query($connection,$query);
if (!$results) {
die('Invalid query: ' . mysqli_error());
}

$options = array(); 
$options[] = array("label" => "Select the supplier", "value" => "");
foreach ($results as $result) {
$options[] = array("label" => $result, "value" => $result);
}

wp_reset_postdata();
}

return $options;
mysqli_close($con);
}

Thanks in advance

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyuai8994 2018-10-24 09:31
    已采纳

    I've managed to achieve what I am after with the following

    $options = array();
    while($row = mysqli_fetch_assoc($results))
    {
    $options[] = array (
    'label' => $row['SUP_Name'],
    'value' => $row['SUP_Name'] );
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题