#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,657
    Rep Power
    171

    Problem delivering data from controller to view


    I need a simpler way to print values in view. At the moment I print values like echo $page_data[1]['title'];.

    I like a way so I could only write $title instead of echo $page_data[1]['title'];. Here is my controller:
    PHP Code:
    foreach($results as $val)
        {
            foreach(
    $val as $column=>$value)
                {
                    
    $data['page_data'][] = array($column=>$value);
                }
        }
    $this->load->vars($data);
    $this->view_page(); 
    View
    PHP Code:
    echo $page_data[1]['title']; //Which I want to replace with $title. 
    Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    that data structure is rather odd. Is there a reason why you need an array of one-element associative arrays instead of a single associative array?

    So
    Code:
    array(
    	array(
    		'a' => 1
    	),
    	array(
    		'b' => 2
    	),
    	...
    )
    would become
    Code:
    array(
    	'a' => 1,
    	'b' => 2,
    	...
    )
    Then you can already leave out the numerical index.

    Regarding the $title:

    Well, it's your decision whether you put a value directly into the "global namespace" of the template or group several values with an array.

    If you want all values from the loop as separate template values and you don't see a problem with possible naming conflicts, just load them directly into the template:
    PHP Code:
    // make a list of allowed values
    $allowed_columns = array();

    foreach(
    $results as $val

        foreach(
    $val as $column => $value
        {
            if (
    in_array($column$allowed_columns) && !isset($data[$column]))
            {
                
    $data[$column] = $value;
            }
        } 

  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,657
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    that data structure is rather odd. Is there a reason why you need an array of one-element associative arrays instead of a single associative array?

    So
    Code:
    array(
    	array(
    		'a' => 1
    	),
    	array(
    		'b' => 2
    	),
    	...
    )
    would become
    Code:
    array(
    	'a' => 1,
    	'b' => 2,
    	...
    )
    Then you can already leave out the numerical index.

    Regarding the $title:

    Well, it's your decision whether you put a value directly into the "global namespace" of the template or group several values with an array.

    If you want all values from the loop as separate template values and you don't see a problem with possible naming conflicts, just load them directly into the template:
    PHP Code:
    // make a list of allowed values
    $allowed_columns = array();

    foreach(
    $results as $val

        foreach(
    $val as $column => $value
        {
            if (
    in_array($column$allowed_columns) && !isset($data[$column]))
            {
                
    $data[$column] = $value;
            }
        } 

    Thank you for the reply. I might as well show you the mvc and see what your solution is

    Model
    PHP Code:
    class Page_content_model extends CI_Model
        
    {
            public 
    $result =array();
            function 
    about_page()
                {
                    
    $query $this->db->get('sincity_content_about');
                    foreach(
    $query->result() as $row)
                        {
                            foreach(
    $row as $val=>$data)
                                {
                                    
    $this->result[] = array($val=>$data);
                                }
                        } 
                    return 
    $this->result;
                }
            
        } 
    Controller
    PHP Code:
    $this->load->model('page_content_model');
    $results $this->page_content_model->about_page();
    foreach(
    $results as $val)
        {
            foreach(
    $val as $column=>$value)
                {
                    
    $data['page_data'][] = array($column=>$value);
                }
        }
    $this->load->vars($data);
    $this->view_things(); 
    View
    PHP Code:
    <h1><?php echo $page_data[1]['title'];?></h1>
    I am also very interested on the idea of using constants, but I need more specific direction if you don't mind.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Is it possible for $this->db->get('sincity_content_about'); to return more than one row? If so, what is the meaning of each individual row? Your array structure seems to be deeper and more complicated than it needs to be. If it does not need to return more than one row, do what Jacques1 suggested and flatten it out:

    about_page() can return a single dimensional array like:
    array('title' => 'xyz')

    The controller can merge it directly into $data rather than setting it at $data['page_data']. The code Jacques1 is cleaner, but essentially does this (assuming about_page returns a 1d array):
    PHP Code:
    $results $this->page_content_model->about_page();
    $data array_merge($data$results); 
    Don't use constants here
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,657
    Rep Power
    171
    Finally came up with something that makes sense to me
    PHP Code:
    //CONTROLLER:

    $results $this->waitresses_model->list_waitresses();
    $data['records'] = $results->result_object;
    //MODEL:
    foreach($query->result() as $row)
        {
            
    $this->result[]=array('id'=>$row->id,'name'=>$row->name'photo'=>$row->photo);
        }
    return 
    $query;

    //VIEW
    foreach($records as $record)
            { 
               
    $record->id
            

  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Yes, this makes much more sense.

IMN logo majestic logo threadwatch logo seochat tools logo