Welcome Guest, Not a member yet? Register   Sign In
Tutorial − News section, help !!!
#1

[eluser]Kuglepen[/eluser]
Hey, I cant see where i have to use this code.

From Tutorial
The next thing to do is passing this data to the views! but what views are they talking about ?

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

[eluser]qcsites[/eluser]
The views that are created in the previous portion of the tutorial

http://ellislab.com/codeigniter/user-gui...pages.html

#3

[eluser]Kuglepen[/eluser]
arh okay, so its just another function in same controller.. ?
#4

[eluser]qcsites[/eluser]
Sorry, not sure what you are asking.

Code:
public function index()
{
// This is where the data is set/gathered to be passed to the views
$data[‘news’] = $this->news_model->get_news();
$data[‘title’] = ‘News archive’;

// Below are the views the data is being passed to
// This view file is the application/views/templates/header.php The $data passes the title which is displayed in the header file by echo $title
$this->load->view(‘templates/header’, $data);

// This view file is the application/views/news/index.php
$this->load->view(‘news/index’, $data);

// This view file is the application/views/templates/footer.php
$this->load->view(‘templates/footer’);
}
#5

[eluser]Kuglepen[/eluser]
I dont know, where i have to use this function you are posting ? tutorials says i have to passing this data in to views..
#6

[eluser]qcsites[/eluser]
The index function you posted originally does pass the data in to the view.

I just added comments to it to try to show you what was happening in the code.
#7

[eluser]Unknown[/eluser]
I am newbie in codeigniter and I also facing the problem in this tutorial in News Category. I did same as said in the tutorial. After added a news, all the news displayed correctly. But when I click on link, it redirects on the particular news page, but there it shows the following error..

Error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: news_items

Filename: news/view.php

Line Number: 1

view.php file


Code:
<h2>&lt;?php echo $news_items['title'] ?&gt;</h2>
&lt;?php echo $news_items['text']; ?&gt;

How to solve this problem ?

Thanks in advance

#8

[eluser]qcsites[/eluser]
Looks like you are not defining or passing the data from the controller.

Do you have code similar to this in the view?
Code:
&lt;?php foreach ($news as $news_item): ?&gt;

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    <p><a href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>

&lt;?php endforeach ?&gt;

In the controller calling the view do you have this?
Code:
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');
}

Also, do you have the model function and/or the table shown in the tutorial?
#9

[eluser]Unknown[/eluser]
If you want variables to be available in your view, you have to make sure they are assigned in your controller. So since the standard way to pass the variables to the view is with the $data array, you should have something like this in your controller

$data['news_items']['title'] = "Amazing Title";
$data['news_items']['text'] = "Lorem ipsum...";

When I started with CI that always confused me. Whatever you assign as $data['variable'] in the controller, is available as $variable in the view.

So you could even avoid having multidimensional arrays by doing your variables like ..

$data['news_item_title'] = "Amazing Title";
$data['news_item_text'] = "Lorem ipsum...";

Which would be available as $news_item_title and $news_item_text.
#10

[eluser]InsiteFX[/eluser]
In the view if your passing the array all you need to do is php echo $title and $text.




Theme © iAndrew 2016 - Forum software by © MyBB