Friday, June 3, 2016

jQuery Extension

(function ($) {
    $.fn.serializeFormJSON = function () {

        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

Friday, February 12, 2016

Mysqli

<?php
class BaseModel {

protected $conn;
public function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$this->conn = mysqli_connect($servername,$username,$password);


if($this->conn->connect_error)
{
die("Connection failed: ".$this->conn->connect_error);
}

mysqli_select_db($this->conn,"crud") or die("error to select db");
}

private function query($sql)
{
return mysqli_query($this->conn, $sql);
}

public function insert_by_tbl($table_name, $arr_data = array())
{
$arr_sql_field = array_keys($arr_data);
$arr_sql_val = array_values($arr_data);
$sql  = "INSERT INTO ".$table_name." (".implode(",",$arr_sql_field).") VALUES('".$arr_sql_val."')";
$this->query($sql);
}

public function update_by_tbl($table_name, $arr_data = array(), $wh_query)
{
$arr_sql_set = array();
foreach($arr_data as $field_name=>$field_val)
{
$arr_sql_set[] = $field_name."=".$field_val;
}
$sql  = "UPDATE ".$table_name." SET ".implode(",",$arr_sql_set)." WHERE 1 AND ".$wh_query;
return $this->query($sql);
}
public function get_by_tbl($table_name, $wh_query="" , $select_field = "*")
{
$sql  = "SELECT ".$select_field." FROM  ".$table_name." WHERE 1 ";
if(!empty($wh_query))
{
$sql .= $wh_query;
}
$res =  $this->query($sql);
return $arr_data = mysqli_fetch_all($res,MYSQLI_ASSOC);
while($row = mysqli_fetch_assoc($res))
{
$arr_data[] = $row;
}
return $arr_data;

}

}


======================================


<?php
require_once(__DIR__."/BaseModel.php");

class ContactModel Extends BaseModel{
public function get()
{
return parent::get_by_tbl("tbl_contact");
}
}



==========================================
<?php

$action = !empty($_GET["action"]) ? $_GET["action"] : "list";
$pk = !empty($_GET["pk"]) ? $_GET["pk"] : "";
require_once(__DIR__."/Model/ContactModel.php");
$obj_contact = new ContactModel();

if($action == "list")
{
$contact_res = $obj_contact->get();
echo "<pre>";
print_r($contact_res);
}



Map

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.google-map {
  height: 300px;
  width: 100%;
  border: 1px solid lightgray;
}
</style>
</head>
<body>

<div class="row">
  <!-- Hidden fields to hold the latitude and longitude -->
  <input type="text" name="latitude" id="latitude" value="13.0839" />
  <input type="text" name="longitude" id="longitude" value="80.2700" />
  <!-- GMap Container -->
  <div class="google-map" id="map"></div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

//var defaultLatLng = new google.maps.LatLng(13.0839, 80.2700);

var defaultLatLng = new google.maps.LatLng(document.getElementById("latitude").value, document.getElementById("longitude").value);
// Get the map container element
    var mapContainer = document.getElementById("map");

// Set the default options for the map
var myOptions = {
 zoom: 8,
 center: defaultLatLng
};

var map = new google.maps.Map(mapContainer, myOptions);

var marker = new google.maps.Marker({
 draggable: true, // Boolean, to set the draggable action to true.
 position: defaultLatLng, // The default latitude and longitude object.
 map: map, // The Map Object that we created.
 title: "Drag to your Location"
});
google.maps.event.addListener(marker, 'dragend', function(event) {
 document.getElementById("latitude").value = this.getPosition().lat();
 document.getElementById("longitude").value = this.getPosition().lng();

  $("#latitude").trigger("change");
  //$("#longitude").trigger("change");
});
});

$("#latitude , #longitude").change(function(){
jQuery.ajax({
url: 'save.php',
type: 'post',
data: 'latitude=' + $("#latitude").val() + '&longitude=' +  $("#longitude").val(),
success: function(results){
alert(results);
}
});
});





 </script>

</body>
</html>