EllisLab text mark
Advanced Search
     
Alter an object
Posted: 05 May 2009 06:42 AM   [ Ignore ]
Joined: 2009-04-21
390 posts

Situation: A method in a model is fetching info from a database. I call this method in a controller sending a url-segment as an argument. This is to get pageinfo. Now I’m having a hard time getting the info in the shape I want it. The info is pasted to $this->data[‘page_contents’] as an array. With my limited skill I can’t alter data. I would like to separate page_contents. Like this.

page_contents (just an example)

Array(
[0] Array(
   
[page] Start
   [title] 
Welcome
   [text] 
Lorem ipsum...
   )
[1] Array(
   
[page] Start
   [title] 
Hi again
   [text] 
Ut enim ad...
   )

Now I’m ending up with a solution thats somewhat questionable.
The view-file:

foreach($page_contents AS $row)
echo $row->page; break; 

The controller I’m tinkering with:

$url_segment $this->uri->segment(1'home');
$this->data['page_contents'$this->pagesdata->get_page_content($url_segment,1);

$data['header'$this->load->view('view_html_head'$this->datatrue);
$this->load->view('view_index'$this->data); 
 Signature 

I love the smell of code in the morning.

 
Posted: 05 May 2009 07:43 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2985 posts

Instead of returning all records with $query->result() you need to select only one page result and return that. You have $url_segment so in get_page_content() do something as simple as:

function get_page_content($slug)
{
    $query 
$this->db->getwhere'pages', array('slug' => $slug) );
    return 
$query->row();

That will return a single page and stop you having to run through all the records to find the right row.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 05 May 2009 07:54 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-04-21
390 posts

Well you got a point, that was one option I was going for. However, I need the other stuff two. So that would make two querys and that felt… queery. It wasn’t the render speed I was worried about, merely the fact that it is irritating that I can’t make it work with one query…

Thanx!!! I’ll go with your solution for now…

 Signature 

I love the smell of code in the morning.

 
Posted: 05 May 2009 09:41 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-30
70 posts

Seems to me that you would benefit from using 2 tables here for the sake of normilization.

I am presuming you are using just 1 table?

for example (fields here just for example)

a) page (page_id,url/title/meta,keywords/description etc) - page info
b) page_content (content_id,page_id,content etc)  - content blocks etc.

You could query for a single page/url and get data from both the tables in 1 query (joining the tables, results would look similar to your current results) or 2 queries (1 result for the page, another result for the content blocks etc) depending on how you decide to do it.

 
Posted: 05 May 2009 09:49 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-04-21
390 posts
lee74 - 05 May 2009 01:41 PM

Seems to me that you would benefit from using 2 tables here for the sake of normilization.

I do actually; pages and page_content. But I’ve got link_tags, meta etc in the config-file.

 Signature 

I love the smell of code in the morning.