Create insert, edit, update and delete data in mongoDB with codeigniter

View: 601    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Codeigniter   Fields: Other

10 point/2 review File has been tested

How to install up MongoDB and PHP on Ubuntu.After creating connection with mongoDB in codeigniter read this tu

Introduction

How to install up MongoDB and PHP on Ubuntu.After creating connection with mongoDB in codeigniter read this tutorial

How to connect mongoDB with codeigniter .Once you are connected create three files in the view folder to with name addPost.php (To add the post) , viewPost.php (To view the post) and editPost.php(To edit and update the post) copy and paste the followig code addPost.php

<div class="nav">
<ul class="select"><li><a href="<?php echo site_url('post');?>"><b>Add Post</b></a></li>
<li><a href="<?php echo site_url('post/viewPost');?>"><b>View Post</b></a></li>
</ul>
</div>
<form method = "POST" action = "<?php echo site_url('post/addPostData');?>" enctype= 'multipart/form-data'>
<table border="0" cellpadding="0" cellspacing="0" id="id-form">
<tr>
<th valign="top">Title:</th>
<td><input type="text" name="post_title" class="inp-form" /></td>
<td></td>
</tr>
<tr>
<th valign="top">Description:</th>
<td><textarea rows="10" cols="40" class="form-textarea" name="post_desc"></textarea></td>
<td></td>
</tr>
<tr>
<th valign="top">catagories:</th>
<td><select name="post_catag">
<option>Select</option>
<option value = "Catagory1" >Catagory1</option>
<option value = "Catagory2" >Catagory2</option>
</select></td>
</tr>
<tr>
<tr>
<th valign="top">Add Image:</th>
<td> <input type="file" name="userfile[]" id="file" multiple></td>
</tr>
<tr>
<th>&nbsp;</th>
<td valign="top">
<input type="submit" value="submit" />
</td>
<td></td>
</tr>
</table>
</form>

copy and paste the following code viewPost.php

<div class="nav">
<ul class="select"><li><a href="<?php echo site_url('post');?>"><b>Add Post</b></a></li>
<li><a href="<?php echo site_url('post/viewPost');?>"><b>View Post</b></a></li>
</ul>
</div>
<table border="0" width="" cellpadding="0" cellspacing="0" id="product-table">
<tr>
<th class="table-header-repeat line-left minwidth-1"><a href="">Title</a></th>
<th class="table-header-repeat line-left minwidth-1"><a href="">catagories</a></th>
<th class="table-header-repeat line-left minwidth-1"><a href="">Feature Image</a></th>
<th class="table-header-repeat line-left"><a href="">Date</a></th>
<th class="table-header-options line-left"><a href="">Action</a></th>
</tr>
<?php
$count = count($user);
for($i = 0;$i< $count;$i++)
{?>
<tr style="border:1px solid black">
<td style="border:1px solid black"><?php echo $user[$i]['title'];?></td>
<td style="border:1px solid black"><?php echo $user[$i]['catagories'];?></td>
<td style="border:1px solid black">
<?php
$image = $user[$i]['featureImage'];
$explode = explode(',',$image);
?>
<img src="<?php echo base_url('upload');?>/<?php echo $explode[0]; ?>" height="80px" width="80px"></td>
<td style="border:1px solid black"><?php echo $user[$i]['saved_at'];?></td>
<td style="border:1px solid black"><a href="<?php echo site_url('post/editPost');?>/<?php echo $user[$i]['_id'];?>">Edit</a>
<a href="<?php echo site_url('post/deletePost');?>/<?php echo $user[$i]['_id'];?>">Delete</a></td>
</tr>
<?php } ?>
</table>

copy and paste the followig code editPost.php

<div class="nav">
<ul class="select"><li><a href="<?php echo site_url('post');?>"><b>Add Post</b></a></li>
<li><a href="<?php echo site_url('post/viewPost');?>"><b>View Post</b></a></li>
</ul>
</div>
<form method = "POST" action = "<?php echo site_url('post/updatePost');?>">
<table border="0" cellpadding="0" cellspacing="0" id="id-form">
<tr>
<th valign="top">Title:</th>
<td><input type="hidden" name="post_id" class="inp-form" value = "<?php echo $user[0]['_id'] ;?>"/>
<input type="text" name="post_title" class="inp-form" value = "<?php echo $user[0]['title'] ;?>"/>
</td>
<td></td>
</tr>
<tr>
<th valign="top">Description:</th>
<td><textarea rows="5" cols="20" class="form-textarea" name="post_desc" ><?php echo $user[0]['description'] ;?></textarea></td>
<td></td>
</tr>
<tr>
<th valign="top">catagories:</th>
<td><select name="post_catag">
<option value="<?php echo $user[0]['catagories'] ;?>"><?php echo $user[0]['catagories'] ;?></option>
<option value="Clothes">Clothes</option>
<option value="Sports">Sports</option>
</select></td>
<td></td>
</tr>
<tr>
<th>&nbsp;</th>
<td valign="top">
<input type="submit" value="Update" class="form-submit" />
</td>
<td></td>
</tr>
</table>
</form>

Now create a controller name Post.php and copy and paste the following code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('upload');
}
public function index()
{
$data['catagory'] = $this->mongo_db->get('catagoryCollection');
$this->load->view('addPost',$data);
}
public function addPostData()
{
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);
$data = array('title' => $this->input->post('post_title'),
'description' => $this->input->post('post_desc'),
'catagories' => $this->input->post('post_catag'),
'saved_at' => date('d-m-Y'),
'featureImage'=> $fileName
);
$this->mongo_db->insert('postCollection',$data);
redirect(site_url('post/viewPost'));
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
public function viewPost()
{
$data['user'] = $this->mongo_db->get('postCollection');
$this->load->view('viewPost',$data);
}
public function editPost($id)
{
// echo $id;
$data['user'] = $this->mongo_db->get_where('postCollection',array('_id' => new \MongoId($id)));
$this->load->view('editPost',$data);
}
public function deletePost($id)
{
$id = new MongoId($id);
$this->mongo_db->where(array('_id'=> $id))->delete('postCollection');
redirect(site_url('post/viewPost'));
}
public function updatePost()
{
$id = $this->input->post('post_id');
$data = array('title' => $this->input->post('post_title'),
'description' => $this->input->post('post_desc'),
'catagories' => $this->input->post('post_catag'),
'saved_at' => date('d-m-Y')
);
$this->mongo_db->where('_id', new MongoId($id))->set($data)->update('postCollection');
redirect(site_url('post/viewPost'));
}
}

We will use the controller to insert, update, select and delete the data using mongoDB.
Now run your post controller and add edit and update your post.
Hope this small tutorial how to insert, edit, update and delete data in mongoDB with codeigniter will help you.

For more on mongoDB read this tutorial How to create collection and insert, delete and update data in mongoDB
Fell free to ask any question on mongoDB and codeigniter.

Create insert, edit, update and delete data in mongoDB with codeigniter

Create insert, edit, update and delete data in mongoDB with codeigniter Posted on 08-01-2016  How to install up MongoDB and PHP on Ubuntu.After creating connection with mongoDB in codeigniter read this tu 5/10 601

Comment:

To comment you must be logged in members.

Files with category

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd