如何将增量变量合并到变量中

时间:2014-08-11 06:19:40

标签: javascript

我正在做一些复杂的事情,我需要把价值作为全局变量。 我做了一个while循环来尝试将每个新的循环成员数据存储到全局变量但是失败了。请查看包含此问题代码的行['mem' +x] = member;

我有一些全局变量

var mem1 = "";
var mem2 = "";
var mem3 = "";
var mem4 = "";

while(x<=4){ // if loop is below or equal to 4, run loop                                                            
    member = response['mem' + x]; // retrieve member data from php
    currentMem = member;
    ['mem' +x]  = member; // tried this but got error 'invalid assignment left hand side'
    console.log("mem1 is " +mem1);
    console.log("mem2 is " +mem2);
    console.log("mem3 is " +mem3);
    console.log("mem4 is " +mem4);
    console.log("current x value is " + x);
}
对于那些不确定我想做什么的人来说,这是一个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;
    }

0 个答案:

没有答案