• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter Tutorial News section

#11
(07-27-2015, 12:40 PM)Dracula Wrote:
(07-27-2015, 09:00 AM)christaliise Wrote: I am now studying the News section - user_guide\tutorial\news_section.html - and having to spend many hours trying to understand, and where I expect I should be spending only around an hour on such simple instructions.


(1) The 1st is clear. - Open up the application/models directory and create a new file called News_model.php and add the following code.
(2) The 2nd is difficult to understand. - Connect to your database and run the SQL command below. Also add some seed records. - How do I connect to the database and how do I run the SQL command? I assume the command is to be pasted into News_model.php
(3) The 3rd is partially clear. - Add the following code to your model. - I assume the code is to be pasted into News_model.php
(4) The 4th is partially clear. - Create the new controller at application/controllers/News.php. - I assume the code is to be pasted into News.php
(5) The 5th is difficult to understand. - The next thing to do is passing this data to the views. - I assume the code is to be pasted into News.php
(6) The 6th is clear. - Create application/views/news/index.php and add the next piece of code.
(7) The 7th is difficult to understand. - Go back to the news controller and update view() with the following: - I assume the code is to be pasted into News.php but don't understand why there are 2 loads.
(8) The 8th is clear. - The only things left to do is create the corresponding view at application/views/news/view.php. Put the following code in this file.
(9) The 9th is clear. - Modify your routing file (application/config/routes.php) so it looks as follows.

After following those instructions and assumptions, and pointing my browser to http://localhost/MyProject/index.php/news I can only get an error page - The website cannot display the page - HTTP 500

This is the code in my News_model.php page



Code:
<?php
class News_model extends CI_Model
{

       public function __construct()
       {
               $this->load->database();
       }

       CREATE TABLE news (
       id int(11) NOT NULL AUTO_INCREMENT,
       title varchar(128) NOT NULL,
       slug varchar(128) NOT NULL,
       text text NOT NULL,
       PRIMARY KEY (id),
       KEY slug (slug)
);

public function get_news($slug = FALSE)
{
       if ($slug === FALSE)
       {
               $query = $this->db->get('news');
               return $query->result_array();
       }

       $query = $this->db->get_where('news', array('slug' => $slug));
       return $query->row_array();
}
}
This is the code in my News.php page


Code:
<?php
class News extends CI_Controller
{
       public function __construct()
       {
               parent::__construct();
               $this->load->model('news_model');
       }

       public function index()
       {
               $data['news'] = $this->news_model->get_news();
       }

       public function view($slug = NULL)
       {
               $data['news_item'] = $this->news_model->get_news($slug);
       }

       public function index()
       {
       $data['news'] = $this->news_model->get_news();
       $data['title'] = 'News archive';

       $this->load->view('templates/header', $data);
       $this->load->view('news/index', $data);
       $this->load->view('templates/footer');
       }

       public function view($slug = NULL)
       {
       $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');
       }
}

I have tried many variations, and now exhausted, and need guidance. Can anybody help?


CodeIgniter is right for you if you need clear, thorough documentation.

Yeah right!
Reply

#12
(07-29-2015, 01:32 PM)Wouter60 Wrote:
Quote:(5) The 5th is difficult to understand. - The next thing to do is passing this data to the views. - I assume the code is to be pasted into News.php

You're right: the index() method is part of the News controller.
It collects the news items via the model.
It stores the data in an array called $data.

Then, it passes this array to the views that are loaded:

PHP Code:
$this->load->view('news/index'$data); 

The first parameter for loading the view is the view file itself. In this case application/views/news/index.php.
The second parameter is an array containing all the variables that you want to pass to the view. In this case $data.

The view will automatically extract the array. So, when your controller defines an array element $data['news'], the view can simply process a variable $news. Which - in this case - is an array itself, containing one or more records. That's why the view index.php has a foreach .... endforeach loop to display the text from all news items.
Are you getting the picture?

Thanks Wouter60, I'm grateful for your help. I'll need to study more to get the picture.
Reply

#13
(07-28-2015, 08:33 AM)Wouter60 Wrote: Like @ciadmin suggested, the best way is to create the table with the database tool you are using, because this is just a one-time issue.
If you managed to create a database, that means that you have access to phpmyadmin. Open it and select your database. Click the SQL tab and paste the sql-command that you find in the CI documentation. Click Start to run the command. The news table is created.
After you've done that, insert a few records. Use your fantasy to make up a few titles, and search the internet for lorum ipsum texts that can be used as imaginary news items.
Once these records exist, you can use CI to collect records from the database and pass data to your views.

I've deleted the sql-command from my News_model.php page and pasted it via phpmyadmin and created the table. I've inserted two records with titles, slug and text.

And pointed my browser to http://localhost/MyProject/index.php/news BUT still get the HTTP 500 error.

I assume each time I add code that it is to be added "before" the last curly brace }.   I've tried moving the curly brace but no different.  

Any suggestions where I could be going wrong?
Reply

#14
Check if your controller has multiple index() and view() functions in it. Your example code that you posted earlier this week shows that it does. Function names inside a class must be unique. When the documentation says that you have to add code to the index function in your controller, don't create a new function, but put the extra code before the closing curly brace of the existing index function. The same goes for the view() function.

Besides all that,  you are not supposed to get an http 500 error, but a readable php error message on your localhost. Check your index.php file for the setting of the ENVIRONMENT constant. Does it say 'development' or 'production'?
Development suppresses php error messages. That's fine when your website is online, but while you are writing, testing and debugging your application, you want to see what's going wrong, don't you?

