Kategori
PHP

Delete Multiple Data with PHP

Case Study : Create function to delete multiple data on the database with checkbox component
Requirements : Webserver Packages, already installed.

Multi-delete Checkbox like in Gmail, we can choose to delete a message in our inbox smply by clicking the checkbox on the data that we want to remove. This tutorial will create a sample application to remove the data – specific data which is marked by checkbox (like in Gmail).

Ok, let’s get into the tutorial.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Script to Show the Data

  1. Type the following script,
    [sourcecode language=”php”]
    <html><head>
    <title>Itx.Web.Id</title>
    <script type="text/javascript" src="pilihan.js"></script>
    <script type="text/javascript" src="warning.js"></script>
    </head>
    <body>
    <h2> The Data <h2>
    <?php
    include ‘connect.php’;
    $query = "SELECT * FROM tb_book"; //the query for get all data in tb_student
    $result = mysql_query($query);
    ?>

    <form name="myform" method="post" action="prosesdeletemulti.php">

    <?php
    echo "<table border=’0′ cellpadding=’2′ cellspacing=’2′>";
    echo "<tr bgcolor=’orange’>
    <td> <b> Title </b> </td>
    <td> <b> Author </b> </td>

    </tr>";

    $i = 0;
    while ($data = mysql_fetch_array($result)) { //mysql_fetch_array = get the query data into array
    echo "<tr>
    <td>".$data[‘title’]."</td>
    <td>".$data[‘author’]."</td> "; ?>
    <td><input type="checkbox" name="id[<?php echo $i;?>]" value="<?php echo $data[‘id’];?>" /></td>
    <?php
    $i++;
    }
    echo "</table>";
    ?>
    <td> <input type="submit" value="Delete" onClick="return warning();" name="submit">
    <input type="reset" value="Cancel" name="reset"> </td>
    </form>
    </body>

    </html> [/sourcecode]

  2. save with the name student_data1.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    [sourcecode language=”javascript”]
    function warning() {
    return confirm(‘Are You Sure to Delete This Data?’);
    }
    [/sourcecode]
  2. save with the name warning.js

Step 6 : Javascript to get the checkbox ed component

  1. Type the following script,
    [sourcecode language=”javascript”]
    function pilihan()
    {
    // read the component from ‘myform’
    var jumKomponen = document.myform.length;
    // no check all
    if (document.myform[0].checked == false)
    {
    for (i=1; i<=jumKomponen; i++)
    {
    if (document.myform[i].type == "checkbox") document.myform[i].checked = false;
    }
    }
    }
    [/sourcecode]
  2. save with the name pilihan.js

Step 7 : Script to process delete multiple data

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    include ‘connect.php’;
    $data = $_POST[‘id’]; //get all the id book that will be deleted

    foreach($data as $data1) { //looping according to the total data that checked
    //echo $data1;
    $query = "DELETE FROM tb_book where id = $data1"; //the query to delete data according to id
    $result = mysql_query($query);
    }
    if ($result) {
    include "student_data1.php";
    echo "Delete sucess";
    }
    ?>
    [/sourcecode]

  2. save with the name prosesdeletemulti.php

Step 8 : Testing Code

  1. Go to http://localhost/tutorphp/student_data1.php. Check the checkbox on the data where you will delete the data.
  2. Warning appear. Choose Ok.
  3. Success confirmation appear, and the data really has been deleted. Check your database also 😀

For the script explanation.. see script comment. and feel free to comment 😀

OK. Haaaaaaaapppy Coding! 😀

Kategori
PHP

Delete Mysql Data Function with PHP

Case Study : Create function to delete data on mysql database.
Requirements : Webserver Packages, already installed.

