CodeIgniter Edit records buttond doesnt work - php

Started working with CodeIgniter, and when I press to edin posts I get error
"Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster."
I changed everything, starting from controller, model and view but nothing heppend and I dont know where I made mistake
Posts Controller
<?php
class Posts extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('upload');
}
public function index($offset = 0){
$config['base_url'] = base_url() . 'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['per_page'] = 3;
$config['uri_segment'] = 3;
$config['attributes'] = array('class' => 'pagination-link');
$this->pagination->initialize($config);
$data['posts']= $this->Posts_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($mjestoOdredista=NULL){
$data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
$post_id = $data['posts']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['posts'])){
show_404();
}
$data['id'] =$data['posts'];
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('templates/footer');
}
public function create(){
//check if user is logged in
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
$this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
$this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
$this->form_validation->set_rules('datumPovratka', 'Datum Odredista', 'required');
$this->form_validation->set_rules('cijena', 'Cijena', 'required');
$this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
// $this->form_validation->set_rules('opis', 'Opis', 'required');
// $this->form_validation->set_rules('post_image', 'Image', 'required');
$data['title'] ='Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if($this->form_validation->run()===FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
}else {
//upload image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->library('upload');
if(!$this->upload->do_upload()){
$error=array('error'=>$this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$data = array('upload_data'=>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created') ;
redirect('posts');
}
}
public function delete($id){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->delete_post($id);
$this->session->set_flashdata('post_deleted', 'You post has been deleted ') ;
redirect('posts');
}
public function edit($slug){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['mjestoOdredista']= $this->Posts_model->get_posts($slug);
//Check if user is logged in
if($this->session->userdata('user_id') != $this->Posts_model->get_posts($slug)['user_id']){
redirect('posts');
}
$data['categories'] = $this->Posts_model->get_categories();
if(empty($data['slug'])){
show_404();
}
$data['id'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit',$data);
$this->load->view('templates/footer');
}
public function update(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->update_post();
$this->session->set_flashdata('post_updated', 'You post has been updated ') ;
redirect('posts');
}
}
?>
Posts Model
<?php
class Posts_Model extends CI_Model{
public function __construct(){
$this->load->database();
}
function get_posts($mjestoOdredista=FALSE, $limit = FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($mjestoOdredista === FALSE){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get('posts');
return $query->result_array();
}
$query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
return $query->row_array();
}
//Kreiranje post
public function create_post($post_image){
$mjestoPolaska = url_title($this->input->post('title'));
$data=array(
'mjestoPolaska' => $mjestoPolaska,
'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
'datumPolaska' => $this->input ->post('datumPolaska'),
'datumPovratka' => $this->input ->post('datumPovratka'),
'brojMjesta' => $this->input ->post('brojMjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
return $this->db->insert('posts',$data);
}
//Brisanje posta
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
//editovanje posta
public function update_post(){
$slug = url_title($this->input->posts('title'));
$data=array(
'slug' => $slug,
'id'=> $this->input->posts('id'),
'category_id'=> $this->input->post('category_id'),
'user_id' => $this->session->userdata('user_id'),
'mjestoPolaska' => $this->input->posts('mjestoPolaska'),
'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
'datumPolaska' => $this->input ->post('datumPolaska'),
'datumPovratka' => $this->input ->post('datumPovratka'),
'cijena' => $this->input ->post('cijena'),
'brojMjesta' => $this->input ->post('brojMjesta'),
'opis' => $this->input ->post('opis'),
'post_image'=> $post_image
);
$this->db->where('id', $this->input->posts('id'));
return $this->db->update('posts', $data);
}
public function get_categories(){
$this->db->order_by('name');
$query = $this->db->get('categories');
return $query->result_array();
}
public function get_posts_by_category($category_id){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get_where('posts',array('category_id'=> $category_id));
return $query->result_array();
}
}
?>
Edit View
<h2><?= $title;?></h2>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
$( "#datepicker1" ).datepicker();
} );
</script>
<?php echo form_open_multipart('/posts/edit');?>
<?php echo validation_errors();?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label>Mjesto Polaska</label>
<input type="text" class="form-control" name="mjestoPolaska" placeholder="Mjesto Polaska">
</div>
<div class="form-group">
<label>Mjesto Odredista</label>
<input type="text" class="form-control" name="mjestoOdredista" placeholder="Mjesto Odredista">
</div>
<div class="form-group">
<label>Datum Polaska</label>
<input type="text" id="datepicker" class="form-control" name="datumPolaska" placeholder ="Datum Polaska" >
</div>
<div class="form-group">
<label>Datum Povratka</label>
<input type="text" id="datepicker1" class="form-control" name="datumPovratka" placeholder="Datum Povratka">
</div>
<div class="form-group">
<label>Cijena</label>
<input type="text" class="form-control" name="cijena" placeholder="Cijena">
</div>
<div class="form-group">
<label for="select">Broj slobodnih mjesta</label>
<select class="form-control" id="select", name="brojMjesta">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<div class="form-group">
<label for="Kategorije">Kategorija</label>
<div>
<select name="category_id" class="form_control">
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['name'];?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label>Postavi sliku:</label>
<p><b>samo oni koji nude prevoz nek postave sliku svojeg vozila</b></p>
<input type="file" name="userfile" size="20">
</div>
<div class="form-group">
<label>Opis:</label>
<textarea class="form-control" rows="5" id="comment" name="opis"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
</div>
.htaccess file
RewriteEngine on
RewriteCond $1 !^(index.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

replace your htaccess file with this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /CS/index.php/$1 [L,QSA]
</IfModule>
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /CS/index.php?$1 [L]

Related

CodeIgniter Image file cannot be upload

The one problem which I can not resolve in while, i tried every possible solution but I doesn't go well
Well, the problem is when I want to upload image, i select image from my comp and when I click submit button, image is not upload, and it's show me defaule image which i put in a case that user doesn't select image (noimage.jpg)
Function for upload image start in create() method
Posts Controller
<?php
class Posts extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('upload');
}
public function index($offset = 0){
$config['base_url'] = base_url() . 'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['per_page'] = 3;
$config['uri_segment'] = 3;
$config['attributes'] = array('class' => 'pagination-link');
$this->pagination->initialize($config);
$data['posts']= $this->Posts_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($mjestoOdredista=NULL){
$data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
$post_id = $data['posts']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['posts'])){
show_404();
}
$data['id'] =$data['posts'];
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('templates/footer');
}
public function create(){
//check if user is logged in
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
$this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
$this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
$this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
$this->form_validation->set_rules('cijena', 'Cijena', 'required');
$this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
$this->form_validation->set_rules('opis', 'Opis', 'required');
$data['title'] ='Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if($this->form_validation->run()===FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
}else {
//upload image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '100';
$config['max_height'] = '100';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$data = array('upload_data'=>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created') ;
redirect('posts');
}
}
public function delete($id){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->delete_post($id);
$this->session->set_flashdata('post_deleted', 'You post has been deleted ') ;
redirect('posts');
}
public function edit($mjestoOdredista){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['posts']= $this->Posts_model->get_posts($mjestoOdredista);
//Check if user is logged in
if($this->session->userdata('user_id') != $this->Posts_model->get_posts($mjestoOdredista)['user_id']){
redirect('posts');
}
$data['categories'] = $this->Posts_model->get_categories();
if(empty($data['posts'])){
show_404();
}
$data['title'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit',$data);
$this->load->view('templates/footer');
}
public function update(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->update_post();
$this->session->set_flashdata('post_updated', 'You post has been updated ') ;
redirect('posts');
}
}
?>
Posts_model
<?php
class Posts_Model extends CI_Model{
public function __construct(){
$this->load->database();
}
function get_posts($mjestoOdredista=FALSE, $limit = FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($mjestoOdredista === FALSE){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get('posts');
return $query->result_array();
}
$query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
return $query->row_array();
}
//Kreiranje post
public function create_post($post_image){
$mjestoPolaska = url_title($this->input->post('title'));
$data=array(
'mjestoPolaska' => $this->input->post('mjestoPolaska'),
'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
'datumPolaska' => $this->input ->post('datumPolaska'),
'datumPovratka' => $this->input ->post('datumPovratka'),
'brojMjesta' => $this->input ->post('brojMjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
return $this->db->insert('posts',$data);
}
//Brisanje posta
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
//editovanje posta
public function update_post(){
$mjestoOdredista = url_title($this->input->post('title'));
$data=array(
'category_id' => $this->input->post('category_id'),
'mjestoPolaska' => $this->input->post('mjestoPolaska'),
'mjestoOdredista' => $this->input->post('mjestoOdredista'),
'datumPolaska' => $this->input ->post('datumPolaska'),
'datumPovratka' => $this->input ->post('datumPovratka'),
'cijena' => $this->input ->post('cijena'),
'brojMjesta' => $this->input ->post('brojMjesta'),
'opis' => $this->input ->post('opis'),
);
$this->db->where('id', $this->input->post('id'));
return $this->db->update('posts', $data);
}
public function get_categories(){
$this->db->order_by('name');
$query = $this->db->get('categories');
return $query->result_array();
}
public function get_posts_by_category($category_id){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get_where('posts',array('category_id'=> $category_id));
return $query->result_array();
}
}
?>
Create View
<h2><?= $title;?></h2>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$("#datepicker").datepicker({
dateFormat: 'dd-mm-yy'
});
$("#datepicker1").datepicker({
dateFormat: 'dd-mm-yy'
});
});
</script>
<?php echo form_open_multipart('posts/create/');?>
<?php echo validation_errors();?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label>Mjesto Polaska</label>
<input type="text" class="form-control" name="mjestoPolaska" placeholder="Mjesto Polaska">
</div>
<div class="form-group">
<label>Mjesto Odredista</label>
<input type="text" class="form-control" name="mjestoOdredista" placeholder="Mjesto Odredista">
</div>
<div class="form-group">
<label>Datum Polaska</label>
<input type="date" id="datepicker" class="form-control" name="datumPolaska" placeholder ="Datum Polaska" >
</div>
<div class="form-group">
<label>Datum Povratka</label>
<input type="date" id="datepicker1" class="form-control" name="datumPovratka" placeholder="Datum Povratka">
</div>
<div class="form-group">
<label>Cijena</label>
<input type="text" class="form-control" name="cijena" placeholder="Cijena">
</div>
<div class="form-group">
<label for="select">Broj slobodnih mjesta</label>
<select class="form-control" id="select" name="brojMjesta">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<div class="form-group">
<label for="kategorije">Kategorija</label>
<?php
echo '<select class="form-control" id="kategorije" name="category_id">';
foreach($categories as $category) :
echo '<option value="' . $category['id'] . '">' . $category["name"] . '</option>';
endforeach;
echo '</select>';
?>
</div>
<div class="form-group">
<label>Postavi sliku:</label>
<p><b>samo oni koji nude prevoz nek postave sliku svojeg vozila</b></p>
<input type="file" name="userfile" size="20">
</div>
<div class="form-group">
<label>Opis:</label>
<textarea class="form-control" rows="5" id="comment" name="opis"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
</div>
if store data without image then
public function create() {
//check if user is logged in
if (!$this->session->userdata('logged_in')) {
redirect('users/login');
}
$this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
$this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
$this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
$this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
$this->form_validation->set_rules('cijena', 'Cijena', 'required');
$this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
$this->form_validation->set_rules('opis', 'Opis', 'required');
$data['title'] = 'Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if ($this->form_validation->run() === FALSE) {
$this->session->set_flashdata("error", validation_errors());
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
$post_image='';
if (!empty($_FILES['userfile']['name'])) {
$path = 'assets/images/posts/';
$post_image = $this->ImageUpload($path, 'userfile', '');
if (!is_array($post_image)) {
$this->session->set_flashdata('error', $post_image);
redirect(site_url() . 'posts/create');
}
$post_image = $post_image['file_name'];
}
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created');
redirect('posts');
}
}
Try This
Replace your create function with this code
public function create()
{
//check if user is logged in
if (!$this->session->userdata('logged_in')) {
redirect('users/login');
}
$this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
$this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
$this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
$this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
$this->form_validation->set_rules('cijena', 'Cijena', 'required');
$this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
$this->form_validation->set_rules('opis', 'Opis', 'required');
$data['title'] = 'Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if ($this->form_validation->run() === FALSE) {
$this->session->set_flashdata("error", validation_errors());
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
if (empty($_FILES['userfile']['name'])) {
$this->session->set_flashdata('error', "Please select image");
redirect(site_url() . 'posts/create');
}
$path = 'assets/images/posts/';
$post_image = $this->ImageUpload($path, 'userfile', '');
if (!is_array($post_image)) {
$this->session->set_flashdata('error', $post_image);
redirect(site_url() . 'posts/create');
}
$post_image = $post_image['file_name'];
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created');
redirect('posts');
}
}
public function ImageUpload($upload_path, $uploading_file_name, $file_name = '') {
$obj = &get_instance();
$obj->load->library('upload');
//echo $uploading_file_name.$file_name;
$file_name = 'image_' . time() . $file_name . "." . pathinfo($_FILES[$uploading_file_name]['name'], PATHINFO_EXTENSION);
$full_path = FCPATH . $upload_path;
// $full_path = $upload_path;// uncomment if you using windows os
// if folder is not exists then create the folder
//if (!is_dir($full_path)) {
// mkdir($full_path, 0775);
//}
$config['upload_path'] = $full_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
$config['max_size'] = 2048;
$config['max_width'] = 2048;
$config['max_height'] = 2048;
$config['file_name'] = $file_name;
$obj->upload->initialize($config);
if (!$obj->upload->do_upload($uploading_file_name)) {
return $obj->upload->display_errors();
} else {
return $obj->upload->data();
}
}
And Replace <?php echo validation_errors();?> to <?php echo $this->session->flashdata("error")?>
Or you can use layout library instead of calling header n footer [https://code.tutsplus.com/tutorials/how-to-create-a-layout-manager-with-codeigniter--net-15533]
FCPATH works fine in linux system only so check FCPATH if error come path is wrong

Codeigniter view is not loading

I am creating a simple login site(first step of the project) and I get error regardless of the information I fill in the username and password forms.
this is my code:
login.php
<?php
class login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
function login_control()
{
$data['title'] = "Login page";
$this->load->view("loginview", $data);
}
function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run())
{
//true
$username = $this->input->post('username');
$password = $this->input->post('password');
//model function
$this->load->model('server_model');
if($this->server_model->can_login($username, $password))
{
$session_data = array('username' => $username);
$this->session->set_userdata($session_data);
redirect(base_url() . 'login/enter');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url() . 'login/login_control');
}
}
else
{
//false
$this->login_control();
}
}
function enter()
{
if($this->session->userdata('username') != '')
{
echo 'Welcome - '. $this->session->userdata('username');
echo '<label>Logout</label>';
}
else
{
redirect(base_url().'login/login_control');
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect(base_url() . 'login/login_control');
}
}
server_model.php
<?php
class server extends CI_Model
{
function can_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows()>0)
{
return true;
}
else
{
return false;
}
}
}
loginview.php
<!DOCTYPE html>
<html>
<head>
<title>Login | <?php echo $title; ?></title>
<link rel ="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
<div class = "container">
<br /><br /> <br />
<form method="post" action="<?php echo base_url(); ?>login/login_validation">
<div class="form-group">
<label>Enter Username</label>
<input type="text" name="username" class="form-control" />
<span class="text-danger"><?php echo form_error('username'); ?>
</span>
</div>
<div class="form-group">
<label>Enter Password</label>
<input type="password" name="password" class="form-control" />
<span class="text-danger"><?php echo form_error('password'); ?>
</span>
</div>
<div class="form-group">
<input type="submit" name="insert" value="Login" class="btn btn-info" />
<?php
echo '<label class="text-danger">'.$this->session->flashdata("error").'</label>';
?>
</div>
</form>
</div>
</body>
</html>
My .htaccess file looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
my routes.php is like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'login/login_control';
$route['login/login_control'] = 'loginview';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I think the error is probably at my .htaccess or routes files but I have been trying to fix this for hours and I couldnt solve the problem.
Any help appreciated
So as I thought the problem was something really stupid of me. I am using Sublime Text so when I save a file I need to also add the extension .php which apparently I hadn't done. Thanks to everyone who tried on solving this problem although there wasn't any real problem out there.

