2014-03-05 23 views
0

我正在研究一個web應用程序,其中情況是這樣的 「當用戶點擊標記爲收藏按鈕時,jquery選擇該用戶的標識(即標記爲最喜歡的)」並調用阿賈克斯。現在我想創建一個存儲所有這些ID的數組,然後我將這個數組存儲到wordpress user_meta_field中。但問題是,每次Ajax創建一個新的數組,並不會將該ID存儲在同一個數組中。這裏是我的代碼在php中創建一個ID的動態數組

jQuery(function(){ 
jQuery('.fav-<?php echo $myID; ?>').click(function(){ 

var user_IDs = jQuery(this).attr('id'); 
var current_IDs = <?php echo $cui ?>; 

jQuery.ajax({ 
      url: '<?php bloginfo('url') ?>/', 
      type: 'POST', 
      data: 'ajaxreturn=101&id='+user_IDs+'&myid='+current_IDs, 
      success: function(result){ 
       jQuery('.testres').html(result); 
       } 

        });//ajax ends here 
}); 
       }); 

這裏是代碼生成響應中的functions.php

add_action('init','my_ajaxreturn_101'); 
function my_ajaxreturn_101(){ 
$reminders = array(); 
if($_POST['ajaxreturn']==101){ 

$userID = $_POST['id']; 
$loggedID = $_POST['myid']; 


    array_push($reminders,json_decode(get_user_meta($loggedID, 'meta_favorite', true), true)); 
    array_unique($reminders); 


     if(in_array($_POST['id'],$reminders)) { 
      echo "Failed: Auction already in list"; 
     } else { 
      array_push($reminders,intval($_POST['id'])); 

      if(update_user_meta($loggedID, 'meta_favorite', json_encode($reminders))) { 
       echo "Success"; 
      } 
     } 

     print_r($reminders); 

exit; 


    } 

    } 

,這裏是第一次點擊

SuccessArray ([0] => [1] => 39) 

這裏的輸出爲第二輸出點擊

SuccessArray ([0] => Array ([0] => [1] => 39) [1] => 34) 

這裏是第三次點擊的輸出

SuccessArray ([0] => Array ([0] => Array ([0] => [1] => 39) [1] => 34) [1] => 33) 
+0

由於數組不是全局的,你不能實現這一點,但如果你將數組存儲在會話變量中,你可以實現它 –

+0

嗨,謝謝你的回覆。你可以在這裏寫一點代碼,如何做到這一點。謝謝 – user3376865

+0

確定檢查第一個答案 –

回答

0

更改您的代碼

array_push($_SESSION['reminders'],json_decode(get_user_meta($loggedID, 'meta_favorite', true), true)); 
array_unique($_SESSION['reminders']); 

可以打印陣列狀

print_r($_SESSION['reminders']); 

注意:你推入會話之前,你必須啓動會話爲用戶

更改以下行:

... 
} else { 
    array_push($_SESSION['reminders'],intval($_POST['id'])); 
    if(update_user_meta($loggedID, 'meta_favorite', json_encode($reminders))) { 
     echo "Success"; 
    } 
} 
... 
+0

對不起Viswanath Polaki,它不工作。它只存儲一個值..只打印「成功」 – user3376865

+0

它的響應來自ajax。嘗試在另一個鏈接上打印會話,或者在if語句 –

+0

中打印數組而不是成功,您可以在這裏查看http://dev.optimizebusinessgrowth.com/gopaperboy/members/。請點擊每個框中的紅色心形圖標 – user3376865