Vacation to Krabi-Thailand

Vacation to Krabi-Thailand

FIRST DAY

At 10-12,Feb 2012 I went vacation to Krabi, Thailand , together with my friends. We departed from Singapore and arrived there about 3 PM . For transport to hotel (at Aonang beach), it’s really easy, at airport there is taxi counter to booking taxi directly, no need to looking around and do not be afraid of being deceived (after go out from immigration gate, we already can see the taxi counter easily at first floor). First time, I though the taxi likes regular taxi has taxi lamp on the top of car, after i saw the car, really suprised because like we rent private car and the cost only 600 Baht ( 20 SGD / 25 SGD ). Travel time from Airport to Aonang Beach take about 1 Hour.

We stayed at Haleeva Sunshine, Krabi, Ao Nang. The hotel is really good, clean, and their service is really nice. When we just arrived and wanted to check in, they provided us welcome  drink, gave us Aonang map and explained how to go to beach. Luckily we got a good room because the room overlooking the mountains 🙂 . After put our luggage, we walked to Aonang beach . Just take some photo there and saw the sunset. The sunset is really really nice . Here some beach photo and sunset photos which is taken there.

Read More

Simple Paging (PHP)

For programmers must often make applications that display data. Well, if the data is small, it does not matter, but what if the data that would show number in the dozens or even thousands of rows , will face a very long scroll window or may be your browser hang. Now, to overcome it used a system that is widely known that Paging System. Paging System is a system that displays the data step by step, it’s divided into several pages with each page has only a few data only.

Now I will explain a basic/simple logic of the paging system. See the table below:

For example, we had 22 records of student, then we want to show with divided into several pages with a maximum 5 records per page.
Well, first we find out the number of pages :

Number of Pages = Total Record / Maximum Record every page 
Number of Pages = 22/5 = 4.4 Pages round up to 5 Pages

For the data from the database (mysql), we take from the first record with a multiple of 5 to the next page, 
See table above:

  • on page 1, we just take the data from the record-0 up to record to 4,
  • on page 2, we simply take the data from record to record the 5th to the 9th, etc. (the first record in mysql starting from the number 0). 

So the formula to get the start record is:

(Current page number - 1) * Maximum records per page

So based on example above (with a maximum record per page = 5):
Start record for page 1: (1-1) * 5 = 0
Start record for page 2: (2-1) * 5 = 5
Start record for page 3 (3-1) * 5 = 10
Start record on page 4: (4-1) * 5 = 15
Start record to page 5: (5-1) * 5 = 20

Well, based on the logic that I described above , following the example in the form of coding PHP:

<?
    function generate_paging_text($curPage,$totalRec,$maxRec){
        $totalPage=ceil($totalRec/$maxRec);
        $str="";
        
        /*prev button*/
        if($curPage>1){             
            $prevPage = $curPage-1;
            $str.=" ".makeLink("prev","?p=".$prevPage)." ";            
        }
        
        /*generate page number*/
        for($i=1;$i<=$totalPage;$i++){
            if($i==$curPage){
                $bold=true;
            }else{
                $bold=false;
            }
            $str.=" ".makeLink($i,"?p=".$i,$bold)." ";
        }
        
        /*next button*/
        if($curPage<$totalPage){             
            $nextPage=$curPage+1;
            $str.=" ".makeLink("next","?p=".$nextPage)." ";            
        }
        
        return $str;
        
    }
    function makeLink($str,$url,$bold="false"){
        if($bold){
            $str="<b>".$str."</b>";
        }
        return '<a href="'.$url.'">'.$str.'</a>';
    }
    
        
    //Connect to database    
    $con = mysql_connect("localhost","root","");
    mysql_select_db("example",$con);
    
    $curPage=($_GET['p']==null)?1:$_GET['p']; 
    $maxRec=5;// maximum record per page
    

    /* GET DATA FROM MYSQL */    
    $curRec = ($curPage-1)*$maxRec; //TO GET START RECORD
    $sql = "select * from customer order by name asc limit ".$curRec.",".$maxRec;
    echo "Query for Page  ".$curPage.": <b>".$sql."</b><Br><BR>";
    $rs=mysql_query($sql,$con);
    while ($row = mysql_fetch_assoc($rs)) {
        echo "Nama : ".$row['name']." , Umur : ".$row['age']."<br>";
    }
    
    // get total Record from your table in database
    $total_record=mysql_query("select count(customer_id) as tot from customer",$con);    
    $total_record=mysql_fetch_row($total_record);
    $total_record=$total_record[0];
        
    
    /* PRINT PAGING LINK */
    echo "<br>";
    echo generate_paging_text($curPage,$total_record,$maxRec);
