CI 2.1.1 documentation The page you requested was not found - php

I Have following step by step the documentation in news section. In news, CI can open data from database. in database there are 2 data like this
Put news data from database
In there, there is view article link. but after click the link, CI open error 404 like this
News Item from View article
can help me to solve the problem? i'm sorry my english is bad
News_model.php
<?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();
}
}
Controller (News.php)
<?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)
{
$data['news'] = $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');
}
}
Index.php in news View
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
view.php in view
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];

You haven't completed the "Routing" section at the end of that tutorial.
Also, don't start learning with CodeIgniter 2.x - it's no longer supported - go for version 3 instead.

Related

Query and display result in CodeIgniter

I have the following code that query and display the result from database.
Database
id title desc
1 BUILD The
2 BEAUTIFUL Use
The piece of code in model folder
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_webinfo()
{
$query = $this->db->select('title')->from('webinfo')->where('id', 1)->get();
return $query->row_array();
}
}
?>
The piece of code in controllers folder
<?php
class Portfolio extends CI_Controller {
public function view($portfolio = 'home')
{
if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
$this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');
}
}
?>
The page that I want to display the data
<h2 class="intro-text text-center">
<strong><?php echo $webinfo['title']; ?></strong>
</h2>
However, I get the following error when I run the display page
A PHP ERROR WAS ENCOUNTERED
SEVERITY: NOTICE
MESSAGE: UNDEFINED VARIABLE: WEBINFO
May I know how should I go about editing the code to resolve the error?
Try 01
Controller
<?php
class Portfolio extends CI_Controller {
public function view($portfolio = 'home')
{
if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
print_r($data['webinfo']);
/* $this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');*/
}
}
?>
Model
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_webinfo()
{
$query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
$result = $query->result_array();
return $result;
}
}
?>
view
<h2 class="intro-text text-center">
<strong><?php echo (!empty($webinfo[0]['title'])) ? $webinfo[0]['title'] : 'Empty Title' ;; ?></strong>
</h2>
Try this
In Model
public function get_webinfo()
{
$query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
$result = $query->result_array();
return $result;
}
In View
<h2 class="intro-text text-center">
<strong><?php echo (!empty($webinfo['title'])) ? $webinfo['title'] : 'Empty Title' ;; ?></strong>
</h2>
Note: If I'm right sometimes this $webinfo['title'] gives error and its work fine with with $webinfo[0]['title']
And there is way to write controller.
__construct
index()
then other all functions
EDIT 01
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
print_r($data['webinfo']);
/* $this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');*/
}
controller:
public function index()
{
$data['result'] = $this->portfolio_model->get_webinfo();
//$this->load->view('templates/header', $data);
//$this->load->view('portfolio/index', $data);
// $this->load->view('templates/footer');
print_r($data);
}
model
public function get_webinfo()
{
$this->db->select('*');
$this->db->from('pwebinfo');
$this->db->where('id',1);
$query = $this->db->get();
if($query->num_rows() ==''){
return 'failure';
}else{
$results = $query->result();
$result = $results[0];
return $result;
}
}
view
<h2 class="intro-text text-center">
<strong> <?php echo $result[0]->title; ?> ?></strong>
</h2>

Undefined variable & Invalid argument supplied for foreach() in CodeIgniter

