Welcome Guest, Not a member yet? Register   Sign In
Passing dynamic data to a view - help needed
#1

[eluser]k2zs[/eluser]
Hello,

I am new to MVC and CodeIgniter. I have been working with it since this weekend and have made a lot of progresss but I am stuck.

I have a model that collects page information from a single database table to deliver the web pages dynamically. I want to set some dynamic variables for the page title, page id, parent id, and so on. Page ID and parent ID will be used to change the css class in navigation to show the active page. Here is my model:

Code:
<?php
/**
* Model - application/models/main_model.php
*
* DB query to get all active page titles to view in nav
* This model collects the page information in 3 functions
* getMainNav collects the pages from the table where parent ID equals 0
* getSubNav collects the page entries where parent ID is NOT equal to 0
* getPage selects the defaule page for the root URL or parses the uri for a requested page
*/

class Main_model extends Model {
    

    // select the page body content
    function getPage()
    {
        if ($this->uri->total_segments() <> 0) {
            $this->db->where('pgName', $this->uri->segment(1));
        } else {
            $this->db->where('ParentID', 0);
            $this->db->where('pgNavOrder', 1);
        }
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
    
    // collect the items for main navigation
    function getMainNav()
    {

        $this->db->order_by('ParentID', 'asc');
        $this->db->order_by('pgNavOrder', 'asc');
        $this->db->where('pgActive', 1);
        $this->db->where('ParentID', 0);
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
    
    // collect the sub-navigational items
    function getSubNav()
    {
        $this->db->order_by('ParentID', 'asc');
        $this->db->order_by('pgNavOrder', 'asc');
        $this->db->where('pgActive', 1);
        $this->db->where('ParentID !=', 0);
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
}

Here is my controller:

Code:
&lt;?php
class Home
    extends Controller {

    function Home()
    {
        // load controller parent
        parent::Controller();
        // load this controllers model
        $this->load->model('main_model');
    }

    // get the model info and pass it to the template
    function index()
    {
        $data['mainNav'] = $this->main_model->getMainNav();
        $data['subNav'] = $this->main_model->getSubNav();
        $data['page'] = $this->main_model->getPage();
        if ($data['page'] <> '') {echo 'boo';
        }
        $this->load->view('template_view', $data);

    }
}

I have been trying to figure out how to create the dynamic variables in the controller and pass them to the template but can't seem to get it. The nav, sub-nav, and page body work as I am doing a "foreach" loop in the view but to but I don't want to do a "foreach" inside the title tag in the head. as a temporary solution I have a PHP block at the top before any output that loops the "pages" data to create the variables I need but to me that defeats the whole purpose of MVC...

Can someone explain to me how to build dynamic variables in a controller and pass them to a view?
#2

[eluser]Mischievous[/eluser]
looks like everything is good... just do &lt;?=$mainNav?&gt; in your view?
#3

[eluser]k2zs[/eluser]
[quote author="Mischievous" date="1288316770"]looks like everything is good... just do &lt;?=$mainNav?&gt; in your view?[/quote]
?????? Whats that supposed to do? It has nothing to do with what I need and it does nothing when I do that... Could you have ment
Code:
&lt;?= echo $mainNav?&gt;
? If so you can't echo an array....
#4

[eluser]k2zs[/eluser]
[quote author="Mischievous" date="1288316770"]looks like everything is good... just do &lt;?=$mainNav?&gt; in your view?[/quote]... also whats the significance of
Code:
&lt;?= =>
? How does that differ from
Code:
&lt;?php ?&gt;
?
#5

[eluser]k2zs[/eluser]
Perhaps I should start over with this question...

How do I extract specific elements of a multi-dimensial array in my controller, returned from a model, and pass those elements to my view as a variable?
#6

[eluser]tonanbarbarian[/eluser]
why not just pass the array to the view and sort it out in the view

as for the &lt;?= ?&gt;, that is basic PHP.
http://au.php.net/manual/en/language.bas...hpmode.php
#7

[eluser]k2zs[/eluser]
[quote author="tonanbarbarian" date="1288331729"]why not just pass the array to the view and sort it out in the view

as for the &lt;?= ?&gt;, that is basic PHP.
http://au.php.net/manual/en/language.bas...hpmode.php[/quote]

Ok... Being new to codeigniter and the MVC concept I thought you were supposed to keep raw processing in the controller. This is what I have done as a work around in my view:
Code:
&lt;?php
if (!EMPTY($page)) {
    foreach($page as  $row) :
    $pgTitle = $row->pgTitle;
    $pText = $row->pText;
    $pgID = $row->pgID;
    $ParentID = $row->ParentID;
    endforeach;
}
?&gt;

if that is normal I will continue that way...

Thanks
#8

[eluser]tonanbarbarian[/eluser]
it depends on what you mean by raw processing

a view is supposed to be the presentation logic, so az view is supposed to take the data provided by the controller and present it in some format, mostly HTML
generally that can require looping through recordsets to display all of the data etc.

the things that fall into the grey area are whether any formatting on the data should be done in the view or in the controller or the model.

personally i do most of my formatting in the view because of the way i implement MVC

there is not hard and fast rule in CI how you implement the mvc
in fact in CI the only thing you MUST have is a controller, if you choose not to use models or views you can, but almost no one does this

The way I see MVC is...

Controller handle the business logic, i.e. they take a request from the user and decide how that is processed to give a result. This means taking data input by the user and passing it to models and then sending the resulting data from the model to the view where it is formatted for presentation to the user.

Models handle raw data. Create, Retrieve, Update and Delete. Models do not need to really know what the data is for or how it will be presented. Some simple formatting of data such as concatenation or substring of fields but no html formatting etc

Views present raw data in some format. Usually the format is HTML, but it could just as easily be CSV, XLS, PDF, JSON, XML or any other format you like.
This means I generally have a bit more PHP code in my view than some people may like, but some complex formatting can be done with helpers, such as creation of form fields, which helps resuce or simplify the php code in the view
#9

[eluser]k2zs[/eluser]
Thank you for that explanation,

In the above model I would like to limit the results of "getSubNav" to the "ParentID" returned by "getPage". How would I pass variables between functions in my model?




Theme © iAndrew 2016 - Forum software by © MyBB