Delete function will be done one by one, so, here it is.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Script to show the data

  1. Type the following script,
    [sourcecode language=”php”]
    <html><head>
    <title>Itx.Web.Id</title>
    <script type="text/javascript" src="warning.js"></script>
    </head>
    <body>
    <h2> Student’s Data <h2>
    <?php
    include ‘connect.php’;
    $query = "SELECT * FROM tb_student"; //the query for get all data in tb_student
    $result = mysql_query($query);

    echo "<table border=’0′ cellpadding=’4′ cellspacing=’4′>";
    echo "<tr bgcolor=’orange’ align=’center’>
    <td> <b> No </b> </td>
    <td> <b> Name </b> </td>
    <td> <b> Address </b> </td>
    <td> <b> Action </b> </td>
    </tr>";
    while ($data = mysql_fetch_array($result)) //mysql_fetch_array = get the query data into array
    {
    echo "<tr align=’center’>
    <td>".$data[‘no’]."</td>
    <td>".$data[‘name’]."</td>
    <td>".$data[‘address’]."</td> "; ?>
    <td> <i> <a href="prosesdelete.php?no=<?php echo $data[‘no’];?>" onClick="return warning();"> Del </a> </i></td>
    <?php
    }
    echo "</table>";
    ?>
    </body>

    </html>
    [/sourcecode]

  2. Save with the name student_data.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    [sourcecode language=”javascript”]
    function warning() {
    return confirm(‘Are You Sure to Delete This Data?’);
    }
    [/sourcecode]
  2. Save with the name formupdate.php

Step 6 : Create script to process delete

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    include "connect.php";
    //get the value from form update
    $no = $_GET[‘no’]; //get the no which will deleted

    //query for update data in database
    $query = "DELETE from tb_student WHERE no = ‘$no’" ;
    $hasil = mysql_query($query);
    //see the result
    if ($hasil) {
    include "data.php";
    echo "<h4> delete data success </h4>";
    }
    ?>
    [/sourcecode]

  2. Save with the name prosesdelete.php

Step 7 : Testing Code

  1. Go to http://localhost/tutorphp/date.php. Click delete in one of the data showed.
  2. When confirmation window appear, choose OK.
  3. Thus the success conformation appear, and the data has been deleted. Check your database, too. Make sure that the data really deleted 😀

For the explanation of the script just see the comment script or ask here… and feel free to comment 😀
OK. Haaaaaaaapppy Coding! 😀

Kategori
PHP

Create UPDATE Database Function with PHP

Case Study : Create update function to update student data in database
Requirements : Webserver Packages, already installed.

Ok, Here we go.

Step 1 : Prepare Database

  1. Create database named db_tutorial
  2. Prepare table named tb_student, with structure as below.
  3. Insert some sample data like below
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Script to show the data

  1. Type the following script,
    [sourcecode language=”php”]
    <h2> Student’s Data <h2>
    <?php
    include ‘connect.php’;
    $query = "SELECT * FROM tb_student"; //the query for get all data in tb_student
    $result = mysql_query($query);

    echo "<table border=’0′ cellpadding=’2′ cellspacing=’2′>";
    echo "<tr bgcolor=’orange’ align=’center’>
    <td> <b> No </b> </td>
    <td> <b> Name </b> </td>
    <td> <b> Address </b> </td>
    <td> <b> Action </b> </td>
    </tr>";
    while ($data = mysql_fetch_array($result)) //mysql_fetch_array = get the query data into array
    {
    echo "<tr align=’center’>
    <td>".$data[‘no’]."</td>
    <td>".$data[‘name’]."</td>
    <td>".$data[‘address’]."</td>
    <td> <i> <a href=’formupdate.php?no=".$data[‘no’]."’> Update </a> </i></td>
    </tr>";
    }
    echo "</table>";
    ?>
    [/sourcecode]

  2. Save with the name data.php

