Welcome Guest, Not a member yet? Register   Sign In
Extending an array - or adding another item to array - I think
#1

[eluser]KSShooter[/eluser]
Noob warning ahead.

Ok, not exactly sure the title phrases the question precisely, but what I want to achieve:

I'm playing with the "news" tutorial in the documentation, and trying to learn both PHP and CI at the "same time" - so forgive the ignorance, not exactly sure how this should be accomplished within CI.

The basic tutorial is working fine, and I've toyed with it, changing a few things to see and gain an understanding of how this all works. What I'm doing now is extending the "news" database into not only having a "news" table, but also a "submitter" table. So I've added a "submitterid" to the news table, and a new table "submitter" with just an "id" (autonumber), and "name" columns. Modified the "news" table to enter submitterids for the existing records, and added corresponding records to the "submitter" table.

So - I'm basically trying to extend the tutorial to do what most apps do with a normalized database - read a record from table1, and then get the supporting "descriptions/fields/etc." from another table based on keys within the record from table1 - then display the information in the view.

I've modified the ~/news/view.php file to show news->submitterid, so I know I have that coming through just fine

Code:
<?php
echo '<h2> View ' .$news_item['title'].'</h2>';
echo $news_item['ntext'];
echo '<p>Submitter ' .$news_item['submitterid'];

Now I'm looking to read the corresponding record from the "submitter" table, and instead of displaying news->submitterid, I want to display submitter->name, coming from the second table. So it seems like I would want to change application/controllers/news.php from:


Code:
public function view($slug) {
  $data['news_item'] = $this->news_model->get_news($slug);
  
  if (empty($data['news_item'])) {
   show_404();
  }
  $data['title'] = $data['news_item']['title'];
  
  $this->load->view('templates/header', $data);
  $this->load->view('news/view', $data);
  $this->load->view('templates/footer', $data);
}

to something like this:

Code:
public function view($slug) {
  $data['news_item'] = $this->news_model->get_news($slug);
  $data['news_item'] += $this->news_model->get_submitter($data['news_item']['submitterid']);
OR
  $data['news_item']['submittername'] = $this->news_model->get_submitter($data['news_item']['submitterid']);
  
  if (empty($data['news_item'])) {
   show_404();
  }
  $data['title'] = $data['news_item']['title'];
  
  $this->load->view('templates/header', $data);
  $this->load->view('news/view', $data);
  $this->load->view('templates/footer', $data);
}

Ok - overly simplified, as I should make sure I got the "slug" record to begin with, not sure that that is the correct method to extend an array (see second example), etc., etc., - but is this a "correct" approach? Extending the array to be passed to the view? Or read another data array - "data2" and pass the second array in the view (I'm presuming I can pass multiple elements to the view - but I haven't run into any examples that show that), and then change the view to use the specific element->field name that was passed, etc.?

Or is there a more elegant way of doing this? I'm sorry - but I've done some forum/wiki/Google searching, and I'm not coming up with anything here that appears to answer what I'm looking for.
#2

[eluser]KSShooter[/eluser]
FYI - I DO have this basic methodology working - I'm just not sure if this type of thing is better done with db joins, or there is some other slicker method of doing this.




Theme © iAndrew 2016 - Forum software by © MyBB