The http 500 error may also be caused by (a) bad server configuration or (b) an error in your .httaccess file.
To check (a): type http://localhost in your browser. You should get the welcome page of your server.
To check (b): rename your .htaccess file to .htaccess_org and try to start your website again. It should start the index.php file with your default controller.

If you are still getting the same error, please upload your News.php file as an attachment to this topic.
Reply

#15
(07-31-2015, 11:09 AM)Wouter60 Wrote: Check if your controller has multiple index() and view() functions in it. Your example code that you posted earlier this week shows that it does. Function names inside a class must be unique. When the documentation says that you have to add code to the index function in your controller, don't create a new function, but put the extra code before the closing curly brace of the existing index function. The same goes for the view() function.

Besides all that,  you are not supposed to get an http 500 error, but a readable php error message on your localhost. Check your index.php file for the setting of the ENVIRONMENT constant. Does it say 'development' or 'production'?
Development suppresses php error messages. That's fine when your website is online, but while you are writing, testing and debugging your application, you want to see what's going wrong, don't you?

The http 500 error may also be caused by (a) bad server configuration or (b) an error in your .httaccess file.
To check (a): type http://localhost in your browser. You should get the welcome page of your server.
To check (b): rename your .htaccess file to .htaccess_org and try to start your website again. It should start the index.php file with your default controller.

If you are still getting the same error, please upload your News.php file as an attachment to this topic.

(1) Yes there were multiple index() and view() functions in News.php, so I've fixed that and now this is my current News.php file.

Code:
<?php
class News extends CI_Controller
{
       public function __construct()
       {
               parent::__construct();
               $this->load->model('news_model');
       }

       public function index()
       {
       $data['news'] = $this->news_model->get_news();
       $data['title'] = 'News archive';

       $this->load->view('templates/header', $data);
       $this->load->view('news/index', $data);
       $this->load->view('templates/footer');
       }

       public function view($slug = NULL)
       {
       $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');
       }
}

After changing that I now get this error message

An Error Was Encountered
Unable to load the requested class: Authentication

(2) I've checked C:/xampp/htdocs/MyProject/index.php and changed this line
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
to
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
but the results are no different however above that line it says
NOTE: If you change these, also change the error_reporting() code below
but there are 3 error_reporting() codes and I don't know which to change
error_reporting(-1);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);

(3a) When I point my browser to http://localhost I get the XAMPP Welcome message and the URL changes to http://localhost/xampp/ but you say I should get "welcome page of your server" so I guess that means Apache server.

(3b) I've renamed 2 htaccess files to htaccess_org
C:/xampp/htdocs/MyProject/htaccess_org
C:/xampp/htdocs/MyProject/application/htaccess_org

Having done all of that I still get the error message

An Error Was Encountered
Unable to load the requested class: Authentication

I've searched many files but can't find a class named "Authentication".
Reply

#16
I guess there is an autoload instruction for authentication. Check this in the file application/config/autoload.php.
PHP Code:
$autoload['libraries'] = array(...); 
If there is reference to 'authentication', remove it.
Reply

#17
(08-02-2015, 11:53 AM)Wouter60 Wrote: I guess there is an autoload instruction for authentication. Check this in the file application/config/autoload.php.


PHP Code:
$autoload['libraries'] = array(...); 
If there is reference to 'authentication', remove it.

Yes, there was reference to 'authentication' and I have removed it, and now have the following;

Code:
$autoload['libraries'] = array('database', 'email', 'session', 'table');

and I've tried removing the others, BUT I now get this same result whatever is the content;

404 Page Not Found
The page you requested was not found.

The page is at application/controllers/News.php and I'm pointing my browser at http://localhost/MyProject/index.php/news
Reply

#18
There could be an error in your application/config/routes.php file.
Which routes did you declare in that file?
Reply

#19
(08-03-2015, 10:45 AM)Wouter60 Wrote: There could be an error in your application/config/routes.php file.
Which routes did you declare in that file?

The routes.php is correct.

I have solved the problem. I tried pointing my browser to http://localhost and back to http://localhost/MyProject/index.php/news but it made no difference but then I tried http://localhost/MyProject/index.php/pages/view, which worked OK and then back to http://localhost/MyProject/index.php/news, and then it worked!!

I guess there is some error in my internal cache. Thanks for all your help.

Reference to C:/xampp/htdocs/MyProject/index.php
NOTE: If you change these, also change the error_reporting() code below

Can you tell me which one of the 3 error_reporting() codes to change, and what to change it to?

error_reporting(-1);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
Reply

#20
In CI, the error reporting level depends on the ENVIRONMENT setting in index.php.
There is a line with this code:
PHP Code:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); 
This checks for the existence of the $_SERVER['CI_ENV'] superglobal.
If you're not sure about that, you can simply define the ENVIRONMENT constant like this:
PHP Code:
define('ENVIRONMENT','development'); 
Once you are ready to upload your website to a hosted server (in order to make it available online), you change it to:
PHP Code:
define('ENVIRONMENT','production'); 
On my computer, I have an index.php and an index_production.php
The first one is using the 'development' environment, the other one 'production'.
When I upload my website, I make sure that on the hosted server, my index_production.php is renamed to index.php.

A bit lower in index.php, you will see a "switch (ENVIRONMENT) {  }" structure.
With that, the correct error reporting level is set, depending on the value of the ENVIRONMENT constant.
The different levels are explained here: http://php.net/manual/en/function.error-reporting.php
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.