Welcome Guest, Not a member yet? Register   Sign In
How to get results from 2 tables in a database, using foreach within a foreach
#1

[eluser]Juan Velandia[/eluser]
Hello Everyone. Again I'm asking for help. I have 2 tables within a dabase:

cms_section
cms_article

there's a url_section field in the two tables to relate articles and sections, and I'm trying to make a dynamic menu here it is:

http://190.146.3.147/assa-seguros.com/

the thing is that i don't know how to tell the model wich article belongs to wich section. I've tried differente approaches but I finally decided to ask for help:

Here's the controller
Code:
function index()
    {
        $data['lang'] = 'ES';
        $data['meta_title'] = 'Pagina Principal';
        $data['meta_description'] = 'Sus asesores de seguros de confianza ';
        $data['meta_keywords'] = 'Sus asesores de seguros de confianza ';

        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['article_list'] = $this->article_content_model->get_article_list();

        $partials = array('top_logo'=>'view_top_logo', 'top_menu'=>'view_top_menu',  'main_footer'=> 'view_main_footer', );
        $this->template->load('master_home', $partials, $data);
      

       }

the models

Code:
<?



class Article_content_model extends Model {

    function Article_content_model()
    {
        parent::Model();
        $lang='ES';
    }

   function get_article_list()
    {
        $lang='ES';
        $this->db->select('name');
        $this->db->where('lang',$lang);
        $this->db->where('url_section',???????????????????;
        $query = $this->db->get('cms_article');

        return $query->result();
    }
}
?>

Code:
<?

class Item_placeholder_model extends Model {

    function Item_placeholder_placeholder()
    {
        parent::Model();
        $lang='ES';
    }

    function show_item_placeholder_1()
    {

$lang='ES';
        $this->db->select('name');
        $this->db->select('url_section');
        $this->db->where('lang',$lang);
        $this->db->select('meta_title');
        $this->db->select('meta_description');
        $this->db->where('placeholder',1);
        $query = $this->db->get('cms_section');
        
        return $query->result();
        
    }
and the view:

Code:
<div>
<ul class="menu" id="menu">
  &lt;?php foreach ($item_placeholder_1 as $row1): ?&gt;

    <li>&lt;?echo anchor('master/section/'.$row1->url_section, $row1->name , array('class'=>'menulink')) ?&gt;
        <ul>
            &lt;?php foreach ($article_list as $row2): ?&gt;
               <li><a href="#" class="sub">&lt;?echo $row2->name?&gt;</a>
               </li>
            &lt;?php endforeach; ?&gt;
        </ul>
    </li>
&lt;?php endforeach; ?&gt;
</ul>


  [removed]
    var menu=new menu.dd("menu");
    menu.init("menu","menuhover");
  [removed]
</div>

I hope you can give some ideas, thanks in advance!
#2

[eluser]ulugbek[/eluser]
Hi!

2 questions:

1. What exactly you want? While getting the list of articles you want to know which article belongs to which section?
2. What is url_section? is it section ID in articles table?
#3

[eluser]Juan Velandia[/eluser]
Hi ulugbek

Thanks for your response.

1. What exactly you want? While getting the list of articles you want to know which article belongs to which section?
Yes, you can see my intention here: http://190.146.3.147/assa-seguros.com/

I have different sections, and the name of some articles that i want to list in the menu, but i only get to list ALL the articles in All the section of the menu

2. What is url_section? is it section ID in articles table?

yes, I'm using it as an ID.

I hope you have some Ideas.. thanks again. I love CI but I'll have a lot to learn!
#4

[eluser]ulugbek[/eluser]
Code:
$this->db->select('*.cms_article, id.cms_section as section_id, name.cms_section as section_name,', FALSE); //select all fields from articles, section id and section name
$this->db->join('cms_section', 'cms_section.id' = 'cms_article.url_section');
$this->db->where('cms_article.lang', $lang);
$query = $this->db->get('cms_article');
return $query->result();

Hope will help!
#5

[eluser]Juan Velandia[/eluser]
Thanks ulugbek, I´ll check it tomorrow. It´s pretty late here...

I´ll let you know how it worked, best regards!
#6

[eluser]Juan Velandia[/eluser]
After some hours dealing with this issue i decided to go like this, it’s not the optimal solution because i put some SQL in the view, but I’m on a deadline… please feel free to post your comments. Any suggestion will be appreciated

Code:
<div>

<ul class="menu" id="menu">
&lt;?php foreach ($item_placeholder_1 as $row1): ?&gt;

    <li>&lt;?echo anchor('master/section/'.$row1->url_section, $row1->name , array('class'=>'menulink')) ?&gt;


&lt;?      $lang='ES';

        $this->db->select('name');

        $this->db->where('lang',$lang);

        $this->db->where("url_section",$row1->url_section);
        $query = $this->db->get('cms_article'); ?&gt;


             <ul>
               &lt;? foreach ($query->result() as $row):?&gt;
               <li><a href="#" class="sub">&lt;? echo $row->name; ?&gt;</li>
               &lt;? endforeach; ?&gt;
             </ul>

    </li>
&lt;?php endforeach; ?&gt;
</ul>


</div>




Theme © iAndrew 2016 - Forum software by © MyBB