Welcome Guest, Not a member yet? Register   Sign In
Basic Tutorial Help
#1

I am working with the simple tutorial in the documentation. 

Create a file at application/controllers/Pages.php with the following code. 




<?php
class Pages extends CI_Controller {

       public function view($page = 'home')
       {
       }
}




And this I am not sure if it goes in the Pages.php file or a new one, and if new I don't see the name of it and where it goes, that is which folder. Also, I do not see a closing ?> php tag in any of this code.




public function view($page = 'home')
{
       if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
       {
               // Whoops, we don't have a page for that!
               show_404();
       }

       $data['title'] = ucfirst($page); // Capitalize the first letter

       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page, $data);
       $this->load->view('templates/footer', $data);
}





Sorry, I am very new to CodeIgniter and actually php to be honest. I have tried adding the code to the Pages.php file but only get error's.

Is there a completed version somewhere so I can see what I am doing wrong?

Thanks for any help!
Reply
#2

It goes into the sane controller they are just showing you step by step how to add it in.
What did you Try? What did you Get? What did you Expect?

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

It says error on line 12 which is this:

public function view($page = 'home')

Here is my complete controller code in Pages.php:

<?php
class Pages extends CI_Controller {

public function view($page = 'home')
{
}
}

?>

<?php
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
?>

And complete errors:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/sportsd7/public_html/codeigniter/application/controllers/Pages.php:12)

Filename: core/Common.php

Line Number: 573

Backtrace:

A PHP Error was encountered

Severity: Parsing Error

Message: syntax error, unexpected 'public' (T_PUBLIC)

Filename: controllers/Pages.php

Line Number: 12

Backtrace:

Thanks for your help!
Reply
#4

You do not need the closing php tag anymore remove it.

Headers already sent means you have output some data to the view or a space in the code before the php tag.
What did you Try? What did you Get? What did you Expect?

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

I got it working. Thanks for your help! Now to finish the rest of the tutorial! I am sure I will have more questions as I continue.
Reply
#6

(This post was last modified: 12-31-2016, 06:13 PM by in2tech.)

News Section tutorial I am working on. Can display news items manually input into the database, but can not add items through the Create form:

News.php

Code:
<?php
class News extends CI_Controller {

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

       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');
       }

       public function create()
       {
               $this->load->helper('form');
               $this->load->library('form_validation');
           
               $data['title'] = 'Create a news item';
           
               $this->form_validation->set_rules('title', 'Title', 'required');
               $this->form_validation->set_rules('text', 'Text', 'required');

               if ($this->form_validation->run() === FALSE)
       {
               $this->load->view('templates/header', $data);
               $this->load->view('news/create');
               $this->load->view('templates/footer');

       }
       else
       {
               $this->news_model->set_news();
               $this->load->view('news/success');
       }
       }
     // Original bracket below
 
}

News_model.php

Code:
<?php
class News_model extends CI_Model {

       public function __construct()
       {
               $this->load->database();
       }
       
       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();
       }

       public function set_news()
       {
           $this->load->helper('url');
       
           $slug = url_title($this->input->post('title'), 'dash', TRUE);
       
           $data = array(
               'title' => $this->input->post('title'),
               'slug' => $slug,
               'text' => $this->input->post('text')
           );
       
           return $this->db->insert('news', $data);
       }

// Original bracket below

}

create.php (in the views/news folder)

Code:
<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

   <label for="title">Title</label>
   <input type="input" name="title" /><br />

   <label for="text">Text</label>
   <textarea name="text"></textarea><br />

   <input type="submit" name="submit" value="Create news item" />

</form>

routes.php (changes from tutorial)

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

All I get is a blank page when trying to insert a new record. I can manually add news items to the database through phpMyAdmin but not through the Create form!

Here is the error as it is not connecting to the database:

http://www.screencast.com/t/8NFaonzVBvW

Thanks for your help!

Update: It was the config base_url, and now is inserting data into the database via the create.php file, but the success message has an error I need to try and fix. I kind of figured it was something simple. Now to see if I can fix the new error.

When or if I figure this one out, I'll let you know.

Success Error:

http://www.screencast.com/t/VASIK9juls

All done. It helps if you include the success.php file. Everything working now. It's the little obvious things that always get me, having the wrong database name in the database.php file, not realizing the future code in the tutorial goes inside of the controller or model code, NOT below it. Not having the correct base_url in the config file, and not putting the success.php file in the views/news/ folder. Now to style it and see if I can come up with some changes to make my own.

Thanks and good luck everyone new to PHP and CodeIgniter like I am.
Reply
#7

You would need to create that yourself, should be almost like all the create stuff.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB