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

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'] );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建