I'm following the Codeigniter tutorial: http://localhost:8080/myproject/user_guide/tutorial/news_section.html
When i point my browser to http://localhost:8080/myproject/news as the tutorial indicates at the final of the section, "Point your browser to your document root, followed by index.php/news and watch your news page." appears a blank page.
I tried to point to localhost:8080/index.php/news or /myproject/news but occurs the same problem.
Also i tried to set in autoload.php the next: $autoload['libraries'] = array('database');
as is indicated in codeigniter news section tutorial not working but not solve, so i leave it like this: $autoload['libraries'] = array('');
This is the routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The News.php:
<?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();
}
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');
}
}
the view.php:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
and the 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();
}
}
I expect to watch the news page
Related
Second EDIT - Found the Problem, and answered it as well.
First Edit - added my Post_model.php file as well for clearer explanation of my code.
I am trying to redirect a Blogs slug to a separate page where I can show the entire blogs content.
here's an example slug
http://localhost/aag/posts/test-one
Here's the Posts controller
<?php
class Posts extends CI_Controller {
public function index(){
// Shows a blog listing
}
public function view($slug = NULL){
$data['post'] = $this->post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
}
The posts/view.php file
<h2><?php echo $post['title']; ?></h2>
<small class="post-date">Created on <?php echo $post['created_at']?></small><br>
<div class="post-body">
<?php echo $post['body']; ?>
</div>
the Post_model.php
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => '$slug'));
return $query->row_array();
}
}
routes.php
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['(:any)'] = 'pages/view/$1';
Okay, so years of writing mysqli queries made me do this mistake. In my Post_model.php I am getting the data where (DB column field) slug should match the $slug that gets passed in, but I surrounded the slugs inside single quotes that was causing the error. Here's the working code now.
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = NULL)
{
if ($slug === NULL) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
}
TLDR: Don't encapsulate your passed in arguments inside quotes.
I'm trying to load a specific page within a controller. I followed the Codeigniter tutorial and the main pages work but the individual page (loaded with view) doesn't load according to the given slug.
blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/index
* - or -
* http://example.com/index.php/Index/index
* - or -
*/
function __construct()
{
parent::__construct();
$this->load->model('blog_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['post'] = $this->blog_model->get_posts();
$data['title'] = 'Blog archive';
$this->load->view('header', $data);
$this->load->view('blog', $data);
$this->load->view('footer', $data);
}
public function view($slug = NULL)
{
$data['post'] = $this->blog_model->get_posts($slug);
if (empty($data['post']))
{
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('header', $data);
$this->load->view('post', $data);
$this->load->view('footer', $data);
}
}
blog_model.php
<?php
class Blog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE)
{
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
$query = $this->db->get();
return $query->result_array();
}
$this->db->select('*');
// $this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
// $this->db->where('slug', $slug);
$query = $this->db->get_where('blog_posts', array('slug' => $slug));
return $query->row_array();
}
}
As you can see I tried a few combinations because I'm not sure it's retrieving the table in get_posts when slug is not false.
try like this
call url_helper helper in __construct like following
$this->load->helper('url');
Now, update the routes.php like following
If your URL like
http://www.example.com/blog/view/slug
Your ruote should be like this
$route['blog/view/(:any)'] = 'blog/view/$1';
If your URL like
http://www.example.com/view/slug
Your ruote should be like this
$route['view/(:any)'] = 'blog/view/$1';
And, your get_posts model function repeating queries, use it simply like below
public function get_posts($slug = FALSE){
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
if($slug){
$this->db->where(compact('slug'));
}
$query = $this->db->get();
return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
}
In the end I solved it by modifying the routes:
At first I had:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/$1';
So I changed it to:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/view/$1';
And changed the method name in blog.php from blog to view. "view" looks more standard than "blog" so I left it that way. In 'blog/view/$1' represents the controller, view the method and of course $1 is the first param. Actually if I try Blog/view/hello-world it works too.
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
I am building my first CodeIgniter website and I am conquering a problem that seems to be hard to solve..
When I click on a article to read it I keep getting a 404 page. ( not found )
the link that I get is as follows
index.php/articles/articlename
In my controller I have the following for the view part
public function view($slug) {
$data['article_item'] = $this->articles_model->get_article($slug);
if (empty($data['article_item']))
{
show_404();
}
$data['title'] = $data['article_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('articles/view', $data);
$this->load->view('templates/side', $data);
$this->load->view('templates/footer');
}
As you see it has to include the view.php in the articles directory
In my router I have the following
$route['articles/(:any)'] = 'articles/view/$1';
$route['articles/create'] = 'articles/create';
$route['(:any)'] = 'pages/view/$1';
$route['gallery'] = 'gallery';
$route['articles'] = 'articles';
$route['default_controller'] = 'home';
$route['404_override'] = '';
Just to be sure I have posted every relevant stuff here is the link that I create
Read more
and the model
<?php
class Articles_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_article($slug = FALSE) {
if ($slug === FALSE) {
$query = $this->db->get('articles');
return $query->result_array();
}
$query = $this->db->get_where('articles', array('slug' => $slug));
return $query->row_array();
}
}
I hope someone can tell me what I'm doing wrong here..
You are missing the function name in your link.
Your URL should be: index.php/articles/view/articlename
The first part in your URL after index.php is the name of your Class and the second part is the name of your function and then your input values
I try add example from http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html
to my site, this is code:
news_model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($art = FALSE)
{
if ($art === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('art' => $art));
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'),
'art' => $art,
'text' => $this->input->post('text'),
'image'=> $image
);
return $this->db->insert('news', $data);
}
}
?>
news_controller
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('main/open_news', $data);
}
public function view($art) {
$data['news_item'] = $this->news_model->get_news($art);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$this->load->view('main/open_one_news', $data);
}
}
open_news
<?php
$this->load->view('mains/header');
$this->load->view('main/news');
$this->load->view('mains/footer');
?>
news view
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
View article
<?php endforeach ?>
And when I click in View article
The page is not forwarding to concret page with news, only in link duplicate "news":
http://localhost/index.php/news/news/news/news/news/news
I dont know what is problem. But I think it will by may in routes.config
Because I have only:
$route['default_controller'] = 'login'; -> this is my start page
But in CodeIgniter tutorial is:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
But when I add some from 3 first line, even the first page with list of news doesn`t open.
SOLVED:
I make stupid mistake. Because controller name news, but function: public function view($art), and the link should be: 'news/view/'.$news_item['art'].
I think problem with the below link
View article
use codeigniter anchor instead
anchor('news/'.$news_item['art'],'View article');
try this and feed me back
You forgot the echo at:
View article
You should use:
View article
or:
<?php echo anchor('news/' . $news_item['art'], 'View article');
or:
<?php echo anchor("news/{$news_item['art']}", 'View article');