0

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.

2 Answers 2

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>
0

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>
4
  • Thanks for your answer. $value isn't null. I get the right array when browsing the file. Jan 1, 2017 at 10:44
  • Question is, I don't know how to carry back this array to the wordpress form. Jan 1, 2017 at 10:46
  • Yes, it is a select to populate a drop down form element. I really don't know much about serialize. If serialize, I get the array. If unserialize, I get an error. Jan 1, 2017 at 10:51
  • added code sample from your clarification of your question
    – Vitzkrieg
    Jan 2, 2017 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.