Friday, August 28, 2015

Simple and easiest Pagination in PHP with PDO

Pagination in 5 minute

Try out my function to achieve pagination


<?php
function data($limit_2=10){
$page=(!isset($_REQUEST['page'])?1:$_REQUEST['page']);
$limit_1=($limit_2*$page)-$limit_2;
$link=include 'config.php';
$data_set=array();
$sql=$sql_for_pagination="select * from clients order by id desc";
$sql.=' limit '.$limit_1.", ".$limit_2;
$query_c=$link->query($sql_for_pagination);
$total_records=$query_c->rowCount();

echo $sql.'<br />';
// fetch data
$query = $link->prepare($sql);
$query->execute();
if($query->rowCount()){
$total_pages = ceil($total_records / $limit_2);
$pagination='';
for($p=1;$p<=$total_pages;$p++){
$pagination.='<a href="?page='.$p.'">'.$p.'</a>&nbsp; &nbsp;';
}
$pagination.='</div>';
$data_set['pagination']=$pagination;
$data_set['total_data'] = $query->fetchAll(PDO::FETCH_ASSOC);
return $data_set;
}
else{
return 'no record found';
}
}
$data=data(5);
echo '<pre>';
print_r($data);
echo '</pre>';
?>

Thursday, August 27, 2015

Connect DataBase using PDO in PHP

While mysql_connect has been depreciate, there is only two way to connect database in php
1.  mysli_
2. PDO (the best and safest way).

below  is the code

<?php
define ('DB_HOST', "127.0.0.1");
define ('DB_USER', "root");
define ('DB_PASSWORD', "");
define ('DB_DATABASE', "database_name");
    try
    {
        $pdo = new PDO('mysql:host='. DB_HOST .';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'its connect';
    }
    catch (PDOException $e)
    {
$pdo='Error: '.$e->getMessage();
die('DB execution failed due to this error <br />'.$e->getMessage());
    }
//print_r($pdo);
return $pdo;

?>

Tuesday, August 25, 2015

get current url query string and change then redirect. (modifying URL in PHP)

lets say your current url is
abcd.com/index.php?r=employee_list&search=sudhir&l1=0&l2=5

where l1= limit 1
and  l2= limit 2

you want to make pagination and want to change the limit 1 value.

code

$_GET['l1']=6;
$queryString = http_build_query($_GET, '', '&');

now  your query is r=employee_list&search=sudhir&l1=6&l2=20

header ("Location: http://abcd.com/".$queryString ); // redirect to same page with new range

Sunday, August 23, 2015

how to find total number of record with limit parameter in mysql

#1          
  $stmt = $this->link->prepare("SELECT *,(SELECT COUNT(*) FROM clients) total_count                  FROM clients LIMIT 0, 10");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rowcount = $rows[0]['total_count'];
echo $rowcount; // return total number of row
                print_r($rows);  // return data



#2

$query = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE name LIKE 'a%' LIMIT 0,50");
$query_count = mysql_query("SELECT FOUND_ROWS()");
$result = mysql_fetch_array($query_count);
$total_num_rows = $result[0];

/* HERE IS YOUR CODE PARCING THE RESULTS OF MAIN QUERY */

#3 using PDO

 $query = $this->link->query("SELECT SQL_CALC_FOUND_ROWS * FROM clients LIMIT 0,50");
 $query_count = $this->link->query("SELECT FOUND_ROWS()");
 $result = $query_count->fetchAll(PDO::FETCH_ASSOC);
 $total_num_rows = $result[0];
 print_r($total_num_rows);

Wednesday, August 19, 2015

find any elemnts height and width in object formate using javascript

<code>
<script>
var ul = $('.TokensContainer');
var p=ul.outerWidth(Math.max(ul.width('').outerWidth()));
alert('height is - '+p[0]['clientHeight']);
console.log(p);
</script>
</code>
must check in console.log

Thursday, August 13, 2015

How to detect when that enter key is pressed inside a TextBox or somewhere else ?

How to detect when that enter key is pressed inside a TextBox or somewhere else ?




$('form input').live("keypress", function(e) {
        if (e.keyCode == 13) {
//put your logic here
             alert('enter key has been pressed inside the textbook');
            return false; // prevent the button click from happening
        }
});