Codeigniter error in adding functions - php

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']);

Related

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

Codeigniter - missing argument 1

I am having problems with my college assignments. I'm learning to make a update function trough codeigniter, then I got an error :
Missing argument 1 for Account::update()
When I press "Submit" button.
Controller :
class Account extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('cpanel/account/account_model');
}
public function update($nim) {
$this->form_validation->set_rules('nim', 'nim', 'required');
$this->form_validation->set_rules('nama', 'nama', 'required');
$this->form_validation->set_rules('sandi', 'sandi', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('telp', 'telp', 'required');
if ($this->form_validation->run() === FALSE) {
$data['akun'] = $this->account_model->detail();
$data['detail'] = $this->account_model->detail($nim);
$data = array('title'=> 'Mengubah Data Akun : '.$data['detail']['ortu_nama'], 'akun'=> $this->account_model->detail(), 'detail'=> $this->account_model->detail($nim), 'isi'=>'cpanel/account/account_edit_view');
$this->load->view('cpanel/layout/wrapper',$data);
} else {
$data = array(
'ortu_nim_mhs' => $this->input->post('nim'),
'ortu_nama' => $this->input->post('nama'),
'ortu_email' => $this->input->post('email'),
'ortu_telp' => $this->input->post('telp')
);
$this->account_model->update($data);
$konten = array('title'=>'Perubahan Data Sukses', 'isi'=>'cpanel/account/success_view');
$this->load->view('cpanel/layout/wrapper', $konten);
}
}}
Model :
class Account_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function update($data) {
$this->db->where('ortu_nim_mhs', $data['ortu_nim_mhs']);
return $this->db->update('user_ortu', $data);
}
public function detail($nim = FALSE) {
if ($nim === FALSE) {
$query = $this->db->get('user_ortu');
return $query->result_array();
}
$query = $this->db->get_where('user_ortu', array('ortu_nim_mhs'=>$nim));
return $query->row_array();
}}
Try $data = array(); in model function
public function update($data = array()) {
$this->db->where('ortu_nim_mhs', $data['ortu_nim_mhs']);
return $this->db->update('user_ortu', $data);
}
In your above code if you are submitting form with POST method than function argument is not required. Please check below.
public function update() {
// Your code
}
But in case if you submitting form with get method than you need to set data for validation first, like given below.
public function update() {
$this->form_validation->set_data($this->input->get());
$this->form_validation->set_rules('nim', 'nim', 'required');
$this->form_validation->set_rules('nama', 'nama', 'required');
$this->form_validation->set_rules('sandi', 'sandi', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('telp', 'telp', 'required');
// Your other code
}
This will help you. Let me know if it not works.

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

Fatal error: Call to a member function where() on a non-object in

I've been debugging this code but still not successful. Can anyone help me out please?
class Membership_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
I kept on getting an fatal error on the line
$this->db->where('username',$this->input->post('username'));
this is the controller/login.php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
The database probably is not being initialized properly before validate is called.
Looks like you're not connected to your DB. Make sure you are connecting to your database.
You can either connect automatically everytime your script runs or connect to the DB manually. Look at the CI guides for your connection options : http://codeigniter.com/user_guide/database/connecting.html
Could it be that the load->model( 'membership_model' ) should receive the name of a class, and that that name is case sensitive? You should probably check the return value of the API functions...

Categories