-
Altazar
Explorer
-
Posts: 4
Threads: 1
Joined: Dec 2014
Reputation:
0
Hi, I'm building the News module following the tutorial. So far I've built this:
Model
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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(); }
}
?>
Controller
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class News extends MX_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('header', $data); $this->load->view('menu'); $this->load->view('index', $data); $this->load->view('footer'); }
public function view($slug) { $data['news_item'] = $this->news_model->get_news($slug); if (empty($data['news_item'])) { redirect('index'); } $data['title'] = $data['news_item']['title'];
$this->load->view('header', $data); $this->load->view('menu'); $this->load->view('view', $data); $this->load->view('footer'); }
function write() { $this->form_validation->set_rules('title', 'title', 'required|max_length[255]'); $this->form_validation->set_rules('body', 'body', 'required'); $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); if ($this->form_validation->run() == FALSE) // validation hasn't been passed { $this->load->view('header'); $this->load->view('menu'); $this->load->view('write'); $this->load->view('footer'); } else // passed validation proceed to post success logic { // build array for the model $form_data = array( 'title' => set_value('title'), 'body' => set_value('body') ); // run insert model to write data to db if ($this->news_model->SaveForm($form_data) == TRUE) // the information has been successfully saved in the db { $this->success(); } else { echo 'An error occurred saving your information. Please try again later'; // Or whatever error handling is necessary } } } } ?>
View (write news form)
PHP Code: <div class="jumbotron"> <div class="container">
<?php $attributes = array('class' => '', 'id' => ''); echo form_open($this->lang->lang() . '/news', $attributes); ?>
<p> <label for="title">title <span class="required">*</span></label> <?php echo form_error('title'); ?> <br /><input id="title" type="text" name="title" maxlength="255" value="<?php echo set_value('title'); ?>" /> </p>
<p> <label for="body">body <span class="required">*</span></label> <?php echo form_error('body'); ?> <br />
<?php echo form_textarea( array( 'name' => 'body', 'rows' => '5', 'cols' => '80', 'value' => set_value('body') ) )?> </p>
<p> <?php echo form_submit('submit', 'Submit'); ?> </p>
<?php echo form_close(); ?>
</div> </div>
View index (show all news)
PHP Code: <div class="jumbotron"> <div class="container"> <?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2> <div id="main"> <?php echo $news_item['body'] ?> </div> <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?> <p><a href="news/write">Write an article</a></p> </div> </div>
View
PHP Code: <div class="jumbotron"> <div class="container"> <?php echo '<h2>'.$news_item['title'].'</h2>'; echo $news_item['body']; ?> <br /> </div> </div>
I have a problem in Controller, if the $slug is not set in the URI:
if (empty($data['news_item']))
{
redirect('index');
}
I tried different things but always get errors...
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(12-30-2014, 05:29 AM)Altazar Wrote: I have a problem in Controller, if the $slug is not set in the URI:
if (empty($data['news_item']))
{
redirect('index');
}
I tried different things but always get errors...
You must pass an uri (controller name + method + param), something like redirect('/news') or redirect('/news/view/abc123').
See : http://www.codeigniter.com/userguide3/he...l#redirect
-
Altazar
Explorer
-
Posts: 4
Threads: 1
Joined: Dec 2014
Reputation:
0
Quote:You must pass an uri (controller name + method + param), something like redirect('/news') or redirect('/news/view/abc123').
I actually do this:
$this->success();
Quote:public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
$this->success();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('header', $data);
$this->load->view('menu');
$this->load->view('view', $data);
$this->load->view('footer');
}
http://kulturfokus.eu/en/news/view/film (slug)
http://kulturfokus.eu/en/news/view ( errors)
http://kulturfokus.eu/en/news (all news)
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
You have this error :
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: slug
Filename: controllers/news.php
Line Number: 23
You need to put a default value to $slug if it's optional, and since index() display all news, you can simply redirect to it.
PHP Code: public function view($slug = '') { if (empty($slug)) { // index is the default function, it's the same as redirect('news/index') redirect('news'); }
$data['news_item'] = $this->news_model->get_news($slug); ... }
-
Altazar
Explorer
-
Posts: 4
Threads: 1
Joined: Dec 2014
Reputation:
0
01-01-2015, 08:35 AM
(This post was last modified: 01-01-2015, 08:37 AM by Altazar.)
Thanks, but I'm still getting an error:
PHP Code: public function view($slug = '') {
if (empty($slug)) { redirect('news/index'); }
else { $data['news_item'] = $this->news_model->get_news($slug); $data['title'] = $data['news_item']['title'];
$this->load->view('header', $data); $this->load->view('menu'); $this->load->view('view', $data); $this->load->view('footer'); } }
Happy 2015
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
Code: A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home/altazar/webapps/kulturfokus/aplikacija/language/english/about_lang.php:1)
Filename: helpers/url_helper.php
Line Number: 542
Headers already sent... you have other bugs that prevents the redirection! Look at your about_lang.php file. There's something wrong with it.
-
Altazar
Explorer
-
Posts: 4
Threads: 1
Joined: Dec 2014
Reputation:
0
Quote:Headers already sent... you have other bugs that prevents the redirection! Look at your about_lang.php file. There's something wrong with it.
I checked about_lang.php but there is no error and other pages use the translations without any problems. I added a default slug (film) and it works.
PHP Code: public function view($slug = '') { if (empty($slug)) { $slug = "film"; $data['news_item'] = $this->news_model->get_news($slug); $data['title'] = $data['news_item']['title']; $this->load->view('header', $data); $this->load->view('menu'); $this->load->view('view', $data); $this->load->view('footer'); } }
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
|