Welcome Guest, Not a member yet? Register   Sign In
multiple query and foreach loop
#1

[eluser]forzaferrari87[/eluser]
Hallo,
I am a newbie at CodeIgniter and want to know how does something.

I have two tables. One of the is a category table and the other a normal one for informations. In my case I want to print out first the category with a foreach loop. That works. But in my second query I want to print out the information with the correct category. Without a framework I did this by a second foreach loop.

My question. How I can print out the second table throught the id of the category table?

Also I am searching at Google too, but I don't found the correct solution till now.
#2

[eluser]CroNiX[/eluser]
This is a SQL issue, not framework. Check out JOINs. You can get categories and information from separate tables with a single query.
#3

[eluser]CroNiX[/eluser]
client
-id
-name

projects
-id
-client_id
-description

Code:
SELECT client.name, projects.description
FROM client
JOIN projects on client.id = projects.client_id
ORDER BY client.name

will list each client name along with all of their project descriptions
#4

[eluser]forzaferrari87[/eluser]
[quote author="CroNiX" date="1388161737"]client
-id
-name

projects
-id
-client_id
-description

Code:
SELECT client.name, projects.description
FROM client
JOIN projects on client.id = projects.client_id
ORDER BY client.name

will list each client name along with all of their project descriptions[/quote]

Your SQL query is right, but I want it to do with where, because my target in the viewis this
Code:
<?php foreach($cats as $cats_item): ?>
<table class="table">
<tr><td>&lt;?php echo $cats_item['lcatname']; ?&gt;</td></tr>
&lt;?php foreach($links as $links_item): ?&gt;
<tr><td><a href="&lt;?php echo $links_item['linkurl']; ?&gt;" target="_blank">&lt;?php echo $links_item['linkname']; ?&gt;</a></td></tr>
&lt;?php endforeach; ?&gt;
</table>
&lt;?php endforeach; ?&gt;

i think this i can only make with where or there are another solution in codeigniter for printing out a category loop and into this one a link loop?
#5

[eluser]CroNiX[/eluser]
Code:
$data = $this->db
  ->select('client.name, projects.description')
  ->join('projects', 'client.id = projects.client_id')
  //->where(where conditions, if needed)
  ->order_by('client.name')
  ->get('client')
  ->result_array();

View:
Code:
$last_client = null;

foreach($data as $d)
{
  //only list the client name once (category in your case)
  if ($last_client != $d['name'])
  {
    $last_client = $d['name'];
    echo $d['name'] . '<hr>';
  }
  //now list all of their project descriptions (linkurl in your case)
  echo $d['description'] . '<br>';
}




Theme © iAndrew 2016 - Forum software by © MyBB