Welcome Guest, Not a member yet? Register   Sign In
Tutorial: News Section Parse Error
#1

[eluser]Anton-JandJ[/eluser]
Hi guys,

Trying to get to grips with CI and working through the tutorial but I've hit a hurdle rather early on and I don't know why or what to do! I've been carefully reading the explanations of the code and even copied and pasted the code so not to make a mistake but I now get a parse error in my news.php controller code on line 22! It's really annoying me.

Parse error: syntax error, unexpected T_PUBLIC in /CodeIgniter/application/controllers/news.php on line 22

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)
{
  $data['news'] = $this->news_model->get_news($slug);
}
}

public function index() //line 22
{
$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)
{
  $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 tried looking for other topics with this problem but others seemed to have issue with their model rather than the controller. It's quite disheartening when I'm trying to learn something new and get stuck so early on.

Any help would be much appreciated. Thanks.
#2

[eluser]Aken[/eluser]
Don't just copy/paste; you don't learn anything doing that.

A parse error usually means you have an extra or missing character somewhere that messes up the parse order. A curly brace, parentheses, semicolon, etc. So look near the line the error mentions for something out of place.
#3

[eluser]TheFuzzy0ne[/eluser]
In this particular instance, you have an extra curly brace somewhere.
#4

[eluser]Anton-JandJ[/eluser]
[quote author="Aken" date="1362790107"]Don't just copy/paste; you don't learn anything doing that.

A parse error usually means you have an extra or missing character somewhere that messes up the parse order. A curly brace, parentheses, semicolon, etc. So look near the line the error mentions for something out of place.[/quote]

I have manually typed out the code mentioned in the tutorial and got the same error. That's why I did a copy and paste job to ensure it was exactly right and alas, the same error.


[quote author="TheFuzzy0ne" date="1362823678"]In this particular instance, you have an extra curly brace somewhere.[/quote]

If you've spotted the out of place curly bracket can you point it out, because to me, there looks to be a correct ammount and all look in place.
#5

[eluser]TheFuzzy0ne[/eluser]
I will not point it out because the error tells you EXACTLY where it is. Unexpected T_PUBLIC means the "public" declaration of the method. Since that's unexpected, your problem must appear just before it.

I highly suggest using an IDE that supports syntax highlighting, and perhaps does error checking as well. I use Netbeans IDE for PHP. It's a bit clunky, but can help a lot.

Hope this helps.
#6

[eluser]TheFuzzy0ne[/eluser]
In fact, I've changed my mind, because it's more of a mess than I thought.

It looks like you've somehow mangled two classes together. index() cannot be defined once, and neither can view(). And both of these are appearing OUTSIDE of your class. You've clearly copied and pasted, but you've copied and pasted it in the wrong place.
#7

[eluser]Anton-JandJ[/eluser]
[quote author="TheFuzzy0ne" date="1362852144"]In fact, I've changed my mind, because it's more of a mess than I thought.

It looks like you've somehow mangled two classes together. index() cannot be defined once, and neither can view(). And both of these are appearing OUTSIDE of your class. You've clearly copied and pasted, but you've copied and pasted it in the wrong place.[/quote]

Yeah I repositioned the curly brace so that all the code was inside the class, then I got the error about not being able to define the same class twice, so I merged them and then got another error.

The tutorial isn't exactly clear on where to add the snippets of code which isn't too helpful. None the less I'll keep trying and solve it somehow.

Thanks for your responses.




Theme © iAndrew 2016 - Forum software by © MyBB