Welcome Guest, Not a member yet? Register   Sign In
How to pass variables from a model to another model?
#1

[eluser]Juan Velandia[/eluser]
Hello every one.

I've working on this for a while, but i decided to ask for help. I'm working on this site menu:

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

I have a database with:

cms_article
cms_section

they both have a field url_section wich works as an ID for the table cms_section. and I need to show the name of the article that belongs to the section in the menu: here's the model to get the sections
Code:
function show_section()
    {

        $lang='ES';
        $this->db->select('name');
        $this->db->where('lang',$lang);
        $query = $this->db->get('cms_section');
        return $query->result();
        
    }
here's the model to get the articles belonging to the sections
Code:
function get_article_list()

    {

        $lang='ES';

        $this->db->select('cms_article.name');
        $this->db->from('cms_article');
        $this->db->join('cms_section', 'cms_article.url_section = cms_section.url_section');
        $this->db->where('cms_article.lang',$lang);
        $this->db->where('cms_section.url_section',??????????????????);
        $query = $this->db->get();
        return $query->result();
        

    }
and here's the view

Code:
<ul class="menu" id="menu">
  &lt;?php foreach ($show_section as $row1): ?&gt;
     <li>&lt;?echo $row1->name?&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>

I think need to pass the url_section from the first model to the second to discriminate wich article bolong to wich section, but I I'm sotocked here, i only get to show all the records from cms_article

Any ideas?? thanks a lot in advance!
#2

[eluser]Althalos[/eluser]
I think I know what you are looking for so I put together an example:
http://pastebin.com/eihYT9Z6

What I do is I put both of these functions together into one. This isn't necessary, I just think it is a better solution. Then I loop through the result of the first and add in the articles. I have to do quite a few db queries, which may not be so good from an optimization perspective but it does the job well on smaller websites.
#3

[eluser]Juan Velandia[/eluser]
Hello Athalos. Thanks for your response. I think your approach is right. As I´m a newbie on CI and on Programming as well, i´ll have to check it carefully to fully understand the function. Again thanks a Lot and I´ll let you know how it worked... best regards!
#4

[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>


[removed]

    var menu=new menu.dd("menu");

    menu.init("menu","menuhover");

[removed]

</div>
#5

[eluser]Althalos[/eluser]
I had some errors in my code, if you still feel like giving it a try. First and foremost, the following line:
$sections = $this->db->select('name')->where('lang', $lang)->get('cms_section');

Should be:
$sections = $this->db->select('name')->where('lang', $lang)->get('cms_section')->result_array();

It is very important to return the query-data as an array, since I am assuming it to be an array in my foreach. I create a new key in this result array, into which I put relevant data which belongs to that section.

But if you don't feel you have the time to do it, it doesn't matter. Your code works and as long as the application is not going to huge and worked on by several people, adhering to the MVC principles is not the most important thing in the world.
#6

[eluser]Juan Velandia[/eluser]
Hello Althalos. Thanks for answering again.. I´ll will do it as you said. Clearly It´s a better solution and now that I´ve showed that the menu worked i´ll have the chance to do the coding the best I can.

Best regards from colombia

PD the site will be www.assa-seguros.com, hopefully it will be on the air tonight..
#7

[eluser]InsiteFX[/eluser]
If you need something from one model in another model you can pass it in
the models constructor!

InsiteFX
#8

[eluser]Juan Velandia[/eluser]
Hello InsiteFX, it seems that i haven't reach that part of the CI manual yet... I have to check it. I must confess that I'm a graphic designer learning programation by learning CI. Thanks for your support!
#9

[eluser]InsiteFX[/eluser]
To get a fast look at how this is done take a look at the
CodeIgniter Email Library!

InsiteFX
#10

[eluser]Juan Velandia[/eluser]
Thanks, I'll do it!

Regards!




Theme © iAndrew 2016 - Forum software by © MyBB