Welcome Guest, Not a member yet? Register   Sign In
Tutorial - Use new table
#6

The very basic idea is like this:

You have a database with whatever name you've given to it (Code08, mydatabase etc.) as long as you set up the database connection correctly in application/config/database.php. Inside the database, you have a table with news items, e.g. news08.

You have a model that collects records from the news08 table, like this:
PHP Code:
public function get_all_news()
{
 
 $query $this->db->get('news08');
 
 if ($query->num_rows() == 0) {
 
   return FALSE;
 
 }
 
 else {
 
   return $query->result();
 
 }


In your controller, you call the model function get_all_news to get the news items:
PHP Code:
$data['records'] = $this->news_model->get_all_news(); 

Now, the $data['records'] variable holds the result of the model function, i.e. all records from the news08 table (if there are any).

Next, your controller outputs the $data to a view:
PHP Code:
$this->load->view('news/show_all_news_items',$data); 

Important: the view file "show_all_news_items.php" is in the folder application/views/news

Finally, your view displays the result:
PHP Code:
<table>
<
tr><th>Date</th><th>Title</th></tr>
<?
php foreach($records as $record) : ?>
  <tr>
    <td><?= $record->date;?></td>
    <td><?= $record->title;?></td>
  </tr>
<?php endforeach; ?>
</table> 

<?= is short for <?php echo
Reply


Messages In This Thread
Tutorial - Use new table - by JeffreyB - 11-18-2017, 09:34 AM
RE: Tutorial - Use new table - by Wouter60 - 11-18-2017, 12:26 PM
RE: Tutorial - Use new table - by JeffreyB - 11-18-2017, 08:56 PM
RE: Tutorial - Use new table - by JeffreyB - 11-18-2017, 09:00 PM
RE: Tutorial - Use new table - by InsiteFX - 11-18-2017, 12:39 PM
RE: Tutorial - Use new table - by Wouter60 - 11-19-2017, 12:52 AM
RE: Tutorial - Use new table - by InsiteFX - 11-19-2017, 05:06 AM
RE: Tutorial - Use new table - by JeffreyB - 11-20-2017, 12:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB