![]() |
Tutorial - Use new table - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Tutorial - Use new table (/showthread.php?tid=69416) |
Tutorial - Use new table - JeffreyB - 11-18-2017 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 ![]() 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'; RE: Tutorial - Use new table - Wouter60 - 11-18-2017 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(); 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. RE: Tutorial - Use new table - InsiteFX - 11-18-2017 You create a MY_Model PHP Code: // ./application/core/MY_Model.php 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. RE: Tutorial - Use new table - JeffreyB - 11-18-2017 (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.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. ![]() 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. RE: Tutorial - Use new table - JeffreyB - 11-18-2017 (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.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. ![]() 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. RE: Tutorial - Use new table - Wouter60 - 11-19-2017 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() 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> <?= is short for <?php echo RE: Tutorial - Use new table - InsiteFX - 11-19-2017 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. RE: Tutorial - Use new table - JeffreyB - 11-20-2017 (11-19-2017, 05:06 AM)InsiteFX Wrote: As I mentioned above CodeIgniter accesses the table through the model. 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. ![]() |