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();
}
}
?>

Tuesday 25 November 2014

MPDF error Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in MPDF57\includes\functions.php

When you download MPDF from http://www.mpdf1.com/mpdf/index.php  and working on it with latest version of PHP you may encounter following error:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in MPDF57\includes\functions.php on line 99

So How can you solve this problem? follow below steps:

In file "MPDF57/mpdf.php"...........   Line 32140 :  comment listed below line of code:

$temp[2][$iterator] = preg_replace("/^([^\n\t]*?)\t/me","stripslashes('\\1') . str_repeat(' ', ( $tabSpaces - (mb_strlen(stripslashes('\\1')) % $tabSpaces)) )",$temp[2][$iterator]);

TO

//$temp[2][$iterator] = preg_replace("/^([^\n\t]*?)\t/me","stripslashes('\\1') . str_repeat(' ', ( $tabSpaces - (mb_strlen(stripslashes('\\1')) % $tabSpaces)) )",$temp[2][$iterator]);


And also in "MPDF57/includes/function.php"....... Line number: 96 and 97 comment 
listed below lines of code:

$str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str);
$str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\\1',{$lo})",$str);

TO

//$str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str);
//$str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\\1',{$lo})",$str);

if you are still getting any error then at the top of php page where you are using MPDF use one line of code:

error_reporting(E_ALL ^ E_DEPRECATED);

This will permanently remove the error.



Monday 24 November 2014

Create a simple page in Laravel using Controller

Create a simple page in Laravel using Controller


1) Create a "ClientpagesController.php" file in "app/controllers/" folder as below contents

<?php
class ClientpagesController extends \BaseController {
  public function contactus(){
     return View::make("contactme");
  }
}

2) Open "app/routes.php" file and paste as:
Route::get("contactus" , "ClientpagesController@contactus");

3) Now create "contactme.php" in "app/views/" folder with any matter,
<html>
...............................
...............................
...............................
</html>

4) Now open this url in browser:
http://localhost:90/MyLaravel/public/contactus

Create a simple page in Laravel using Route

Create a simple page in Laravel using Route

To configure Laravel look here  

Here I am creating a simple "aboutus" page using "Routes" in Laravel.
Follow below steps:

1) Open "app/routes.php" file and paste as:

Route::get('aboutus', function()
{
    return View::make('aboutus');

});

2) Now create "aboutus.php" in "app/views/" folder with any matter,
<html>
...............................
...............................
...............................
</html>

3) now open browser with this URL:
http://localhost:90/MyLaravel/public/aboutus




Saturday 22 November 2014

How to create controller in Laravel



There are two methods to create controller in Laravel.
A) By command prompt using "artisan controller:make" :
B) By manually creating controller.
To configure Laravel look here 

A) By command prompt using "artisan controller:make" :
I am assuming you are using wamp 2.5.
  1. Go to in the "c:\wamp\www\" folder
  2. Open command window and cd in "c:\wamp\www\"
    type "composer create-project laravel/laravel TestPro1" in command window; where composer is the exe file name "create-project" is the keywors, "laravel/laravel" is the keyword, "TestPro1" is the project name.
    This will create a folder "TestPro1" with supporting directories and files.
  3. Rename "server.php" to "index.php"
  4. Open command prompt in "d:\wamp\www\TestPro1" and type the                                    "php artisan controller:make MyController".   *Keep in mind that you must in the directory "TestPro1" when you issue the command in command window                            "php artisan controller:make MyController" and there must be a file "artisan", "composer.json", "composer.lock".
  5. This will create a file "MyController.php" in the "TestPro1\app\controllers". 
  6.  replace it's index() method as below
  7. public function index()
        {
             return "
    This is my web page in Laravel";
        }
  8. and register it by "Route::resource('welcome','WelcomeController');" in "routes.php" under "D:\wamp\www\TestPro1\app" folder.
  9. now you can see page content y http://localhost:/TestPro1/
B) By manually creating controller. 
Goto "d:\wamp\www\TestPro1\app\controllers" folder. create a file "MyController.php". and paste listed below content.

<?php
class MyController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        return "This is my web page in Laravel";
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
 
public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

 /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }

 /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

and register it by "Route::resource('welcome','WelcomeController');" in "routes.php" under "D:\wamp\www\TestPro1\app" folder.
now you can see page content y http://localhost:/TestPro1/