Reputation: 546
I have a table in my MySQL and i display data on the page using FOR EACH loop. After that i did an ajax request to update every time my FOR EACH loop when in the database there is inserted a new raw.
The AJAX request is successful but returning empty data. I do not understand what I am doing wrong. I will appreciate any help. Maybe something wrong I am doing and WordPress is taking my code strange?
File That I display data with FOR EACH Loop: ajax.php
// DB Connection (Wordpress)
global $wpdb;
$_IL_START = 0;
$_IL_LIMIT = 10;
if( isset($_GET['paged']) ){
$_IL_ID = $_GET['paged'];
$_IL_START = ($_IL_ID-1) * $_IL_LIMIT;
}
$_IL_QUERY_INTERNAL = $wpdb->get_results( "SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT");
//FOR EACH Loop
echo '<ul id="list">';
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
{
echo $_IL_RESULT_DATA->il_name;
echo $_IL_RESULT_DATA->il_email;
$_IL_RESULT_DATA->il_date;
}
echo '</ul>';
File That I call that ajax.php with jquery: (display.php)
//require once the FOR EACH Loop
require_once ($_SERVER["DOCUMENT_ROOT"]."/ajax.php");
//and the JS Code
<script type="text/javascript" >
fetch();
function fetch(){
jQuery.ajax({
cache: false,
url: '/ajax.php',
success: function(data) {
jQuery(data).hide().prependTo("#list").slideDown("slow");
if(jQuery("#list li").length > 15){
jQuery('#list li:gt(14)').remove();
}
console.log(data);
setTimeout("fetch()", 100);
}
});
}
</script>
When I make console.log(data) is printing this only < ul id="list" >< /ul >
and nothing else.
RESULT OF var_dump($_IL_QUERY_INTERNAL);
array(3) { [0]=> object(stdClass)#427 (11) { ["il_id"]=> string(1) "6" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(14) "[email protected]" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(6) "534534" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(0) "" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } [1]=> object(stdClass)#426 (11) { ["il_id"]=> string(1) "4" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(13) "[email protected]" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(7) "3112312" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(51) "this my comment, appears only if the comment exists" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } [2]=> object(stdClass)#430 (11) { ["il_id"]=> string(1) "3" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(14) "[email protected]" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(6) "345345" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(0) "" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } }
This is my pagination:
$_IL_ROWS_COUNT = mysql_num_rows(mysql_query("select * from $_IL_TABLE_NAME_INTERNAL"));
$_IL_TOTAL = ceil( $_IL_ROWS_COUNT / $_IL_LIMIT );
if($_IL_LIMIT < $_IL_ROWS_COUNT){
echo '<span class="il_pagination_block_admin">';
if( $_IL_ID > 1 )
{
echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".($_IL_ID-1)."' class='il_pagination_prev'><span class='il_pagination_prev_icon'></span></a>";
}
echo "<ul class='il_pagination'>";
for( $i = 1; $i <= $_IL_TOTAL; $i++ )
{
if( $i == $_IL_ID ) { echo "<li class='il_pagination_current'>".$i."</li>"; }
else { echo "<li><a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".$i."'>".$i."</a></li>"; }
}
echo "</ul>";
if( $_IL_ID != $_IL_TOTAL )
{
echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".( $_IL_ID + 1 )."' class='il_pagination_next'><span class='il_pagination_next_icon'></span></a>";
}
echo "</span>";
Upvotes: 0
Views: 1890
Reputation: 31173
EXPLANATION
on the server page (php) that receive the facebook notice, you have to put an extra check so
if( facebook_data_come ) {
// 1 - insert the new data inside your table
// 2 - on a separate database table that you will call for example facebook_notification_check where you have an INT of 0 or 1 (default to 0), update it to 1 after the first step above.
// 3 - on the ajax php page you will first check if facebook_notification_check is equal to 1, if so you will continue with your code and reset the facebook_notification_check back to 0 otherwise you return an empty string...
}
<?php
global $wpdb;
$html = 'unknown error';
$query = "SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT";
if($_IL_QUERY_INTERNAL = $wpdb->get_results($query)){
if(count($_IL_QUERY_INTERNAL)){
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
{
$html = '<li>'. $_IL_RESULT_DATA->il_name . $_IL_RESULT_DATA->il_email . $_IL_RESULT_DATA->il_date .'</li>';
}
} else {
$html = '';
}
} else {
header("HTTP/1.0 500 Internal server error");
$html = mysql_error() . " | sql query : " . $query;
}
echo $html;
?>
<ul id="list"></ul>
<script type="text/javascript" >
fetch();
function fetch(){
jQuery.ajax({
cache: false,
url: '/ajax.php',
success: function(data) {
console.log(data);
if(data!=''){
jQuery(data).prependTo("#list");
jQuery('#list li:first-child').slideDown("slow");
setTimeout("fetch()", 100);
}
},
error: function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
}
});
}
</script>
Upvotes: 1