Showing posts from December, 2015

Get last inserted id in codeigniter

You get last_id like this function add_post($post_data){$this->db->insert('posts',$post_data);$insert_id =$this->db->insert_id();return$insert_id;}OR for Specific table you cannot use $this->db->insert_id() . even the last insert happened long ago it can be fetched like this. may be wrong. but working well for me $this->db->select_max('{primary key}'); $result= $this->db

Remove last comma from string in php

$string = "Bar, Restaurant, Take Away Food, Dive School, Hotel,"; rtrim($string, ","); or You can use substr($string, 0, -1);

How to set and get session in Codeigniter

you can set data to session simply like this in controller $this->load->library('session'); $this->session->set_userdata(array('user_id'=> $user->uid,'username'=> $user->username,'groupid'=> $user->groupid,'date'=> $user->date_cr,'serial'=> $user->serial,'rec_id'=> $user->rec_id,'status'=> TRUE )); and you can get session data like this: $u_rec_id = $this->session->userdata('rec_id'); $serial = $this->session->userdata(

Add http before url in php

function addhttp($url){if(!preg_match("~^(?:f|ht)tps?://~i", $url)){ $url ="http://". $url;}return $url;}

Remove specifc value from array using jQuery

var myarray = ["Banana", "Orange", "Apple", "Mango"];
myarray.splice( 'Orange',myarray) ,1 );
Result:  ["Banana", "Apple", "Mango";


arr = [1, 2, 3, 4, 5] // array inicialvar removeItem = 2;   // item do array que deverá ser removido

arr = jQuery.grep(arr, function(value) {
        return value != removeItem;

// new array
// [1, 3, 4, 5]

check value exists in array in jquery

var fruits = ["Banana", "Orange", "Apple", "Mango"];
if($.inArray('Orange',fruits)!=-1){ // Found It.}

Get image height and width when choose file using jquery

<script type="text/javascript">
  function readURLL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                var image = new Image();
                image.src =;
                image.onload = function () {
                    //Determine the Height and Width.
                    var height = this.height;
                    var width = this.width;
                    if (height > 100 || width > 100) {
                        alert("Height and Width must not exceed 100px.");
                        return false;
                    return true;

           var v=  reader.readAsDataURL(input.files[0]);
<img class="i…

get image ratio using php

$image='Aye-Myat.jpg';//Image Path
list($originalWidth, $originalHeight) = getimagesize($image);
$ratio = $originalWidth / $originalHeight;
$aspect_ratio = round($ratio,2);

Reset form after call ajax using jquery

Use jQuery
$("#form_id")[0].reset(); or in plain JavaScript: document.getElementById("cform").reset();

Reverse an array in php without using the reverse function

<?php $array = array(1, 2, 3, 4); $size = sizeof($array); for($i=$size-1; $i>=0; $i--){ echo $array[$i]; } ?>

sort array without using sort function in php

Here is the way of sorting.
<?php $array=array('2','4','8','5','1','7','6','9','10','3'); echo "Unsorted array is: "; echo "<br />"; print_r($array);for($j =0; $j < count($array); $j ++){for($i =0; $i < count($array)-1; $i ++){if($array[$i]> $array[$i+1]){ $temp = $array[$i+1]; $array[$i+1]=$array[$i]; $array[$i]=$temp;}}} echo "Sorted Array is: "; echo "<br />"; print_r($array);?>OR$array1 = array(1,5,2,25,3); for($i=0; $i<count($array1); $i++) { for($j=0; $j<count($array1); $j++) { if($array1[$j] > $array1[$i]) { $temp=$array1[$i]; $array1[$i] = $array1[$j]; $array1[$j] = $temp; } } } print_r($array1);

Delete confirmation dialog on href-link?

<ahref="delete.php?id=22"onclick="return confirm('Are you sure you want to delete this?')">Delete</a>

get string before a specified character in jquery

If you like it short simply use a RegExp:
var streetAddress =/[^,]*/.exec(addy)[0];OR Try Thisvar newString =string.substr(0,string.indexOf(','));

get url segment in codeigniter

This provides you to retrieve information from your URI strings
$this->uri->segment(n);// n=1 for controller, n=2 for method, etc consider this example
it will return
$this->uri->segment(1);// controller $this->uri->segment(2);// action $this->uri->segment(3);// 1stsegment $this->uri->segment(4);// 2ndsegment

add one array to another in php

use array_push()  to add one or more elements onto the end of array.  Example: <?php
$stack = array("orange", "banana");array_push($stack, "apple", "raspberry");print_r($stack);?>
The above example will output: Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )

session_id() regenerated on refresh in php

First of all, you set your session lifetime to 10 seconds, which means that you get a new session after every 10 seconds.
Side note: It's normal behaviour for some browsers to discard session cookies when closing the browser.
If you need your session to expand over multiple browser sessions, you need to use persistent cookies.
function init_session(/* ... */){if(!isset($_SESSION)){ session_start();}//Is it a running session?if(isset($_SESSION['somevalue'])){//Everything is fine, session is loaded, no need to reload from cookies}else{if(isset($_COOKIE['yourcookiename'])){//reload session from cookie}else{ create_session();}}}function create_session(){ $_SESSION['somevalue']=1;//setcookie}

how to set default timezone in codeigniter?

To resolve this issue you have to set default time zone in index.php of root directory


Difference Between mysql and mysqli?

“mysqli” extension is an improvement over the old “mysql” extension. In fact, the “i” in mysqli stands for “improved”.
mysqli was developed to take advantage of the new features available in MySQL since version 4.1.3. Also, you can use it since PHP 5. So, if you code with PHP5 with a newer version of MySQL, it’s strongly recommended for you to use the extension “mysqli” over “mysql”.
Key benefits of mysqli over mysql extension Object-oriented interface. You can still use the "old procedural" way of calling the mysql extension but the OO version groups the functions by their purpose.Prepared Statements. Those are useful to prevent SQL injections and are executed faster.Multiple Statements. With this "feature", you can execute multiple SQL queries inside only one "mysqli" call. This reduces the round trips between the database server and the PHP server.Support for Transactions. This is really useful to write robust applications. It gives you the …

Get Selected data from select2 and call ajax

$('#AdvertID').on("select2:select", function(e) {
            var selectedData = $('#AdvertID').select2('data')[0];// what you would like to happen
                    type: "POST",
                    url : "Your URL",
                    data: {id:'5',name:'test'},
                    catch: false,
                    success: function(data)