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