Step 5 : Create script to show update form

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    include ‘connect.php’;
    $no = $_GET[‘no’]; //get the no which will updated
    $query = "SELECT * FROM tb_student WHERE no = $no"; //get the data that will be updated
    $hasil = mysql_query($query);
    $data = mysql_fetch_array($hasil);
    ?>
    <form method="post" action="prosesupdate.php">
    <table width="500" border="0" cellpadding="2" cellspacing="2">
    <tr bgcolor="orange">
    <td height="21" colspan="3">
    <strong> Form Update</strong>
    </td>

    <tr>
    <td width="23%"> No </td>
    <td width="2%">:</td>
    <td width="75%"> <input type="text" name="no" disabled=’disable’ value="<?php echo $data[‘no’]; ?>"> </td>
    </tr>

    <tr>
    <td>Name</td>
    <td>:</td>
    <td> <input type="text" name="name" value="<?php echo $data[‘name’]; ?>"> </td>
    </tr>

    <tr>
    <td>Address</td>
    <td>:</td>
    <td> <textarea name="address"><?php echo $data[‘address’]; ?></textarea></td>
    </tr>
    <tr>
    <td></td> <td></td> <td> <input type="hidden" name="no" value="<?php echo $data[‘no’]; ?>">
    <input type="SUBMIT" name="SUBMIT" value="Save"></td>
    </tr>
    </table>
    </form>
    [/sourcecode]

  2. Save with the name formupdate.php

Step 6 : Script to update data

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    include "connect.php";
    //get the value from form update
    $no = $_POST[‘no’];
    $name = $_POST[‘name’];
    $address = $_POST[‘address’];

    //query for update data in database
    $query = "UPDATE tb_student SET name = ‘$name’, address = ‘$address’ WHERE no = ‘$no’" ;
    $hasil = mysql_query($query);
    //see the result
    if ($hasil) {
    include "data.php";
    echo "<h4> update data success </h4>";
    }
    ?>
    [/sourcecode]

  2. Save with the name prosesupdate.php

Step 7 : Testing Code

  1. Go to http://localhost/tutorphp/date.php. Click update in one of the data.
  2. You will see the form filled with the data. See that all date changeable, except field ‘no’ is inactive (because it is an id). Fill with new data, and then save.
  3. You will see the data has been updated 😀

OK. Haaaaaaaapppy Coding! 😀

Kategori
PHP

Create Dynamic Combobox Form from Database (Mysql) with PHP

Case Study : Create dynamic combobox with dynamic value
Requirements : Webserver Packages, already installed.

Combobox is one element in a form that is often used in applications – the application registration, generally combobox is used to select one option where there are many options available. Value in the combobox can actually be written directly in the html, but, what if the data you want to appear very much or depend on certain conditions?
Suppose, we want to show the choice of cities – cities that exist within the Indonesian state, the city is also a city which appears to be in Indonesia.
For example, a user was coming from England, the town which is also a city which appears to be in the UK.

We need dynamic combobox

Then, here we go.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Create the combobox form

  1. Type the following script,
    [sourcecode language=”php”]
    <form name=’form’ method=’post’ action=’prosescombo.php’>
    <h3> Choose Your City</h3>
    <select name="kota">
    <option value=0 selected>- city -</option>
    <?php
    include ‘connect.php’;
    $q = mysql_query("select * from tb_kota where negara = ‘Indonesia’ "); //choose the city from indonesia only

    while ($row1 = mysql_fetch_array($q)){
    echo "<option value=$row1[kota]>$row1[kota]</option>";
    }
    ?>
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>
    [/sourcecode]

  2. Save with the name formcombo.php

Step 5 : Create script to show the result

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $kota = $_POST[‘kota’];

    if ($kota == ‘0’) {
    echo "anda belum memilih";
    } else
    echo "anda memilih ".$kota;
    ?>
    [/sourcecode]

  2. Save with the name prosescombo.php

Step 6 : Testing Code

  1. Go to http://localhost/tutorphp/formcombo.php. You will see the form like below. Choose one option (i choose Solo, here).
  2. Choose submit. And see the result 😀

OK. Haaaaaaaapppy Coding! 😀

Kategori
PHP

PHP Form Input Date with Datetimepicker JQuery

Case Study : Create Form Register with one of the field input using datetimepicker jquery.
Requirements :

  1. Webserver Packages, already installed.
  2. jQuery library, download here
  3. UI Core, download here, dan UI Datepicker, download here
  4. Theme Roller, download here, you can design your own theme, or download available theme. I used “sunny” theme.

Follow this steps.