I have the following code that query the database and display it in view.
However, I am getting these error:
- Message: Undefined variable: portfolio Filename: portfolio/home.php
- Message: Invalid argument supplied for foreach() Filename: portfolio/home.php
How should I resolve the error?
Controller (Portfolio.php)
<?php
class Portfolio extends CI_Controller {
public function view($portfolio = 'home')
{
if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['portfolio'] = $this->portfolio_model->get_portfolio();
$this->load->view('templates/header', $data);
$this->load->view('portfolio/home', $data);
$this->load->view('templates/footer');
}
}
?>
Model (Portfolio_model)
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_portfolio()
{
$query = $this->db->select('title')->from('webinfo')->get();
return $query->result_array();
}
}
?>
View (home.php)
<?php foreach ($portfolio as $portfolio_item): ?>
<h3><?php echo $portfolio_item['title']; ?></h3>
<?php endforeach; ?>
routes.php
$route['portfolio/(:any)'] = 'portfolio/view/$1';
$route['portfolio'] = 'portfolio';
$route['default_controller'] = 'portfolio/view';
$route['(:any)'] = 'portfolio/view/$1';
Couple of things may work
Model
public function get_portfolio() {
$query = $this->db->get('webinfo');
if ($query->num_rows() > 0 ) {
return $query->result_array();
} else {
return FALSE;
}
}
On controller
<?php
class Portfolio extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url');
}
public function index() {
$data['portfolio'] = array();
$results = $this->portfolio_model->get_portfolio();
// You may need to remove !
if (!isset($results) {
foreach ($results as $result) {
$data['portfolio'][] = array(
'title' => $result['title']
);
}
}
$this->load->view('templates/header', $data);
$this->load->view('portfolio/home', $data);
$this->load->view('templates/footer');
}
public function view($portfolio = 'home') {
if (!file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php')) {
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
}
View
<?php if ($portfolio) {?>
<?php foreach ($portfolio as $portfolio_item) { ?>
<h3><?php echo $portfolio_item['title']; ?></h3>
<?php } ?>
<?php } else { ?>
<h3>No Result</h3>
<?php } ?>
I also would recommend auto loading the database
$autoload['libraries'] = array('database');
Instead of using $this->load->database();

Codeigniter news tutorial view method not working

Hey guys im learning codeigniter and im on the news tutorial. Im pretty much finished but my view method is showing 404 rather than the news itself. I've tried to debug with the following code
echo '<pre>';
var_dump($this->news_model->get_news($slug));
exit();
and that returns
NULL
heres how my controller works thats calling the method
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
}
public function index() {
//echo '<pre>';
//var_dump($this->news_model->get_news());
//exit();
$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) {
//echo '<pre>';
//var_dump($this->news_model->get_news($slug));
//exit();
$data['news'] = $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');
}
}
im still a beginner so my debugging solutions are limited.
$data['news'] = $this->news_model->get_news($slug);
should be
$data['news_item'] = $this->news_model->get_news($slug);
according to the rest of your code.
don't use $slug, the tutorial $slug set true when you insert data from set_news but i don't understand why the attribute type varchar slug can save boolean type. Here the code to set variable $slug and all attribute to db. Here the code of controller news
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);
}
edit the code of method view to
public function view($slug = NULL)
{
$data['news'] = $this->news_model->get_news();
//echo print_r($data['news_item']['0']['title'], true);
if (empty($data['news']))
{
show_404();
}
$data['title'] = $data['news']['0']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
and edit the views/news/view.php to
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p>View article</p><?php endforeach; ?>

CodeIgniter Tutorial Not Working

I'm working on the second half of the CodeIgniter beginners tutorial which can be found at http://www.codeigniter.com/userguide3/tutorial/news_section.html and like many others I can't get it to work properly.
The post index works fine but every time I click on "view article" I just get a 404 message.
I've looked over a number of posts about this and tried a number of solutions that were offered but so far nothing has worked which is driving me slightly crazy considering I copied and pasted the code from the tutorial to make sure I didn't copy it wrong by mistake.
My code is as follows:
Controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
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'] = $this->news_model->get_news($slug);
if (empty($data['news']))
{
show_404();
}
$data['title'] = $data['news']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
Model:
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();}}
View:
view.php
echo '<h2>'.$news['title'].'</h2>';
echo $news['text'];
index.php
<h2><?php echo $title ?></h2>
<?php foreach ($news as $news): ?>
<h3><?php echo $news['title'] ?></h3>
<div class="main">
<?php echo $news['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
routes.php
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Thanks in advance!
Did you managed to work it out?
What is the href in the anchor provides you with? I believe you should use the site_url() method to fix it, so the anchor should be:
<p>View article</p>
I swapped the

CodeIgniter News Tutorial delete

I am using CodeIgniter and have completed the news tutorial. I am trying to add a delete and update button within the news section. I have seen your post on stackoverflow about the delete button on this tutorial but i still cant get it working.
Here is my coding that i have used from your post
news_model.php
<?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);
}
public function delete_news($id) {
$this->db->delete('news', array('id' => $id));
}
}
CONTROLLER - news.php
<?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)
{
$data['news'] = $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');
}
}
public function delete($id) {
$this->news_model->delete_news($id);
$this->load->helper('url');
redirect('/www.shelim786.co.uk/CodeIgniter/news');
}
}
VIEWS - index.php
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<p>delete article</p>
<?php endforeach ?>
and ROUTING
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news/delete/(:any)'] = 'news/delete/$1';
The problem i am having is that the news article in www.shelim786.co.uk/CodeIgniter/index.php/news
doesnt delete when i click the delete button.
i would really appreciate if you could help me.
Thanks,
Shelim
You don't have slash after the word 'delete' in the link. Your link gets displayed something like http://www.shelim786.co.uk/CodeIgniter/index.php/news/delete 6. Notice the space after 'delete'?
In your view, change the 'delete article' link line to this.
<p>delete article</p>
Notice the slash after 'delete'?
you should redirect your delete link delete_news function
not delete function as the delete query is written there

Categories