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

(This post was last modified: 11-18-2017, 09:39 AM by JeffreyB. Edit Reason: for clarity )

I am trying to use a second table in my 'news' database as setup in the CI Tutorial. It might be helpful if the Tutorial entries for database and table were not called the same thing - news. Anyway, I am trying to NOT use the 'news' table, but rather ONLY use the 'news08' table which I created in I guess what is the default database 'news'. As you can see in my code below, I am having difficulty pointing to the table I want - news08, which what I thought was correct syntax is commented out. I THINK that is my issue anyway. I have tried many other ways of phrasing this. Some actually process the tutorial code, but head to the database directory news/news instead of news/news08 where I want to go. Also, when I go back to using parts of the Tutorial code, I get other, but understandable and expected errors, so I believe this is my problem. I have also adjusted routes.php several different ways, and database.php also, and just haven't hit the right combination. As I said, news/news =  database/table sure adds to  permutations lol. Not to mention the Tutorial controller is 'news.php' and Tutorial views is /news. (This could get very musical - please check my newsviews, or my controllernews which is interfering with my newsroutes or possibly my newsmodel. Oh well Smile )

     public function __construct()
        {
                parent::__construct();
                $this->load->model('news_model');
                $this->load->helper('url_helper');
                $this->load->library('table');
        }

        public function index()
        {
  //    $data['news'] = $this->news_model->get_news('news/news08');
        $data['news08'] = $this->news_model->get_news();
        $data['title'] = 'Now changed from Tutorials News archive';
Reply
#2

I wonder which tutorial you've read. In the CI Tutorial about News, "news08" is never mentioned.
And I think you're mixing up database/table names with controller / method names.
CI doesn't want you to create a "news" database, just a "news" table.

The line
PHP Code:
$data['news08'] = $this->news_model->get_news(); 
in your code is just getting (all) news items from the table that you refer to in your model. It assigns the result to the 'news08' element of the $data array.
As a result, you can display the news-items in your view, by referring to the $news08 array (or object).

The code you are sharing here, is part of your controller.
Please, also show us what your news_model looks like.
Reply
#3

You create a MY_Model

PHP Code:
// ./application/core/MY_Model.php

class MY_Model extends CI_Model
{
    
/**
     * --------------------------------------------------------------------
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

 
       public $tableName     // Holds the database table name.

    
public $limit 20;        // Default database record limit.


    // --------------------------------------------------------------------

    /**
     * __Construct()
     *
     * Constructor    PHP 5+    NOTE: Not needed if not setting values!
     *
     */
 
        public function __construct()
 
        {
 
           parent::__construct();
 
        }

    
/**
     * -----------------------------------------------------------------------
     * Return Object's:
     * result() = $row->title;
     * row()    = $row->title;
     *
     * Return Array's:
     * result_array() = $row['title'];
     * row_array()    = $row['title'];
     * -----------------------------------------------------------------------
     */

    // -----------------------------------------------------------------------

    /**
     * setTableName()
     *
     * @param  string $tableName
     */
    
public function setTableName($tableName '')
    {
        
$this->tableName $tableName;
    }

}

// ./application/models/Your_model.php

class Your_model extends MY_Model
{

    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

           
$this->setTableName('tableName');

        
log_message('debug'"Name Model Class Initialized");
    }

    
/**
    * getPost ()
    * -------------------------------------------------------------------
    *
        * EXAMPLE:
        *
    * @param  string $id
    * @return array
    */
    
public function getPost($id '')
    {
        
$data = array();

        
$this->db->where('id'$id);
        
$this->db->limit(1);

        
$query $this->db->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            
$data $query->row_array();
        }

            
$query->free_result();

            return 
$data;
    }



Place all of your general code in the MY_Model and  extend it to your models.

You can now use setTableName() to change the name of the table.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(11-18-2017, 12:26 PM)Wouter60 Wrote: I wonder which tutorial you've read. In the CI Tutorial about News, "news08" is never mentioned.
And I think you're mixing up database/table names with controller / method names.
CI doesn't want you to create a "news" database, just a "news" table.