Step 1 : Prepare the Work Folder

  1. Create folder named tutorphp in your document root
  2. Save file jQuery library, UI Core, UI Datepicker, and Theme (download above), so the structure of the folder is show below,

Step 2 : HTML – Form

  1. Type the following script,
    [sourcecode language=”php”]
    <link type="text/css" rel="stylesheet" href="sunny/jquery-ui-1.8.2.custom.css" />
    <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="jquery.ui.datepicker.js"></script>
    <script type="text/javascript" src="jquery.ui.core.js"></script>
    <script type="text/javascript" src="tanggal.js"></script>

    <center><h1>Form Pendaftaran</h1>
    <form method="post" action="tampil.php">
    <table>
    <tr><td>Nama</td><td><input type="text" name="nama"></td></tr>
    <tr><td>Alamat</td><td><input type="text" name="alamat"></td></tr>
    <tr><td>Tgl Lahir</td><td><input type="text" id="tgl_lahir" name="tgl_lahir"></td></tr>
    <tr><td></td><td><input type="submit" name="submit" value="Submit"></td></tr>
    </table>
    </form>
    </center>
    [/sourcecode]

  2. Save with the name form.php, and save in the tutorphp folder

Step 3 : Javascript – Datepicker

  1. Type the following script,
    [sourcecode language=”javascript”]
    $(document).ready(
    function() {
    $(function() {
    $("#tgl_lahir").datepicker({
    showButtonPanel: true,
    //minDate: new Date(),
    showTime: true
    });
    });
    });
    [/sourcecode]
  2. Save with the name tanggal.js, save in the tutorphp folder
  3. This Javascript will show datetimepicker jQuery in the field input with id tgl_lahir

Step 4 : PHP – Showing the Result

  1. Fo==Type the following script,
    [sourcecode language=”php”]
    <?php
    $nama = $_POST[‘nama’];
    $alamat = $_POST[‘alamat’];
    $tgl_lahir = $_POST[‘tgl_lahir’];

    echo $nama.'<br>’;
    echo $alamat.'<br>’;
    echo $tgl_lahir;
    ?>
    [/sourcecode]

  2. Save with the name form.php, and save in the tutorphp folder

Step 5 : Testing Code

  1. Go to http://localhost/tutorphp/form.php. You will see form like below. Fill the field, and then click input Tanggal field, so your datepicker will showed like below 😀
  2. Choose submit, and you will see the data 😀
Kategori
PHP

Create Thumbnail Version from Image with PHP

Case Study : Create Thumbnail Version from Image with PHP GD Library
Requirements : Webserver Packages, already installed. And make sure your PHP support Image GD Library (version 1-8 above)
Follow this step..

Step 1 : Prepare the Work Folder

  1. Create folder named tutorphp in your document root
  2. Prepare sample JPG image in this folder. For this tutorial, i use this sample image (p.s. : you can use any jpeg image). If you want to use this image, just right click and save as
  3. Rename the image to myPic.jpg (p.s. : this “rename” things just to make easy for follow the tutorial 😛 )

Step 2 : Create main script to resize image in to thumbnail

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $image = ‘myPic.jpg’; //default image, change here if you want to use another image

    //get the image size and image info
    list($width_orig, $height_orig, $image_type) = getimagesize($image);

    if($image_type !== 2) { // 2 is for JPEG Image
    echo ‘Image is not JPEG Image!’;
    }
    else {
    $thumbname = ‘thumbnail_’.$image; //default name for thumbnail version of image

    // setting the height for the width 75px (75px is default width for the thumbnail). this is for maintain the ratio
    $height_tb = (int) ((75 / $width_orig) * $height_orig);

    //Create a new true color image
    $image_p = imagecreatetruecolor(75, $height_tb);

    //Create a new JPEG image from file or URL
    $image = imageCreateFromJpeg($image); //

    //Copy and resize part of an image with resampling
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, 75, $height_tb, $width_orig, $height_orig);

    //upload image to folder
    if(!is_writeable(dirname($thumbname)))
    {
    echo ‘Unable to Upload image to ‘ . dirname($thumbname);
    }
    else
    {
    //output image to browser or file, the parameter is imageJPEG(resource image, name of the image, quality)
    imageJpeg($image_p, $thumbname, 100);
    }
    }
    ?>
    <center><h1>Create Thumbnail Image</h1>
    <table border=’1′ cellpadding=’5′ cellspacing=’5′>
    <tr align=’center’> <td>Original</td> <td>Thumbnail</td> </tr>
    <tr> <td><img src="myPic.jpg" alt="image" /></td> <td><img src="<?php echo $thumbname;?>" alt="thumbnail" /></td></tr>
    </table>
    <center>
    [/sourcecode]

  2. Save with the name imagetb.php, save in the tutorphp folder
  3. Explanation : see on the script (script with green) color, Okay

