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>
Related
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
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]
I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.
The data still insert to database despite the same name.
This is the controller code
public function add(){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('add_pbk_g', $data);
}
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));
$this->load->model('mpbk_grup');
if($this->mpbk_grup->check_grup_exist('$Name')){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('layout/header', $title);
$this->load->view('add_pbk_g');
$this->load->view('layout/footer');
} else{
$this->mpbk_grup->add;
redirect('cpbk_grup/index');
}
}
}
this is the Model..
function add($data){
return $this->db->create('pbk_groups', $data);
}
function check_grup_exist($namegroup){
$this->db->where('Name', $namegroup);
$this->db->from('pbk_groups');
$query = $this->db->get();
if($query->num_rows() >0){
return $query->result();
} else {
return $query->result();
//return False;
}
}
and this is the view
<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
<div class="box-body">
<?php echo validation_errors(); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Group</label>
<div class="col-sm-10">
<input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
Kembali
</div>
<!-- /.box-footer -->
</form>
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$data = array ('Name' => $this->input->post('namegroup'));
$this->mpbk_grup->check_grup_exist($data);
}
}
Where does $Name on this line come from?
if($this->mpbk_grup->check_grup_exist('$Name')){
Also it should not have quotes around it, should be...
if($this->mpbk_grup->check_grup_exist($Name)){
Try doing this inside save function
Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);
My Model
Model_Name
$this->main_table = 'pbk_groups';
public function check_grup_exist($data)
{
$query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0)
{
$this->db->insert($this->main_table, $data);
return $this->db->insert_id();
}
else
{
return false;
}
Checking duplication data before inserting the data. Let me know if any query occurs.
My Controller Codes
Looking forward for your help! thank you :
public function index()
{
$registration = $this->Registration_model->get_all();
$data = array(
'registration_data' => $registration
);
$this->load->view('registration/registration_list', $data);
}
public function do_upload()
{
$config['upload_path'] = './uploads/pictures/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000000;
$config['max_width'] = 10240000;
$config['max_height'] = 76800000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('Image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('registration_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
return $data['upload_data']['file_name'];
}
}
public function read($id)
{
$row = $this->Registration_model->get_by_id($id);
if ($row) {
$data = array(
'id' => $row->id,
'firstName' => $row->firstName,
'lastName' => $row->lastName,
'gender' => $row->gender,
'address' => $row->address,
'dob' => $row->dob,
'Image' => $row->Image,
);
$this->load->view('registration/registration_read', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('registration'));
}
}
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('registration/create_action'),
'id' => set_value('id'),
'firstName' => set_value('firstName'),
'lastName' => set_value('lastName'),
'gender' => set_value('gender'),
'address' => set_value('address'),
'dob' => set_value('dob'),
'Image' =>set_value('image'),
);
$this->load->view('registration/registration_form', $data);
}
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$data = array(
'firstName' => $this->input->post('firstName',TRUE),
'lastName' => $this->input->post('lastName',TRUE),
'gender' => $this->input->post('gender',TRUE),
'address' => $this->input->post('address',TRUE),
'dob' => $this->input->post('dob',TRUE),
'Image' => $this->input->post('Image',TRUE),
);
$this->Registration_model->insert($data);
$this->session->set_flashdata('message', 'The New Record was Successfuly Added');
redirect(site_url('registration'));
}
}
My Views Codes
<div class="form-group">
<label for="date">Dob <?php echo form_error('dob') ?></label>
<input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" />
</div>
<div class="form-group">
<label for="varchar">Image <?php echo form_open_multipart('upload/do_upload');?></label>
<input type="file" class="form-control" name="Image" id="Image" height="50" />
<br/>
</div>
<br/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button>
My Model
class Registration_model extends CI_Model
{
public $table = 'registration';
public $id = 'id';
public $order = 'DESC';
function __construct()
{
parent::__construct();
}
// get all
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
// get data by id
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
// get total rows
function total_rows($q = NULL) {
$this->db->like('id', $q);
$this->db->or_like('firstName', $q);
$this->db->or_like('lastName', $q);
$this->db->or_like('gender', $q);
$this->db->or_like('address', $q);
$this->db->or_like('dob', $q);
$this->db->or_like('Image', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
// get data with limit and search
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('id', $q);
$this->db->or_like('firstName', $q);
$this->db->or_like('lastName', $q);
$this->db->or_like('gender', $q);
$this->db->or_like('address', $q);
$this->db->or_like('dob', $q);
$this->db->or_like('Image', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
// insert data
function insert($data)
{
$this->db->insert($this->table, $data);
}
// update data
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
// delete data
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
Hello You can try this
$config['upload_path'] = 'ADD HERE YOUR SITE PATH TO PICTURE'; // Like SITE_PATH .'/uploads/pictures/';
and print $config['upload_path'] and check it's correct your upload picture path
I've had the same issue before. Depending on your server, you may want to remove the ./ from your upload path.
public function do_upload()
{
$config['upload_path'] = 'uploads/pictures/';
Change your view as below. You are not closing form
<?php echo form_open_multipart('upload/do_upload');?>
<div class="form-group">
<label for="date">Dob <?php echo form_error('dob') ?></label>
input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" />
</div>
<div class="form-group">
<label for="varchar">Image </label>
<input type="file" class="form-control" name="Image" id="Image" height="50" />
<br/>
</div>
<br/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button>
<?= form_close()?>
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