Create News Item with CodeIgniter - php

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

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

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 error in adding functions

im creating my first site with codeigniter.
Im trying to put editing function and i have error:
Severity: Notice Message:
Undefined index: program
Filename: controllers/news.php Line Number: 21
here code of 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['program'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar');
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($id)
{
$data = array('news_item' => $this->news_model->get_news($id));
$data['news_item'] = $this->news_model->get_news($id);
$data['program'] = $data['news_item']['program']; // ERROR IS HERE
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar');
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
if (empty($data['news_item']))
{
show_404();
}
}
public function create(){
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Создать';
$this->form_validation->set_rules('program', 'Программа', 'required');
$this->form_validation->set_rules('code', 'Код', 'required');
$this->form_validation->set_rules('course', 'Направление', 'required');
$this->form_validation->set_rules('form', 'Форма обучения', 'required');
$this->form_validation->set_rules('time', 'Срок обучения', 'required');
$this->form_validation->set_rules('price', 'Стоимость', 'required');
$this->form_validation->set_rules('accreditation', 'Аккредитация', 'required');
$this->form_validation->set_rules('department', 'Кафедра', 'required');
$this->form_validation->set_rules('level', 'Уровень', 'required');
$this->form_validation->set_rules('type', 'Тип ОП', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar');
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar');
$this->load->view('news/formsuccess');
$this->load->view('templates/footer');
}
}
public function edit($id) { // передаем ид для редактирования
$this->load->helper(array('form', 'url'));
$this->load->library('validation');
if (!$this->input->post('id') && !$this->input->post('program')) {
$news_item = $this->news_model->get($id);
$this->validation->id = $news_item['id'];
$this->validation->program = $news_item['program'];
}
$data = array();
if ($this->validation->run() === FALSE)
{
$this->load->view('form', $data);
}
else
{
$data = array(
'program' => $this->input->post('program'),
'code' => $this->input->post('code'),
'course' => $this->input->post('course'),
'form' => $this->input->post('form'),
'time' => $this->input->post('time'),
'price' => $this->input->post('price'),
'accreditation' => $this->input->post('accreditation'),
'department' => $this->input->post('department'),
'level' => $this->input->post('level'),
'type' => $this->input->post('type')
);
$this->news_model->update($data);
redirect('/news');
}
}
}
Undefined index Error message is invoked when a variable or value for an array index is not defined.
try
var_dump($data['news_item']);
Does it print the data type as Object ? if its object
try
$data['program'] = $data['news_item']->program; // ERROR IS HERE
If these doesn't help, please post the output of the following line,
This will help everyone here to fix your problem
var_dump($data['news_item']);

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

CodeIgniter News Tutorial - Delete and Update

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();

Categories