Step 3 : Testing Code

  1. Go to http://localhost/tutorphp/imagetb.php
  2. You can see your image (original) with its thumbnail 😀 just like the picture below..
  3. Check tutor.php folder. You will see the thumbnail_myPic.jpg file, that is thumbnail version of your image 😀

😀

Kategori
PHP

Simple Guest Book with PHP & Mysql

Case Study : Create simple guest book with PHP Mysql
Requirements : Webserver Packages, already installed.

Ok, lets just practices.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare tabel named tb_guestbook, with structure like below.
  3. Done!

Step 2 : Prepare the Work Folder

  1. Create folder named tutorphp in your document root
  2. Save all the files in this tutorial in this folder.

Step 3 : Create script to connect to DB

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;?php<br />
    $host = &quot;localhost&quot;;<br />
    $user = &quot;root&quot;;//adjust according to your mysql setting<br />
    $pass = &quot;&quot;; //adjust according to your mysql setting, i use no password here<br />
    $dbName = &quot;db_tutorial&quot;;<br />
    mysql_connect($host, $user, $pass);<br />
    mysql_select_db($dbName)<br />
    or die (&quot;Connect Failed !! : &quot;.mysql_error());<br />
    ?&gt;<br />
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Create the GuestBook Form

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;h1&gt;Guestbook&lt;/h1&gt;<br />
    &lt;form method=&quot;post&quot; action=&quot;savegb.php&quot;&gt;<br />
    &lt;table&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Name&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Email&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;Your Comment&lt;/td&gt;&lt;td&gt;&lt;textarea name=&quot;comment&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
    &lt;/table&gt;<br />
    &lt;/form&gt;</p>
    <p>[/sourcecode]
  2. save with the name formguestbook.php

Step 5 : Create a Processing Script

  1. Type the following script,
    [sourcecode language=”php”]<br />
    &lt;?php<br />
    include ‘connect.php’;</p>
    <p>$name = $_POST[‘name’]; //get the name value from form<br />
    $email = $_POST[‘email’]; //get the email value from form<br />
    $comment = $_POST[‘comment’]; //get the comment value from form</p>
    <p>//query to save to database<br />
    $q1 = &quot;INSERT into tb_guestbook value (”,’$name’,’$email’,’$comment’)&quot;;<br />
    $result1 = mysql_query($q1);</p>
    <p>//query to get data from database<br />
    $q2 = ‘SELECT * FROM tb_guestbook order by id desc’;<br />
    $result2 = mysql_query($q2);</p>
    <p>//show the data from database<br />
    while ($record = mysql_fetch_array($result2)) {<br />
    echo &quot;&lt;table&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘name’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Email&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘email’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;tr&gt;&lt;td&gt;Comment&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&quot;.$record[‘comment’].&quot;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
    echo &quot;&lt;/table&gt;&quot;;<br />
    echo &quot;&lt;br/&gt;&quot;;<br />
    }<br />
    ?&gt;<br />
    [/sourcecode]
  2. Save with the name savegb.php
  3. For the explanation, see script’s comment 😀

Step 6 : Testing Code

  1. Go to http://localhost/tutorphp/formguestbook.php. You will see the form like below. Fill the form (the example is like the picture below).
  2. Click save and you will see the result.

OK. Haaaaaaaapppy Coding! 😀

Kategori
PHP

Get the Other Site’s Feeds with SimplePie RSS Reader

Case Study : Get the other’s site feed with RSS Reader SimplePie, show it, and save it to database.
Requirements : SimplePie library, download here. Webserver Packages, already installed.

RSS (Really Simple Syndication) is a xml based format that used to sharing and delivering web content that usually updated regularly. While the term “output” refers to the outputs of RSS which can be read with RSS Feed Reader. SimplePie is a RSS Feed Reader/Parser that quite famous (and my favorite of course), the way RSS Feed Parser working is to read a XML format contained in the RSS Feed which we set ourselves.

Okay, lets give it a try.

Step 1 : Prepare the Database

  1. Create a new database named db_tutorial
  2. Create table tb_rss, with structure like the picture below.
    Table Structure
  3. Done with the database!

Step 2 : Prepare Working Folder

  1. Create a new folder named rss in your document root
  2. Create new folder named cache in the folder rss (…/rss/cache)
  3. Copy file SimplePie.inc (on simplepie zip file) in to folder rss.

Step 3 : Create a Connection to Database

  1. Create a script to connect to database, type the following script,
    <?php
    //file name : connect.php
    //author : littleflow3r
    $host = "localhost";
    $user = "root"; //sesuaikan dengan setting mysql
    $pass = ""; //
    sesuaikan dengan setting mysql
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
  2. Save in the folder rss with name connect.php

Step 4 : Inisialisasi SimplePie

  1. Type the following script,
    <?php
    //file name : initRSS.php
    //author : littleflow3r
    require_once 'simplepie.inc';
    //initialize simplePie
    $feed = new SimplePie('http://localhost/websmsmasking/feed/rss');
    $feed->set_cache_location('../rss/cache/');
    $feed->set_feed_url('http://localhost/websmsmasking/feed/rss');
    $feed->init();
    $feed->handle_content_type();
    ?>
  2. save in the folder rss and named it initRSS.php
  3. Explanation : The way of SimplePie RSS Parser working is to read the XML format contained adalah dengan membaca format XML contained in the RSS Feed. The Code above is used to initializes SimplePie, by entering the address of our rss [baris 06 dan baris 08]. For example, i use the sample rss address is localhost/websmsmasking/feed/rss –> feed from my local application. You can change this to address that you want, but remember that to gets the rss feed from online sites, you have to be connected to internet, too (this is because simplepie will directly access to that sites.)

Step 5 : Show the Feed

  1. Type the following script,
    <?php
    //file name : rss.php
    //author : littleflow3r
    include 'initRSS.php';
    include 'connect.php';
    //get the feed and show it
    echo "<h1> RSS News Update </h1>";
    foreach ($feed->get_items(0,4) as $item):
    $permalink = $item->get_permalink();
    $title = $item->get_title();
    $desc = $item->get_description();
    echo '<a href="'.$permalink.' ">'; echo $title; echo '</a>';
    echo "<br>";
    echo $desc;
    echo "<br>"; echo "<br>";
    //insert to database
    $q = "INSERT into tb_rss (link, title, description) VALUES('$permalink','$title','$desc')";
    $result = mysql_query($q);
    endforeach;
    ?>
  2. Save in the folder rss, named it rss.php
  3. Explanation: Baris 08 show looping with foreach according to the number of feed obtained from the address of the set RSS’s address, by the get_items(0,4)function, the intention is to take 4 feed items starting from the 0 record.
    Baris 09 save the permalink from each itemsin to variabel $permalink
    Baris 10 get the feed title by the get_title function and save it to $title.
    Baris 11 get the feed description with the get_desc function and save it to $desc.

Step 6 : Testing Code

  1. Go to brwoser, run http://localhost/rss/rss.php
  2. You will see the feed from your rss’s address like image below.
    Result
  3. Check the database, see that tb_rss already filled with rss content from your rss’s address.
    Tb_RSS

Okay, Done. Happy Trying! Happy Coding 🙂

