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!


Messages In This Thread
How to get results from 2 tables in a database, using foreach within a foreach - by El Forum - 07-16-2010, 12:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB