douzhang2680 2015-03-11 06:37
浏览 60

如何使用for循环在codeigniter中将变量中的一个或多个值保存为数组?

I have a array as $result which i got from this select operation.I am using codeigniter

$this->db->select('*');
$this->db->from($table);
$this->db->where('User_Id',$User_Id);
$query = $this->db->get();
$results = $query->result();
$result = $results[0];

I want to save this values in a variables .what i am doing is

$HeightFrom=$result->HeightFrom;
$HeightTo=$result->HeightTo;
$MaritalStatus=$result->MaritalStatus;
$Religion=$result->Religion;
$MotherTongue=$result->MotherTongue;
$Community=$result->Community;
$ProfileCreatedby=$result->ProfileCreatedby;etc....

but i have 50 values in arrary .so want to use for loop .what can I do ?how to write the for loop code . and my question is using for loop i want to save the array value as variable name which contains the value

  • 写回答

2条回答 默认 最新

  • dtcd27183 2015-03-11 06:47
    关注

    You can fetch the results and assign it to a variable at the same time, like this:

    while ($result = $query->result()) {
    
        $HeightFrom=$result->HeightFrom;
        $HeightTo=$result->HeightTo;
        $MaritalStatus=$result->MaritalStatus;
        $Religion=$result->Religion;
        $MotherTongue=$result->MotherTongue;
        $Community=$result->Community;
        $ProfileCreatedby=$result->ProfileCreatedby
    }
    

    It will just stop when no more records are there to fetch.

    Note: In its current format it will overwrite your variables in each iteration of the loop - you have to do something with it, for example output it or save it in another array.

    评论

报告相同问题?