Welcome Guest, Not a member yet? Register   Sign In
How to make foreach loop inside another foreach loop?
#1

[eluser]term25[/eluser]
I have the 1st table called registered (contains all registered users) and this table contains this columns:

Code:
uid (int)
email (varchar)
telephone (varchar)

It looks like this

Code:
++++++++++++++++++++++++++++++++++++++++++++++++
+ uid   +  email             + telephone       +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 1    +  [email protected]    + 333-111-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 2    +  [email protected]    + 333-222-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 3    +  [email protected]    + 333-333-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 4    +  [email protected]    + 333-444-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 5    +  [email protected]    + 333-555-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 6    +  [email protected]    + 333-666-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++
+ 7    +  [email protected]    + 333-777-5555    +
++++++++++++++++++++++++++++++++++++++++++++++++

I have the 2nd table called referrers (contains the relationship between the parent user uid0 and the user uid1 who referes to this parent user. This table contains these columns:

Code:
id (int)
uid0 (int)
uid1 (int)

This second table holds the id which is autoincrement, then uid0 which is the parent user id and uid1 is the user id of the referer

It can have these values for example

Code:
+++++++++++++++++++++++
+ id   +  uid0 + uid1 +
+++++++++++++++++++++++
+ 1    +  2    +  3   +
+++++++++++++++++++++++
+ 2    +  2    +  5   +
+++++++++++++++++++++++


I know how to echo all registered emails and telephones from registered table in the foreach loop.

However, I need to join and echo also all referrers (uid1) emails thatbelong to certain user (uid0).

Code:
+++++++++++++++++++++++
+ id   +  uid0 + uid1 +
+++++++++++++++++++++++
+ 1    +  2    +  3   +
+++++++++++++++++++++++
+ 2    +  2    +  5   +
+++++++++++++++++++++++
+ 3    +  4    +  1   +
+++++++++++++++++++++++
+ 4    +  4    +  6   +
+++++++++++++++++++++++
+ 5    +  4    +  7   +
+++++++++++++++++++++++


So, the final echo result will look like this:

Code:
email                  telephone               referers
[email protected]        333-222-5555            [email protected], [email protected]
[email protected]        333-444-5555            [email protected], [email protected], [email protected]

How can I do this "threaded" foreach loop in my view?

MY VIEW LOOKS LIKE THIS:


Code:
<?php foreach ($registered as $obj) : ?>
  <tr>
    <td>&lt;?php echo $obj->email ?&gt;</td>
    <td>&lt;?php echo $obj->telephone?&gt;</td>
    <td>
        &lt;!-- HERE I NEED ANOTHER FOREACH LOOP WHICH ECHOES THE EMAILS OF REFERRERS --&gt;
    </td>
  </tr>
&lt;?php endforeach; ?&gt;

I think it shuld look like this, but I do not know how to send the actual user id to the get_refereres function in my model.

Code:
&lt;?php foreach ($registered as $obj) : ?&gt;
  <tr>
    <td>&lt;?php echo $obj->email ?&gt;</td>
    <td>&lt;?php echo $obj->telephone?&gt;</td>
    <td>
        &lt;?php foreach ($referrers as $obj2) ?&gt;
     &lt;?php echo $obj2->email; ?&gt;
&lt;?php endforeach; ?&gt;
    </td>
  </tr>
&lt;?php endforeach; ?&gt;

MY CONTROLLER LOOKS LIKE THIS:


Code:
public function index()
{
  $data['registered'] = $this->users_model->get_registered();
  $data['referrers'] = $this->users_model->get_referrers();
  $this->load->view('users_view', $data);
}

MY MODEL LOOKS LIKE THIS:


Code:
function get_registered()
    {
  $query = $this->db->order_by('email desc')
      ->get('registered');
  return $query->result();
    }

function get_referrers()
    {

         // I do not know what code should I write here :(
  
    }

Thanks in advance.




Theme © iAndrew 2016 - Forum software by © MyBB