php - populate dropdown wordpress form from database custom table

admin2025-06-05  2

Trying to populate a drop down element of a html form in a word press page with data from SQL query in a php file in a php directory: site/php/.

Php code is working fine:


<?php
require_once "connectPDO.php";
define('WP_USE_THEMES', false);
require('../wp-load.php');
require_once('../wp-config.php');
require_once('../wp-includes/wp-db.php');
require_once('../wp-includes/pluggable.php');
require('../wp-blog-header.php');

if ( is_user_logged_in() ) {
    global $current_user;
    get_currentuserinfo();
    $user = $current_user->ID;
} else {
    echo '<a href="'. get_bloginfo('url') .'/wp-admin" class="loginlinktop">Login</a>';
}

global $wpdb;
$results = $wpdb->get_results("SELECT my-column FROM wp_table WHERE user_id = ."'$user'".");
if($results){
    foreach($results as $value){
        echo serialize ($value);
    }   

}
?>

In a wordpress page I use execPHP plugin and this instruction:

<?php require_once(ABSPATH. '/php/phpfile.php'); ?>

I just get a blank page. 1 How can I get the result on word press?

2 A code sample to populate drop down from a serialized array?

Thanks. I've been more than a week with this.

Trying to populate a drop down element of a html form in a word press page with data from SQL query in a php file in a php directory: site/php/.

Php code is working fine:


<?php
require_once "connectPDO.php";
define('WP_USE_THEMES', false);
require('../wp-load.php');
require_once('../wp-config.php');
require_once('../wp-includes/wp-db.php');
require_once('../wp-includes/pluggable.php');
require('../wp-blog-header.php');

if ( is_user_logged_in() ) {
    global $current_user;
    get_currentuserinfo();
    $user = $current_user->ID;
} else {
    echo '<a href="'. get_bloginfo('url') .'/wp-admin" class="loginlinktop">Login</a>';
}

global $wpdb;
$results = $wpdb->get_results("SELECT my-column FROM wp_table WHERE user_id = ."'$user'".");
if($results){
    foreach($results as $value){
        echo serialize ($value);
    }   

}
?>

In a wordpress page I use execPHP plugin and this instruction:

<?php require_once(ABSPATH. '/php/phpfile.php'); ?>

I just get a blank page. 1 How can I get the result on word press?

2 A code sample to populate drop down from a serialized array?

Thanks. I've been more than a week with this.

Share Improve this question edited Dec 31, 2016 at 13:18 SNS 1731 silver badge14 bronze badges asked Dec 31, 2016 at 11:24 Joan PescadorJoan Pescador 111 gold badge1 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Thanks everybody for your answers. You bring me value clues. Finally I opted to make the query directly from wordpress page with the wordpress sintax for the defaults wordpress tables, using a little trick: Including my custom tables in wp-includes/wp-db.php.

wordpress code:

<?php
global $current_user;
get_currentuserinfo();
$user = $current_user->ID;
global $wpdb;
global $result;
$courses = array();
$result = $wpdb->get_results ( "SELECT * FROM $wpdb->mytable WHERE user_id = $user" );
?>
name
<select>
  <option selected="selected">name</option>
  <?php
  foreach( $result as $value ) { ?>
    <option value="<?php echo $value->name; ?>"><?php echo $value->name; ?></option>
  <?php
  }
?>
</select>

I would first check that $value is not null. Also, would it make more sense to call unserialize($value); here since you are pulling the data out of the database and not storing it?

Code sample for populating dropdown:

<select class="dropdown" id="mydropdown" name="mydropdown" title="My Dropdown">
    <?php
    foreach ($results as $value) {
            echo '<option value="' . unserialize($value) . '">' . unserialize($value) . '</option>';
    }
    ?>
</select>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749054256a315964.html

最新回复(0)