Kategori
PHP

Load and Save the XML Data to Database with PHP & Mysql

Case Study : Get the XML data and save it to database (mysql)

Requirements : Webserver Packages, already installed.

OKAY, no need for trivial chit-chat. Let’s just practice 😀

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Create table named tb_dataxml, the structure is as shown below.

  3. Done!

Step 2 : Prepare the work folder

  1. Create folder named tutorphp in your document root
  2. Save all the file in this tutorial in this folder.

Step 3 : Create a connection to db script

  1. Type the following script,

    [sourcecode language=”php”]<br /><br />
    &lt;?php<br /><br />
    $host = &quot;localhost&quot;;<br /><br />
    $user = &quot;root&quot;; //adjust according to your mysql setting<br /><br />
    $pass = &quot;&quot;; //adjust according to your mysql setting<br /><br />
    $dbName = &quot;db_tutorial&quot;;<br /><br />
    mysql_connect($host, $user, $pass);<br /><br />
    mysql_select_db($dbName)<br /><br />
    or die (&quot;Connect Failed !! : &quot;.mysql_error());<br /><br />
    ?&gt;<br /><br />
    [/sourcecode]

  2. save with the name connect.php

Step 4 : Prepare the Sample XML Date

  1. Type the following script,

    [sourcecode language=”xml”]<br /><br />
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;<br /><br />
    &lt;data&gt;<br /><br />
    &lt;record&gt;<br /><br />
    &lt;title&gt;Time to Go&lt;/title&gt;<br /><br />
    &lt;author&gt;June Sushan&lt;/author&gt;<br /><br />
    &lt;publisher&gt;Gramedia Inc&lt;/publisher&gt;<br /><br />
    &lt;date&gt;<br /><br />
    &lt;month&gt;June&lt;/month&gt;<br /><br />
    &lt;year&gt;1989&lt;/year&gt;<br /><br />
    &lt;/date&gt;<br /><br />
    &lt;/record&gt;</p>
    <p> &lt;record&gt;<br /><br />
    &lt;title&gt;Time to Come&lt;/title&gt;<br /><br />
    &lt;author&gt;Alan Smart&lt;/author&gt;<br /><br />
    &lt;publisher&gt;Elexmedia Inc&lt;/publisher&gt;<br /><br />
    &lt;date&gt;<br /><br />
    &lt;month&gt;April&lt;/month&gt;<br /><br />
    &lt;year&gt;1989&lt;/year&gt;<br /><br />
    &lt;/date&gt;<br /><br />
    &lt;/record&gt;</p>
    <p>&lt;/data&gt;</p>
    <p>[/sourcecode]

  2. save with the name record.xml

Step 5 : Create script for loads the XML, save it database, and show the XML

  1. Type the following script,

    [sourcecode language=”php”]<br /><br />
    &lt;?php<br /><br />
    include ‘connect.php’;<br /><br />
    if( !$xml = simplexml_load_file(‘record.xml’) ) //using simplexml_load_file function to load xml file<br /><br />
    {<br /><br />
    echo ‘load XML failed ! ‘;<br /><br />
    }<br /><br />
    else<br /><br />
    {<br /><br />
    echo ‘&lt;h1&gt;This is the Data&lt;/h1&gt;’;<br /><br />
    foreach( $xml as $record ) //parse the xml file into object<br /><br />
    {<br /><br />
    $title = $record-&gt;title; //get the childnode title<br /><br />
    $author = $record-&gt;author; //get the child node author<br /><br />
    $publisher = $record-&gt;publisher; //get the child node publisher<br /><br />
    $month = $record-&gt;date-&gt;month; //get the child node month<br /><br />
    $year = $record-&gt;date-&gt;year; //get the child node year</p>
    <p> echo ‘Title : ‘.$title.’&lt;br /&gt;’;<br /><br />
    echo ‘Author : ‘.$author.’&lt;br /&gt;’;<br /><br />
    echo ‘Publisher : ‘.$publisher.’&lt;br /&gt;’;<br /><br />
    echo ‘Month : ‘.$month.’&lt;br /&gt;’;<br /><br />
    echo ‘Year : ‘.$year.’&lt;br /&gt;’;<br /><br />
    echo ‘&lt;br&gt;’;</p>
    <p>//save to database<br /><br />
    $q = &quot;INSERT INTO tb_dataxml VALUES(”,’$title’,’$author’,’$publisher’,’$month $year’)&quot;;<br /><br />
    $result = mysql_query($q);<br /><br />
    }<br /><br />
    if ($result) {<br /><br />
    echo ‘&lt;h2&gt;Success Save to Database &lt;/h2&gt;’;<br /><br />
    }<br /><br />
    else echo ‘&lt;h2&gt;Failed Save to Databaase&lt;/h2&gt;’;<br /><br />
    }<br /><br />
    ?&gt; [/sourcecode]

  2. Save with thename loadxml.php

