PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 08:03 AM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,604 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 9 m 31 sec
Reputation Power: 71
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.

Reply With Quote
  #2  
Old December 11th, 2012, 08:27 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,877 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 32 m
Reputation Power: 813
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;
        }
    } 


Reply With Quote
  #3  
Old December 11th, 2012, 04:44 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,604 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 9 m 31 sec
Reputation Power: 71
Quote:
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.

Reply With Quote
  #4  
Old December 11th, 2012, 11:23 PM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,946 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 28 m 55 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
  #5  
Old December 14th, 2012, 09:55 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,604 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 9 m 31 sec
Reputation Power: 71
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
        


Reply With Quote
  #6  
Old December 15th, 2012, 04:01 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,877 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 32 m
Reputation Power: 813
Yes, this makes much more sense.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Problem delivering data from controller to view

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap