网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

PHP MySQL / WordPress功能/忍者表格

GG网络技术分享 2025-03-18 16:12 2


问题描述:

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

网友观点:

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\'] );

}

标签:

提交需求或反馈

Demand feedback