Discussion:
wp_send_json_success not working with array?
BenderisGreat
2014-01-01 03:01:19 UTC
Permalink
Here is my code - The goal is to return the database data formatted to update
a JS chart. For some reason the function isnt even seeing the $chartData as
executing. if ($chartData) returns false.

function ams_chart_range_selection() {
global $wpdb, $_current_member;


$range = $_POST['range'];
$limit = 5;
if ($range == 'week' )
$limit = 7;
elseif ($range == 'month') {
$limit = 30;
} else
$limit = 90;


$logDataTable = $wpdb->prefix . 'jo_plugin_options';
$chartData = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
$logDataTable WHERE user_id = %d ORDER BY date ASC LIMIT %d",
$_current_member->ID, $limit), ARRAY_A );

$max = count($chartData);
$converted = array();
$counter = 0;

foreach ($chartData as $item) {
$converted[] = "[".$counter++.",".$item['date']."]";
$list = implode(",",$converted);
}

if (!$chartData) {
wp_send_json_error();

} else {

$data = array(
'range' => $range,
'list' => $list );


wp_send_json_success($data);


}


}



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/wp-send-json-success-not-working-with-array-tp43098.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
David Ernst
2014-01-01 05:49:35 UTC
Permalink
Hey Greg— do you have skype? I really admire your attitude. Happy New Years!
—David
Post by BenderisGreat
Here is my code - The goal is to return the database data formatted to update
a JS chart. For some reason the function isnt even seeing the $chartData as
executing. if ($chartData) returns false.
function ams_chart_range_selection() {
global $wpdb, $_current_member;
$range = $_POST['range'];
$limit = 5;
if ($range == 'week' )
$limit = 7;
elseif ($range == 'month') {
$limit = 30;
} else
$limit = 90;
$logDataTable = $wpdb->prefix . 'jo_plugin_options';
$chartData = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
$logDataTable WHERE user_id = %d ORDER BY date ASC LIMIT %d",
$_current_member->ID, $limit), ARRAY_A );
$max = count($chartData);
$converted = array();
$counter = 0;
foreach ($chartData as $item) {
$converted[] = "[".$counter++.",".$item['date']."]";
$list = implode(",",$converted);
}
if (!$chartData) {
wp_send_json_error();
} else {
$data = array(
'range' => $range,
'list' => $list );
wp_send_json_success($data);
}
}
--
http://wordpress-hackers.1065353.n5.nabble.com/wp-send-json-success-not-working-with-array-tp43098.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2014-01-01 13:42:04 UTC
Permalink
If you var_dump( $chartData ) what do you get? I suspect that the query is failing and you will get null.

Yes, wp_send_json_success() works with arrays. It is probably used primarily with arrays.

-J.D.
Post by BenderisGreat
Here is my code - The goal is to return the database data formatted to update
a JS chart. For some reason the function isnt even seeing the $chartData as
executing. if ($chartData) returns false.
function ams_chart_range_selection() {
global $wpdb, $_current_member;
$range = $_POST['range'];
$limit = 5;
if ($range == 'week' )
$limit = 7;
elseif ($range == 'month') {
$limit = 30;
} else
$limit = 90;
$logDataTable = $wpdb->prefix . 'jo_plugin_options';
$chartData = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
$logDataTable WHERE user_id = %d ORDER BY date ASC LIMIT %d",
$_current_member->ID, $limit), ARRAY_A );
$max = count($chartData);
$converted = array();
$counter = 0;
foreach ($chartData as $item) {
$converted[] = "[".$counter++.",".$item['date']."]";
$list = implode(",",$converted);
}
if (!$chartData) {
wp_send_json_error();
} else {
$data = array(
'range' => $range,
'list' => $list );
wp_send_json_success($data);
}
}
--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/wp-send-json-success-not-working-with-array-tp43098.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...