功能没有按预期运行

时间:2014-08-10 18:57:15

标签: javascript php jquery html sql

开始我正在使用jquery,php,sql,html&的CSS。

我正面临着一个给我带来严重问题的问题。我正在尝试运行$ .post函数从我的数据库组中检索值(4个成员值存储在数据库的4列中。检索该值后,我运行while循环并将每个值附加到列表视图。

然后我将当前的while循环值发送到另一个$ .post函数,以检查我当前正在检查的成员的评分,并检索结果以显示我当前正在追加的li。

这就是我所拥有的

$('body').on("pagebeforeshow","#p-partyDetail",function(){                  
                var teamID = globalIndex;
                var currentMem = "";

                $.post("retrieveMemDetails.php", 
                {
                    teamID:teamID, // data to pass into php     
                    username:globalUsername,                
                },  // data to pass into php                                                                                    
                    function(response)
                    {
                        var x = 1; // define value as 1

                        while(x<=4){ // if loop is below or equal to 4, run loop                                                            
                            //member = response.mem + x;
                            member = response['mem' + x]; // define member1 in variable, 
                            currentMem = member;
                            console.log("current x value is " + x);

                            if(member !=""){
                                var y = x.toString();
                                console.log("y is " + y);

                                $.post("retrieveRatingDetails.php", 
                                {
                                    username:currentMem, // data to pass into php                                                                           
                                },  // data to pass into php                                                                        
                                    function(response2)
                                    {                                                                                       
                                        $("p#" +y).html(response2.rating);
                                        console.log("full name is " + response2.name + " rating is " + response2.rating);
                                        console.log("retrieve rating valued:"+response2.rating+" to p#"+y);
                                        console.log("end of loop cycle" + y);
                                    }, 'json'
                                );                                                                  

                                $("#partyDetail-listview").append('<li> <img src="images/final-fantasy-7-final-fantasy-vii-6973833-1024-768.jpg"> <h2>'+ member + '</h2> <p id="'+ x +'"></p> </li>').listview("refresh");  
                                console.log("appended count:" + x);
                            }   
                            x++;                            
                        }                               
                    }, 'json'
                );
            });

我的php用于检索会员详细信息

include_once('db.php');
session_start(); 

$teamID = ($_POST['teamID']);
$username = ($_POST['username']);

$result = $db->query("SELECT * FROM `studentparty` WHERE `id` = '".$teamID."'");
$result3 = $db->query("SELECT *` FROM `userdetails` WHERE `username` = '".$username."'");

    if(mysqli_num_rows($result)>0)
    {
        $row = mysqli_fetch_array($result);
        $mem1 = $row["mem1"];
        $mem2 = $row["mem2"];
        $mem3 = $row["mem3"];
        $mem4 = $row["mem4"];

        $result2 = json_encode(array("mem1"=>$mem1, "mem2"=>$mem2, "mem3"=>$mem3, "mem4"=>$mem4)); 
            echo $result2;
    }

我的php用于检索评分分数

include_once('db.php');
session_start(); 

$username = ($_POST['username']);

$result4 = $db->query("SELECT * FROM `userdetails` WHERE `username` = '".$username."'");

    if(mysqli_num_rows($result4)>0)
    {
        $row = mysqli_fetch_array($result4);
        $rating = $row["Rating"];
        $name = $row["FullName"];

        $result5 = json_encode(array("rating"=>$rating, "name"=>$name)); 
            echo $result5;      
    }

我的会员&amp;评级在不同的表格下,所以我两次调用$ .post。

显然经过几个小时的调试后,我发现它会循环播放

console.log("current x value is " + x);

然后

console.log("y is " + y);

然后

console.log("appended count:" + x);

在运行之前运行总循环次数为4

console.log("full name is " + response2.name + " rating is " + response2.rating);
console.log("retrieve rating valued:"+response2.rating+" to p#"+y);
console.log("end of loop cycle" + y);

这导致评级只对值y进行更新,因为函数的流程已经错误。

我的理想流程是 -retrieve来自php的党员详细信息 - 虽然使用while循环从php显示mem1,但只附加了我的ul。 - 将当前的mem1数据发送到我的下一个$ .post函数中以检索评级数据 用评级数据更新li -end of loop并以member2开头

有人能指出我的剧本有什么问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我强烈建议一个ajax请求足以获取请求的数据。如果你发出内部ajax请求,你必须等待其他行执行。因为它是异步函数。您还可以使用以下代码同步设置ajax调用:

$.ajax({
  ...
  async: false,
  ...
});

但是你必须等到你的回调函数执行完毕。所以,有时候你的表现很危险。

如果你改变你的php端,如下所示,你将得到一个包含成员和成员详细信息的对象的数组。

include_once('db.php');
session_start(); 

$teamID = ($_POST['teamID']);

$result_detail = $db->query("SELECT * FROM `studentparty` WHERE `id` = '".$teamID."'");

if(mysqli_num_rows($result_detail) > 0)
{
  $data = array();
  $mem = array();

  $row = mysqli_fetch_array($result);
  $mem[0] = $row["mem1"];
  $mem[1] = $row["mem2"];
  $mem[2] = $row["mem3"];
  $mem[3] = $row["mem4"];

  for($i = 0; $i < count($mem); $i++) {
    $result_rating = $db->query("SELECT *` FROM `userdetails` WHERE `username` = '".$mem[$i]."'");
    $rating_array = array();

    if(mysqli_num_rows($result_rating) > 0)
    {
      $row2 = mysqli_fetch_array($result_rating);
      $rating = $row2["Rating"];
      $name = $row2["FullName"];

      $rating_array = array("rating"=>$rating, "name"=>$name)); 
    }

    $data[$i] = array_merge(array("member" => $mem[$i]), $rating_array);
  }

  $result_json = json_encode($data); 
  echo $result_json;
}

我还将您的javascript代码用于新的回复。我无法测试两者。我希望它能很好地运作。

$('body').on("pagebeforeshow","#p-partyDetail",function(){                  
  var 
  teamID = globalIndex,
  currentMem = "",
  callback = function(response) {
    var member, id;
    $.each(response, funciton(i, obj) {
      member = obj['member'];
      id = (i+1); // if you have ids for members, you can send in php additionally. it would be better than (i+1) 

      $("#partyDetail-listview").append('<li> <img src="images/final-fantasy-7-final-fantasy-vii-6973833-1024-768.jpg"> <h2>'+ member + '</h2> <p id="'+ id +'"></p> </li>').listview("refresh");  
      console.log("appended count:" + id);

      if (member) {
        $("p#" + id).html(obj['rating']);
        console.log("full name is " + obj['name'] + " rating is " + obj['rating']);
        console.log("retrieve rating valued:"+obj['rating']+" to p#" + id);
        console.log("end of loop cycle" + id);
      }
    });
  };

  $.post("retrieveMemDetails.php", 
  {
    teamID:teamID, // data to pass into php     
    username:globalUsername,                
  },  // data to pass into php                                                                                    
  callback, 'json');
});