How do I fetch multiple variables like var1,var2,.. in view page using loop in codeigniter?

StackOverflow https://stackoverflow.com/questions/22936663

質問

1.

In controller I have code like

2.

foreach($data['response']['data'] as $r) {
        $data['members'.$i] = $this->facebook->api( '/'.$r['id'].'/members');
         $i++;
    }
$this->load->view('index',$data);

3.

In view page I need value of member1,member2...upto membern.. but I could not understand how to get them. If any other way is there then tell me. Also I am sending the no of groups in $data. I want to see groupname(No of members).

役に立ちましたか?

解決

Change:

$data['members'.$i] = $this->facebook->api( '/'.$r['id'].'/members');

Into:

$data['members'][] = $this->facebook->api( '/'.$r['id'].'/members');

Also, you can now get rid of:

$i++;

And in your view:

foreach($members as $value)
{
    echo '<pre>';
    echo print_r($value, true);
    echo '</pre>';
}

他のヒント

The CodeIgniter documentation includes a section titled Generating Query Results which has some examples of what you are looking for.

Here's an (obviously contrived) example:

model:

class Model extends CI_Model
{
    public function get_data()
    {
        return $this->db->query('...');
    }
}

controller:

class Controller extends CI_Controller
{
    public function index()
    {
        $data['query_result'] = $this->model->get_data();
        $this->load->view('index', $data);
    }
}

view:

<?php foreach ($query_result->result() as $row): ?>
    <?php echo $row->field_1; ?>
    <?php echo $row->field_2; ?>
    <?php // and so on... ?>
<?php endforeach; ?>

Try this.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top