My Main Blog

Saturday 13 December 2014

MPDF not inserting image in pdf

MPDF not inserting image in pdf


You may face this problem while working with MPDF when you generate pdf file. this is due to mpdf is not accept full url of image.
for example this will no work:
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCUFHmg4DCI4m29aa_GeLclII2Jkkt6MrPJs-QrzPVDQL2pgXcta3D4hCvGCuFvTWIilBExj4bRd-hAaTrRNSA05SGEAHqNGeITZsQChBvRIDxc4IKy8hl3FwTUKGeEZmLhk7eRVd8624/s1600/video-watermark.jpg"

to overcome this problem you need to download that image in your server and use relative path while generating pdf. for example if your site name is : http://agalaxycode.blogspot.in/
then you need to create a folder "images" and save different name image from download  "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCUFHmg4DCI4m29aa_GeLclII2Jkkt6MrPJs-QrzPVDQL2pgXcta3D4hCvGCuFvTWIilBExj4bRd-hAaTrRNSA05SGEAHqNGeITZsQChBvRIDxc4IKy8hl3FwTUKGeEZmLhk7eRVd8624/s1600/video-watermark.jpg". suppose you have downloaded and saved it with the name "aa.jpg" in "images" foler.
After that use this relative path in MPDF "images/aa.jpg".
MPDF uses relative path rather than absolute path

Wednesday 10 December 2014

mysqli stmt return result

How to return result from mysqli stmt prepare and bind_param statement.
Please note that you must install  MySQL Native Driver (mysqlnd) driver to use get_result();


DBClient.php

<?php
//error_reporting(E_ALL ^ E_DEPRECATED);
class DBClient {
var $host,$db,$user,$pass;
var $mysqli,$isDBConnect;
function __construct() {
    $this->host = "localhost";
    $this->db = "mydb";
    $this->user = "root";
    $this->pass = "pass";
    try {
          $this->mysqli = new mysqli($this->host, $this->user, $this->pass,$this->db );
        if ($this->mysqli->connect_error) {
            die('Error : ('. $this->mysqli->connect_errno .') '. $this->mysqli->connect_error);
        }
    } catch(Exception $ee) {
        $this->link=false;
           $this->isDBConnect=false;
    }
}

public function autocommit_false() {
    $this->mysqli->autocommit(FALSE);   
}

public function commit() {
    $this->mysqli->commit();   
}

public function rollback() {
    $this->mysqli->rollback();
}
public function close() {
    $this->mysqli->close();
}

}
?>


Part_Wise_Class.php
<?php
class Part_Wise_Class {   
    public function part_wise_get_part_nm($class_id,$subject_id) {
        $ssql = "SELECT part_id,part_nm from part_info WHERE class_id =? and subject_id=?";
        $db=new DBClient();
        $stmt = $db->mysqli->stmt_init();
        $stmt = $db->mysqli->prepare($ssql);
        $stmt->bind_param("ii",$class_id,$subject_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $db->close();
        return $result;
   }   
}
?>



test.php

<?php
include_once("DBClient.php");
include_once("Part_Wise_Class.php");   
$part_wise=new Part_Wise_Class();
$class_id=$_GET['class_id'];
$subject_id=$_GET['subject_id'];
$result=$part_wise->part_wise_get_part_nm($class_id,$subject_id);
$rows = array();
while ($row = $result->fetch_array(MYSQLI_BOTH)) {
            echo $row['part_id'];
}
?>

Tuesday 2 December 2014

Php json and jquery

How to use PHP, JSON and JQUERY

It is very simple to use PHP, JSON and JQUERY. for this you needed to create two files.
One file is "index.html", second file is "util.php" and this file the class file "DBClient.php" that will responsible for mysql database connection and return result according the sql query.
index.html file call the util.php using jquery ajax metod. and util.php is calling the DBClient.php and create it's object and return the result. the result will parse in index.html and parse it.

 


index.html

<input type="button" class="button" value="get value" id="btn1" />
<script language="javascript" type="text/javascript">
$("#btn1").click(function() {
    $.ajax({
    type: 'POST',
    url: 'util.php',
    data: { para1: 'city' },
    dataType: 'json',
    success: function (data) {    
        $.each(data.class_info, function(idx, obj) {
            alert(obj.city_name+"===="+obj.city_id);
        });
    }
});   
});
</script>

// you can also use below code,but then you will use different jquery code as below:
 //print  json_encode($rows);
// $.each(data, function(idx, obj) {
//           alert(obj.city_name+"===="+obj.city_id);
//        });util.php

<?php
include_once("DBClient.php");

if(isset($_POST['para1'])) {
$num_rows =0;
$db=new DBClient();
// the "city" table holding the columns "city_id" and "city_name"
$rs_qlist=$db->exeQueryi("select *from city");
$rows = array();
while($r = mysqli_fetch_assoc($rs_qlist)) {
    $rows[] = $r;
}
print ' { "class_info" : ' . json_encode($rows) . ' } ';
}
?>

// you can also use below code,but then you will use different jquery code as below:
 //print  json_encode($rows);
// $.each(data, function(idx, obj) {
//           alert(obj.city_name+"===="+obj.city_id);
//        });


DBClient.php
<?php
//error_reporting(E_ALL ^ E_DEPRECATED);
class DBClient {
var $host,$db,$user,$pass;
var $mysqli,$isDBConnect;
function __construct() {
    $this->host = "localhost";
    $this->db = "mydb";
    $this->user = "root";
    $this->pass = "pass";
    try {
          $this->mysqli = new mysqli($this->host, $this->user, $this->pass,$this->db );
        if ($this->mysqli->connect_error) {
            die('Error : ('. $this->mysqli->connect_errno .') '. $this->mysqli->connect_error);
        }
    } catch(Exception $ee) {
        $this->link=false;
           $this->isDBConnect=false;
    }
}

public function exeQueryi($query) {
    try {
        $result = $this->mysqli->query($query);
        return $result;
    } catch(Exception $ee) {
        return (FALSE);
    }
}

public function autocommit_false() {
    $this->mysqli->autocommit(FALSE);   
}

public function commit() {
    $this->mysqli->commit();   
}

public function rollback() {
    $this->mysqli->rollback();
}
public function close() {
    $this->mysqli->close();
}
}
?>