The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Problem delivering data from controller to view
Discuss Problem delivering data from controller to view in the PHP Development forum on Dev Shed. Problem delivering data from controller to view PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 11th, 2012, 08:03 AM
|
 |
A Change of Season
|
|
|
|
|
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.
|

December 11th, 2012, 08:27 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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;
}
}
}
|

December 11th, 2012, 04:44 PM
|
 |
A Change of Season
|
|
|
|
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.
|

December 11th, 2012, 11:23 PM
|
 |
Lost in code
|
|
|
|
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
|

December 14th, 2012, 09:55 PM
|
 |
A Change of Season
|
|
|
|
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
}
|

December 15th, 2012, 04:01 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Yes, this makes much more sense.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|