Read data from MySQL table and print only a specific item of each line in codeigniter view

74 views Asked by At

In the model (model_users) I have the following :

public function members_list()
{ 
$query = $this->db->get('USERS');
return $query->result();
}

In the controler I have the following function:

public function members_list()
{
$this->load->view('members_list_view');
}

In the view file I have the following code:

$this->load->model('model_users');  

$membersArray = $this->model_users->members_list();

foreach ($membersArray  as $v1) {
       foreach ($v1 as $v2) {
                            echo "$v2\n";
                            }
                                }

My question is the following:

How to echo only the USERID of each line of the USERS table?

With the above code in the view file I got the whole line of the table.

1

There are 1 answers

0
Lupin On BEST ANSWER

Your double structure loop

foreach ($membersArray  as $v1) {
           foreach ($v1 as $v2) {    

Actually runs on each row and then on each element in each row, so when echoing $vs you should get the filed value, and at the end you should get all fields of all rows (maybe even with duplicates).

There is no need to run the second loop, unless you really want to loop on all fields of each array with out inserting them in the right places, usually what is done is something like this:

foreach ($membersArray  as $v1) {

    echo $v1['user_id']."\n";   
}    

In this case $v1 would be an array/object with all the fields of the DB table as elements inside.

If the query returns an object and not an array you can call the specific element like this:

echo $v1->user_id."\n";