Pressing Submit button post has been deleted CodeIgniter

I have problem while I'm trying to post comment in post, when I enter (name,email and body of comment) and pressing Submit button, my post has been deleted automaticaly. I am trying to find a error but can not see where I made a mistake
Post Controller
<?php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('Posts_model');
}
public function index($page='home'){
$data['posts']= $this->Posts_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($mjestoOdredista=NULL){
$data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
$post_id = $data['posts']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['posts'])){
show_404();
}
$data['id'] =$data['posts'];
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('templates/footer');
}
public function create(){
//check if user is logged in
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] ='Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if($this->form_validation->run()===FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
}else {
//upload image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->libary('upload', $config);
if(!$this->upload->do_upload()){
$error=array('error'=>$this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$data = array('upload_data'=>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created') ;
redirect('posts');
}
}
public function delete($id){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->delete_post($id);
$this->session->set_flashdata('post_deleted', 'You post has been deleted ') ;
redirect('posts');
}
public function edit($mjestoOdredista){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['mjestoOdredista']= $this->Posts_model->get_posts($mjestoOdredista);
//Check if user is logged in
if($this->session->userdata('user_id') !=$this->Posts_model->get_posts($mjestoOdredista)['user_id'])
redirect('posts');
$data['categories'] = $this->Posts_model->get_categories();
if(empty($data['mjestoOdredista'])){
show_404();
}
$data['id'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit',$data);
$this->load->view('templates/footer');
}
public function update(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->update_post();
$this->session->set_flashdata('post_updated', 'You post has been updated ') ;
redirect('posts');
}
}
?>
Post Model
<?php
class Posts_Model extends CI_Model{
public function __construct(){
$this->load->database();
}
function get_posts($mjestoOdredista=FALSE){
if($mjestoOdredista === FALSE){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get('posts');
return $query->result_array();
}
$query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
return $query->row_array();
}
//Kreiranje post
public function create_post($post_image){
$mjestoPolaska = url_title($this->input->post('title'));
$data=array(
'mjestoPolaska' => $mjestoPolaska,
'mjestoOdredista' => $this->input ->post('Mjesto Odredista'),
'datumPolaska' => $this->input ->post('Datum Polaska'),
'datumPovratka' => $this->input ->post('Datum Povratka'),
'brojMjesta' => $this->input ->post('Broj Mjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('Opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
return $this->db->insert('posts',$data);
}
//Brisanje posta
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
//editovanje posta
public function update_post(){
$mjestoPolaska=url_title($this->input->post('Mjesto Polaska'));
$data=array(
'mjestoPolaska' => $mjestoPolaska,
'mjestoOdredista' => $this->input ->post('Mjesto Odredista'),
'datumPolaska' => $this->input ->post('Datum Polaska'),
'datumPovratka' => $this->input ->post('Datum Povratka'),
'brojMjesta' => $this->input ->post('Broj Mjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('Opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
public function get_categories(){
$this->db->order_by('name');
$query = $this->db->get('categories');
return $query->result_array();
}
public function get_posts_by_category($category_id){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get_where('posts',array('category_id'=> $category_id));
return $query->result_array();
}
}
?>
View
<h2>Info o voznji:</h2>
<div class></div>
<div class="post-body">
<hr>
<?php echo form_open('/posts/delete/'.$posts['id']);?>
<?php if($this->session->userdata('user_id')==$posts['user_id']);?>
<a class="btn btn-default " href="posts/edit/">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
</div>
<hr>
<h3>Comments</h3>
<?php if($comments):?>
<?php foreach ($comments as $comment):?>
<div class="well">
<h5><?php echo $comment['body']; ?>[by <strong><?php echo $comment['name']?></strong>]</h5>
</div>
<?php endforeach;?>
<?php else : ?>
<p>No Comments to display</p>
<?php endif;?>
<hr>
<h3>Add Comment</h3>
<?php echo validation_errors();?>
<?php echo form_open('comments/create/'.$posts['id']); ?>
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control">
<div class="form-group">
<label>Email</label>
<input type="text" name="name" class="form-control">
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
Comment Controller
<?php
class Comments extends CI_Controller{
public function create($post_id){
$post_id = $this->input->post('post_id');
$data['post'] = $this->Posts_model->get_posts($post_id);
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run()===FALSE){
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('templates/footer');
}else {
$this->comment_model->create_comment($post_id);
redirect('posts/'.$slug);
}
}
}
?>
**Comment Model**
<?php
class Comment_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function create_comment($post_id = NULL){
$data=array(
'post_id'=>$post_id,
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'body' => $this->input->post('body'),
);
return $this->db->insert('comments',$data);
}
public function get_comments($post_id){
$query = $this->db->get_where('comments',array('post_id'=>$post_id));
return $query->result_array();
}
}
Let's take a look at the view:
<div class="post-body">
<hr>
<?php echo form_open('/posts/delete/'.$posts['id']);?>
<?php if($this->session->userdata('user_id')==$posts['user_id']);?>
<a class="btn btn-default " href="posts/edit/">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
</div>
Here are a few problems:
Your if-statement has a semicolon at the end, which essentially makes it a no-op.
You have two forms in your view, but only one </form> or form_close() call (and it's after the comment creation form). Essentially, it boils down to invalid HTML; pressing the Submit button will submit the first form (since it was never closed).
The below code addresses those problems:
<div class="post-body">
<hr>
<?php if ($this->session->userdata('user_id') == $posts['user_id']): ?>
<?= form_open('/posts/delete/' . $posts['id']) ?>
<a class="btn btn-default" href="/posts/edit/<?= $posts['id'] ?>">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
<?= form_close() ?>
<?php endif ?>
</div>

codeigniter and form submit

I am trying to get used to Codeigniter. I am sorry if that is a trivial or dumb question but I have been struggling to have the "News Section" of Codeigniter's tutorial work.
There is this form (from here)
<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>
which, according to this 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'] = 'My 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/', $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');
}
}
}
should, I think, if the validation returns ok, go ahead and insert the data into the db. Now, my problem is that the pages run under:
http://localhost/codeigniter/index.php/news/
The submit button, however, returns me to:
http://localhost/codeigniter/index.php/news/localhost/codeigniter/index.php/news/create
The routes.php file contains the following code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'news/view/$1';
$route['default_controller'] = 'news';
I don't know why this is happening. Thank you for any help.
Did you set your base_url config in application/config/config.php ?

insert and update in single form in CodeIgniter

Here I have 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'] = "News in Detail(s)";
$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 22';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$data['updateid'] = '' ;
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();
if($update == 1){
redirect('/news', 'location');
}
}
}
public function update($id){
$this->load->helper('form');
$this->load->library('form_validation');
$data['updateid'] = $id;
$data['title'] = "Update News in Detail(s)";
$data['update'] = $this->news_model->edit_load_data($id);
$this->load->view('news/create',$data);
if ($this->input->post('submit')) {
$update = $this->news_model->update_news($id);
if($update == 1){
redirect('/news', 'location');
}
}
}
}
?>
Here my model file 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')
'text' => $this->input->post('select')
);
return $this->db->insert('news', $data);
}
public function edit_load_data($id){
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
public function update_news($id){
$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')
'text' => $this->input->post('select')
);
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}
?>
Here my form create.php file:
<?php echo validation_errors();
echo $updateid;
?>
<?php if($updateid == ''){ ?>
<?php echo form_open('news/create'); ?>
<?php } else { ?>
<?php echo form_open('news/update/'.$updateid); ?>
<?php } ?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php if($update['text']=='text1'){ echo "selected";} ?> value="text1">text1</option>
<option <?php if( $update['text']=='text2'){ echo "selected";} ?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
Here when I am going news/create in gives error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: update
Filename: news/create.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\mylab\application\views\news\create.php Line: 15
Function: _error_handler
File: C:\xampp\htdocs\mylab\application\controllers\News.php Line: 54
Function: view
File: C:\xampp\htdocs\mylab\index.php Line: 315 Function: require_once
" />
What can I do for that? I want add and edit for same file create.php
is it possible there?
I don't know that this will produce the outcome you want but it will prevent the use of undefined variables.
<?php
echo validation_errors();
$updateid = isset($updateid) ? $updateid : '';
echo $updateid;
if($updateid == '')
{
echo form_open('news/create');
}
else
{
echo form_open('news/update/'.$updateid);
}
if(!isset($update))
{
$update['title'] = NULL;
$update['slug'] = NULL;
$update['text'] = NULL;
}
?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php
if($update['text'] == 'text1')
{
echo "selected";
}
?> value="text1">text1</option>
<option <?php
if($update['text'] == 'text2')
{
echo "selected";
}
?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
You should probably modify your model so that it returns a default array if/when the inputs are not present. I am not able to identify where you are sending $update to the view either.
I think you're not returning $update['title'], $update['text'], etc.. You are returning $title, $text...
Try changing $update['title'] to $title in create.php to see what
happens

Categories