?>

 

function generate_paging_text($curPage,$totalRec,$maxRec)
  1. $curPage = current active page number.
  2. $totalRec = total records
  3. $maxRec = maximum records that may be displayed for each page.

 

Multiple Delete (PHP)

Now I will try to share a little about how to create a facility to delete a record on the website (php) similar to delete messages in GMail.

  1. First, create a form and then show a list of records retrieved from the database. View in a table where each record by 1 checkbox. 
    <?
    
           $rs=mysql_query("select * from mhs");//ambil record dari tabel mhs
    
           while($data=mysql_fetch_array($rs)){?>
    
              <tr>
    
                 <td>
    
                  <input type="checkbox" name="chk[]" value="<?=$data[id]?>">
    
                 </td>
    
                  <td><?=$data[id]?></td>
    
                  <td><?=$data[nim]?></td>
    
                  <td><?=$data[nama]?></td>
    
               </tr><?
    
           }
    
    ?>

    Checkbox on top of each filled with the id of each record and the name of the checkbox by the same name and added symbol behind the name of “[]”. This is so that when read by PHP, Checkbox considered as an array.

  2. <?
    
              $chk=$_POST[chk];
    
              / * -------------- Change corresponding computer settings masing2 ---- * /
    
              $host="localhost";
    
              $user="root";
    
              $pass="";
    
             /*--------------------------------------------------------*/
    
              $ Con = mysql_connect ($ host, $ user, $ pass); // open connection to the database
    
              mysql_select_db("tes");//pilih database
    
     
              foreach($chk as $id){
    
                  if(mysql_query("delete from mhs where id=".$id)){
    
                      echo "<div align=center>
    
                              Remove record with id = ". $ Id." - Success
    
                            </div><br>";
    
                  }else{
    
                      echo "<div align=center>
    
                              Delete record with id=".$id."-- Failed
    
                            </div><br>";
    
                  }     
    
              }
    
        mysql_close($con);
    
    ?>

     After that create a new php file to delete process.

    First, do any data retrieval checkbox marked. As already known, to take the value sent from a form can use the $ _POST , $ _GET , or $ _REQUEST . Well, to take a checkbox value is the same way, here I am using the POST method.

           $chk=$_POST[chk];

     Because chk on html form written like this ” chk [] ” then the current value of the $ _POST pemgambilan then php will directly menggangap chk it as an array. Then the variable $ chk now be an array that contains the value of checkbox- checkbox is selected / checked.

    When he could take valuenya, then stay manipulate value-value is taken as necessary (in this example we will use valuenya as key to delete the record from the database. It can be seen from the above source code, I use foreach to take one-on-one contents array (array content is id which records obtained from the checkbox is checked only). Later in the recurrence did query delete :

    foreach($chk as $id){
    
        if(mysql_query("delete from mhs where id=".$id)){
    
            echo "<div align=center>
    
                     Remove record with id = ". $ Id." - Success
    
                 </div><br>";
    
        }else{
    
            echo "<div align=center>
    
                     Delete record with id=".$id."-- Failed
    
                  </div><br>";
    
        }     
    
    }