Step 6 : Testing Code

  1. Go to http://localhost/tutorphp/loadxml.php. You will see the data from record.xml already parsed and showed like in the picture 😀

  2. Check the database, make sure that the xml data already saved in the table you’ve created on step 1

So, this is time to trivial chit-chat Ha Ha 😀 XML is extensible Markup Language.

Why XML? XML is the standar format for data communication. Because of this, XML often used on the data communication because the platform independen format 😀

Kategori
PHP

Tutorial Image Uploading, Saving and Showing.

Case Study : Create a script to image uploading, save image location to database, and show them.
Requirements : Webserver Packages, already installed.

Follow all this step,

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare table named tb_image, see the table’s structure below.
  3. Done with the database!

Step 2 : Prepare the wcorking folder

  1. Create folder named helloMobile in your document root
  2. Create folder again named image inside helloMobile folder created before. This Folder required for saving the image that have been uploaded.
  3. Save all file in this tutorial in the helloMobile folder.

Step 3 : Create script connect to DB

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php, and save in the helloMobile folder

Step 4 : Create the upload image form

  1. Type the following script,
    [sourcecode language=”php”]
    <form name="form" method="post" enctype="multipart/form-data" action="proses.php">
    Image : <input name="picture" type="file" />
    <input type="submit" name="upload" value="Upload" />
    </form>
    [/sourcecode]
  2. Save with the name formupload.php, save in the helloMobile folder

Step 5 : Create processing script

  1. Ketikkan script berikut,
    [sourcecode language=”php”]
    <?php
    include "connect.php";
    $fileName = $_FILES[‘picture’][‘name’]; //get the file name
    $fileSize = $_FILES[‘picture’][‘size’]; //get the size
    $fileError = $_FILES[‘picture’][‘error’]; //get the error when upload
    if($fileSize > 0 || $fileError == 0){ //check if the file is corrupt or error
    $move = move_uploaded_file($_FILES[‘picture’][‘tmp_name’], ‘E:/DocumentRootYuni/helloMobile/image/’.$fileName); //save image to the folder
    if($move){
    echo "<h3>Success! </h3>";
    $q = "INSERT into tb_image VALUES(”,’$fileName’,’image/$fileName’)"; //insert image property to database
    $result = mysql_query($q);

    $q1 = "SELECT location from tb_image where filename = ‘$fileName’ limit 1 "; //get the image that have been uploaded
    $result = mysql_query($q1);
    while ($data = mysql_fetch_array($result)) {
    $loc = $data[‘location’]; ?>
    <br/>
    <h2> This is the Image : </h2>
    <img src="<?php echo $loc; ?>" /> <!– show the image using img src –>
    <?php
    }
    } else{
    echo "<h3>Failed! </h3>";
    }
    } else {
    echo "Failed to Upload : ".$fileError;
    }
    ?>
    [/sourcecode]

  2. Save with the name prosesupload.php
  3. See the script’s comment for the explanation 😀

Step 6 : Testing Code

  1. Go to http://localhost/helloMobile/formupload.php. And you will see the form below,
  2. Try to upload image. Choose a image file with click browse button
  3. Save. So the image will be shown to you 😀