CodeIgniter News Tutorial - Delete and Update - php

I am new to CodeIgniter, and I have followed the tutorial to make a news application. I am now on deleting and updating the news article but I can't figure out how to do it.
This is what I have come up with so far, but it doesn't work because when I click delete article in index all that happens is that I get an error message saying : webpage cannot be found.
news controller :
<?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('/news');
}
}
news_model:
<?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));
}
}
index :
<?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 ?>

you have to use absolute path
delete article
also in your controller after delete
instead of
redirect('/news');
use absulot path or you can simply write
$this->index();

Related

CodeIgniter Form is not Submitting

I am new to php and I'm trying to develop a simple project using CodeIgniter php framework. I'm following the tutorial in CodeIgniter website.
My problem is I have create a model,view and controller as the tutorial. But my create form is not submitted when I click the submit button. other functions are working correctly.
I followed below tutorial:
Tutorial
Thanks in advance.
View
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
Model
<?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);
}
}
Controller
<?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();
$data['title'] = 'Title passes from conttoller';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
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');
}
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');
}
}
}
Routes:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
If you're new with Codeigniter, please dont play with "config/routes.php"
create form is not submitted when clicked the submit button? HTML form should submitted the form by default (browser will refresh)
Please check in the "Inspect Element > Network" at your browser before you click the form, and see what is going on

Create News Item with CodeIgniter

I've followed the documentation, but when I try to create a news item, I get the error:
"This page isn't working www.mi-linux.wlv.ac.uk didn’t send any data.
ERR_EMPTY_RESPONSE"
My base url:
$config['base_url'] = 'http://www.mi-linux.wlv.ac.uk/1043809/CodeIgniterNew/';
My Routes:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
My News_model.php File:
<?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);
}
}
My database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '1403809',
'password' => 'IhaveApassword',
'database' => 'db1403809',
'dbdriver' => 'mysqli',
Here is my controller, I have two other files in the controllers folder, one called pages.php and another called Welcome.php
Controller 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();
$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_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');
}
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->index();
}
}
}
Well I guess it's your server which is down:
Just to make sure it is not your code which is troublesome get a local PHP server and run it in local host. Say WAMPP on Windows or XAMPP on Mac / Linux / Windows. They are free

Undefined variable in foreach loop in Codeigniter

I am a beginner in codeigniter and I am making a basic crud for learning. I am facing an error which is in index.php
<h2><?=$title?></h2>
<?php foreach($posts as $post)?>
<h3><?php echo $post['title'];?></h3>o
<small class="post-date">Posted on:<?php echo $post['created at'];?> </small><br>
<?php echo word_limiter($post['body'],60);?>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
<?php endforeach;?>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: posts
Filename: posts/index.php
Line Number: 2
Backtrace:
File: /opt/lampp/htdocs/codeigniter/application/views/posts/index.php
Line: 2
Function: _error_handler
File: /opt/lampp/htdocs/codeigniter/application/controllers/Posts.php
Line: 23
Function: view
File: /opt/lampp/htdocs/codeigniter/index.php
Line: 315
Function: require_once
my controller is
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$data['posts']=$this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
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');
}
public function create(){
$data['title'] = 'create post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','body','required');
if($this->form_validation->run() === false) {
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
} else {
$this->Post_model->create_post();
redirect('posts');
}
}
public function delete($id) {
$this->Post_model->delete_post($id);
redirect('posts');
}
public function edit($id) {
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function update()
{
$this->Post_model->update_post();
redirect('posts');
}
}
my model file is
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = false){
if($slug === false) {
$this->db->order_by('id','DESC');
$query=$this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug' => $slug));
return $query->row_array();
}
public function create_post(){
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('posts',$data);
}
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
public function update_post() {
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
}
On your edit function on controller you have edit($id)
But where you load your model you have get_posts($slug) it should be get_posts($id)
And also change $data['post'] to $data['posts']
public function edit($id) {
$data['posts'] = array();
// rename slug to id
$data['posts'] = $this->post_model->get_posts($id);
if(empty($data['posts'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
/*
Tip You don't have to use index.php on for view you could create a
another view instead $this->load->view('posts/edit_view',$data);
*/
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
On the when load model on controller
Change
$this->Post_model->get_posts($id);
To
$this->post_model->get_posts($id);
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
On Autoload.php
Change
$autoload['model'] = array('Post_model');
To
$autoload['model'] = array('post_model');
And load database in autoload.php make life easier
$autoload['libraries'] = array('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 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