The line
PHP Code:
$data['news08'] = $this->news_model->get_news(); 
Thanks for the replies.

Both replies are helpful.


I am basically following the Tutorial, which is fine, and which I had up and the entire Tutorial running to completion. My issues started when I tried to add a new table and have that working.  Backing up a bit, it is entirely possible my database is called 'news' and my Tutorial table called 'news' because phpmyadmin would not let me create a table called "news" (as per the tutorial told me to create) without a database in existence. So I made one (called "news" just in case,) then put the table "news" inside that. True there is nothing in the Tutorial called "news08." That is just my iterations as I work with CI.
 
The reply about the model might I think points me in the right direction. I was thinking the News.php controller was flowing straight down to the 404 error message s few lines down. I am geting the 404 , which is being triggered over something in the very first lines of News.php.

I think what is happening is the ENTIRE "news_model" is being loaded completely at the top, just before the library and helper. Correct?

Which still doesn't tell me the syntax for my database/table or how to config this up, but I think I am making progress. I will post again on this when I run through all the combinations. Smile

With some playing, as in making a whole new database called Code08, and a "news08" table within that, I can get different error messages referring to line 6 in "news_model" which seems to indicate again, I just ain't pointing at the right database and table.
Reply
#5

(11-18-2017, 12:26 PM)Wouter60 Wrote: I wonder which tutorial you've read. In the CI Tutorial about News, "news08" is never mentioned.
And I think you're mixing up database/table names with controller / method names.
CI doesn't want you to create a "news" database, just a "news" table.

The line
PHP Code:
$data['news08'] = $this->news_model->get_news(); 
Thanks for the replies.

Both replies are helpful.


I am basically following the Tutorial, which is fine, and which I had up and the entire Tutorial running to completion. My issues started when I tried to add a new table and have that working.  Backing up a bit, it is entirely possible my database is called 'news' and my Tutorial table called 'news' because phpmyadmin would not let me create a table called "news" (as per the tutorial told me to create) without a database in existence. So I made one (called "news" just in case,) then put the table "news" inside that. True there is nothing in the Tutorial called "news08." That is just my iterations as I work with CI.
 
The reply about the model might I think points me in the right direction. I was thinking the News.php controller was flowing straight down to the 404 error message a few lines down. I am getting the 404 , which is being triggered over something in the very first lines of News.php.

I think what is happening is the ENTIRE "news_model" is being loaded completely at the top, just before the library and helper. Correct?

Which still doesn't tell me the syntax for my database/table or how to config this up, but I think I am making progress. I will post again on this when I run through all the combinations. Smile

With some playing, as in making a whole new database called Code08, and a "news08" table within that, I can get different error messages referring to line 6 in "news_model" which seems to indicate again, Other error messages tell me my database is not configged correctly. I just ain't pointing at the right database and table.
Reply
#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
#7

As I mentioned above CodeIgniter accesses the table through the model.

So if you create a MY_Model with general database methods all you need to do
is change the table name in the methods like the code I gave you.

If you create a getAll() method it will get all records from any database table
if you just change the table name in the method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

(This post was last modified: 11-20-2017, 06:37 PM by JeffreyB. Edit Reason: my mistake - cant count )

(11-19-2017, 05:06 AM)InsiteFX Wrote: As I mentioned above CodeIgniter accesses the table through the model.

So if you create a MY_Model with general database methods all you need to do
is change the table name in the methods like the code I gave you.

If you create a getAll() method it will get all records from any database table
if you just change the table name in the method.

Man....three days of slogging through some of the worst code (mine!) and I ended up re-installing the entire CI Tutorial again. Iteration Code10 now lol. The good news is, it is making more sense now. Every time I read the tutorial (slowwwwwwwly) I pick up something, or it is sinking in better. Plus I get to see a few items where maybe the instructions could have been clearer, (although they are pretty good) and I just happened to fluke into the right setup in a couple of my earlier interations.

 My apologies for my density. I think I am making progress. Thanks for all your help. Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB