Archive for August 2013

Listing subcategories or chilld categories using php

We can use the following function for getting the subcategories up to any level. It is using recursive technique.

function getSubCats($catid){
        $query= "select cat_id from category where cat_parent_id='$catid'";
        $rst =  $this->db->query($query);
        if($rst->num_rows==0){
            return null;
        }else{
            foreach($rst->rows as $row) {
                $categories[] = $row['cat_id'];
                $children = $this->getSubCats($row['cat_id']);   
                if($children!=null){
                    foreach($children as $child){
                        $categories[] = $child;   
                    }
                }
            }
           
            return $categories;
        }

    }

PHP Encryption and Decryption Function

Sometime we need to convert PHP strings to secret code which cannot be decoded easily by user.  This method will help us to pass coded strings with URL GET arguments and also inside Hidden Form fields ..

Eg : http://myserver.com/index.php?username=j345kjh656bthbf6

PHP has inbuilt base64 encocde and base64 decode functions . but anyone can easily decrypt the string by passing it to base64 decoded functions .. So we need our own encode and decode function so that user cant identify the original content .

Simple encryption and decryption function is given below 

Assign $key with your own secretpasskey

The string encrypted using function encrypt can be decoded only using decrypt function .

For extra security we have included base64 encryption also.

function encrypt($string, $key='mysecretcode123')
{
$result = '';
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode($result);
}

function decrypt($string, $key=' mysecretcode123 ')
{
$result = '';
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}



Web Services



Web service are applications using open protocols and standards to exchange data between applications or systems.Software applications written in various programming languages(Php, Python, Java etc.) and running on various platforms(Linux, Windows etc.) can use web services to exchange data over computer networks(Internet, Intranet, etc) in a manner similar to inter-process communication on a single computer.

PHP_Wincache - Wincache for PHP 5.4

** Update **: Microsoft's official release of Wincache 1.3 for PHP 5.4 is now available on the Wincache home page: http://www.iis.net/download/wincacheforphp

Here is Wincache version 1.3 BETA, for php 5.4 on IIS with FastCGI.

Note that Wincache also works under the following Apache configuration on Windows:


Download:
  • php_wincache.dll for PHP 5.4 NTS only, there is no thread-safe version.  This build is for PHP NTS x86 built with VC9, the standard PHP release for IIS (there's a link to Microsoft's builds at the bottom of this post, I didn't spot it before writing this, but it makes my build a bit pointless).

Microsoft has not officially released this code yet, so there must still be some problems to iron out.  I tested it for 20 minutes and it seemed to work fine, but I did not test the user cache functions, or make any measurements of time and memory consumption.


Links:

DOH!!

I took my own advice and did some exploring on IIS.net, and found a link to Microsoft's own pre-release builds for Wincache... here is the link: http://sourceforge.net/projects/wincache/files/development/.  I did quite a bit of searching before deciding to build my own, then found this one almost immediately after doing it!

    Contro strucrutes in Php: continue

    continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
    Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.
    continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

    <?php
    while (list($key, $value) = each($arr)) {
    if (!($key % 2)) { // skip odd members
    continue;
    }
    do_something_odd($value);
    }

    $i = 0;
    while ($i++ < 5) {
    echo "Outer<br />\n";
    while (1) {
    echo " Middle<br />\n";
    while (1) {
    echo " Inner<br />\n";
    continue 3;
    }
    echo "This never gets output.<br />\n";
    }
    echo "Neither does this.<br />\n";
    }
    ?>


    Omitting the semicolon after continue can lead to confusion. Here's an example of what you shouldn't do.

    <?php
    for ($i = 0; $i < 5; ++$i) {
    if ($i == 2)
    continue
    print "$i\n";
    }
    ?>


    One can expect the result to be :

    0
    1
    3
    4


    but this script will output :

    2


    because the return value of the print() call is int(1), and it will look like the optional numeric argument mentioned above.

    Woltlab Burning Board 2.3.3 Color Palette Fix for Opera

    There is a bug or lack or anything but user can not set color with color palette. Here is fix for this issue If you are using wBB 2.3.x

    1- Open your acp/templates/designpack_colorchooser.htm file with your text editor

    2- Find this block;

    x = 4 * ((document.all ? event.offsetX : e.layerX)-2);
    y = 4 * ((document.all ? event.offsetY : e.layerY)-2+5);

    3- Replace with this code;
    e=window.event;      // added
    e.layerX=e.offsetX; // added
    e.layerY=e.offsetY; // added

    x = 4 * ((document.all ? event.offsetX : e.layerX)-2);
    y = 4 * ((document.all ? event.offsetY : e.layerY)-2+5);


    4- Do the same things for Cache/templates/acp/designpack_colorchooser.htm file
    -------------------------------------------------------------------------------------

    Woltlab Burning Board 2.3.3 admin panelinde kullanılan Renk paleti Opera ile düzgün çalışması için yapılmış küçük bir güncelleme.

    1- acp/templates/designpack_colorchooser.htm dosyasını Text editörü ile açın

    2- Bu kod satırını bulun;
    x = 4 * ((document.all ? event.offsetX : e.layerX)-2);
    y = 4 * ((document.all ? event.offsetY : e.layerY)-2+5);

    3- Bu kod ile değiştirin;
    e=window.event;      // eklendi
    e.layerX=e.offsetX; // eklendi
    e.layerY=e.offsetY; // eklendi

    x = 4 * ((document.all ? event.offsetX : e.layerX)-2);
    y = 4 * ((document.all ? event.offsetY : e.layerY)-2+5);


    4- Aynı şeyi Cache/templates/acp/designpack_colorchooser.htm dosyası içinde yapın. Artık renk paletini Opera tarayıcısı ile birlikte rahatlıkla kullanabilirsiniz.

    where in php with mysql

    connection.inc.php :

    <?php
    $host='localhost';
    $user_name='root';
    $password='';
    @$con = mysql_connect("$host","$user_name","$password");
    if ($con)
     {echo '<br>host coonected<br>';}
    else
      {
      die('Could not connect ');
      }
    ?>

    where_table.php :
    <?php

    require 'connection.inc.php';

    if (mysql_select_db('test'))
    if($result=mysql_query("select * from test where sno=2",$con))
      {
         echo "<br><table><tr><td>sno</td><td>name</td></tr>";
        while($data = mysql_fetch_array($result))
      {
      echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
      }
      echo "</table><br>data selected<br>";
      }
    else
      {
      echo '<br>data not selected :'.mysql_error().'<br>';
      }

    mysql_close($con);
    ?>

    run " where_table.php "


    output :



    host coonected

    snoname
    2human
    2human

    data selected

    Why we learn PHP

    In this episode we'll see why we are trying to learn PHP and Why PHP is so much razzle-dazzle. The question is, there have many web programming languages throughout the world but why we want to learn PHP. I hope it is easy to answer these questions, so let’s start with us to discover the question.

    PHP is free. So you can easily use PHP without investing any costs.

    PHP language is platform independent. Independent means it supports Windows platform, Linux platform without any hassle. You can use any operating system and PHP perform same in all operating system.

    PHP is an object-oriented language. You cannot understand object-oriented languages now but I will discuss it. You can see object-oriented concept in our PHP object-oriented tutorial. PHP is now more popular because of object oriented capability.

    There are millions of people continuously develop websites with PHP web programming language. The demand of PHP web programmer increases day by day. So by learning PHP web development you can easily build your career.
      
    PHP was developed for web development. You want to develop dynamic website so PHP can help you to achieve your goal.

    I have one question why you came to this site. Absolutely to learn PHP and PHP web development. So this is our little initiative to help you. We hope you will enjoy our tutorial.

    Print full formated time using SafGetFullDateTime04

    <?
    //Pass 2005-01-22 01:05:00 and get the output Sun, Jan 22, 07 : 01:05:am
    function SafGetFullDateTime04($mytimestamp){ //send 2006-05-22 00:00:00 Output Sun, Mar 11, 07 : 01:05:am
    if ($timestamp!="0000-00-00 00:00:00") {
    list(
    $year,$month,$day,$hour,$minute,$second) = explode("-",str_replace(array(" ",":"),"-",$mytimestamp));
    $mytimestamp = date('d M, D, y',mktime($hour, $minute, $second, $month, $day, $year));
    $mytimestamp = $mytimestamp . "<br><font color=\"#808080\">" . date('h:i:a',mktime($hour, $minute, $second, $month, $day, $year)) . "</font>";
    return
    $mytimestamp;
    }
    }
    ?>

    Mudahnya Membuat Barcode dengan PHP


    Pada artikel kali ini saya akan mencoba membahas bagaimana membuat barcode dengan script PHP. Mudah-mudahan hal ini bisa membantu rekan-rekan dan juga khususnya para member yang kesulitan membuatnya.

    Eh… sebelum kita bahas bagaimana cara membuat script untuk mengenerate barcode ini pada studi kasus, ada baiknya kita tinjau terlebih dahulu tentang barcode.


    Apa sih barcode itu? Barcode adalah suatu simbol berbentuk garis-garis yang menyatakan suatu kode atau string karakter. Simbol ini dapat dibaca oleh suatu barcode scanner. Salah satu contoh barcode adalah simbol yang ada di produk di toko atau supermarket yang sering kita lihat.

    Ternyata… di dunia ini ada banyak sekali tipe barcode. Diantara sekian banyak tipe barcode, terdapat 6 kategori barcode berdasarkan kegunaannya, yaitu: barcode untuk keperluan retail, barcode untuk keperluan packaging, barcode untuk penerbitan, barcode untuk keperluan farmasi, barcode untuk keperluan non retail, serta barcode untuk keperluan lain.

    Barcode untuk keperluan retail, salah satu contohnya adalah UPC (Universal Price Codes), biasanya digunakan untuk keperluan produk yang dijual di supermarket. Barcode untuk packaging biasanya digunakan untuk pengiriman barang, dan salah satunya adalah barcode tipe ITF. Barcode untuk keperluan penerbitan, sering digunakan pada penerbitan suatu produk, misalkan barcode yang menunjukkan ISSN suatu buku. Sedangkan barcode untuk keperluan farmasi biasanya digunakan untuk identifikasi suatu produk obat-obatan. Salah satu barcode farmasi adalah barcode jenis HIBC. Sedangkan barcode untuk kepentingan non retail, misalkan barcode untuk pelabelan buku-buku yang ada di perpustakaan. Salah satu tipe barcode untuk keperluan non retail ini adalah Code 39.

    Referensi :
    http://www.agamik.co.uk
    http://www.makebarcode.com/specs/code_39.html

    Nah… pada pada artikel ini, saya hanya akan memfokuskan saja pada barcode Code 39. Barcode ini diperkenalkan oleh ANSI (American National Standards Institute). Karakter yang bisa dinyatakan ke dalam Code 39 meliputi digit angka 0-9, huruf kapital A-Z, karakter spasi, karakter ‘-’, ‘+’, ‘.’, ‘$’ dan ‘/’. Berikut ini adalah salah satu contoh tampilah barcode Code 39 untuk mensimbolkan suatu string ‘ROSIHAN ARI 1979′.



    Untuk menyatakan suatu karakter atau string ke dalam bentuk barcode Code 39, caranya adalah mengapit string tersebut dengan tanda asterisk (*). Sehingga pada contoh di atas, string ‘ROSIHAN ARI 1979′ ini harus diapit dengan tanda asterisk menjadi ‘*ROSIHAN ARI 1979*’. Apa akibatnya jika tidak diapit dengan asterisk? Simbol yang muncul nantinya tidak akan bisa dibaca oleh barcode scanner.

    OK… saya kira cukup pembahasan mengenai barcode, khususnya jenis Code 39 ini. Nah… kembali pada cara pembuatan barcode. Pembuatan barcode yang akan dibahas hanya akan difokuskan pada Code 39 yang diterapkan pada studi kasus pelabelan buku di perpustakaan.

    Pertanyaan pertama yang mungkin ada dalam benak Anda adalah bagaimana menciptakan simbol barcode, khususnya untuk Code 39. Ya… good question.

    Untuk membuat barcode Code 39, Anda bisa membeli suatu software untuk mengeneratenya. Beli??? Wah… gak ada uang tuh…  So… gimana donk?? He..3x, jangan khawatir karena meskipun Anda tidak punya uang, Anda tetap bisa membuatnya yaitu dengan mendownload True Type Font (TTF) khusus untuk barcode Code 39 ini. Free kah font ini? Ya… 100% free buat Anda. Dimanakah downloadnya? OK.. akan saya bocorkan kepada Anda link untuk downloadnya yaitu di bawah ini

    Download TTF Code 39

    Setelah Anda download, selanjutnya installah ke direktori font pada sistem operasi Anda. Jika Anda menggunakan Windows, installah ke direktori C:\WINDOWS\FONTS. OK… hanya itu caranya. Trus… bagaimana cara menggunakannya?

    OK… sekarang kita terapkan ke studi kasus untuk membuat label barcode yang menyatakan kode-kode buku yang ada di perpustakaan. Diharapkan nantinya kode barcode ini akan ditempelkan pada setiap buku dan dimanfaatkan untuk mempermudah transaksi di perpustakaan. Ketika proses peminjaman atau pengembalian buku, petugas perpustakaan tidak perlu mengetikkan kode buku secara manual di komputer namun hanya
    melakukan scanning pada label barcode ini.

    Nah… kita mulai membuatnya. Pertama-tama kita siapkan database dan tabel untuk keperluan penyimpanan data buku perpustakaan. Ini salah satu contoh query untuk membuat tabel dan datanya:

    view sourceprint?
    CREATE TABLE buku (
    kodeBuku varchar(5),
    judulBuku text,
    author varchar(20),
    jenis varchar(10),
    PRIMARY KEY (kodeBuku)
    );
    view sourceprint?
    INSERT INTO `buku` VALUES ('A0001', 'Pemrograman Pascal', 'Rosihan Ari Yuana', 'Referensi');
    INSERT INTO `buku` VALUES ('A0002', 'Pemrograman PHP', 'Dwi Amalia Fitriani', 'Referensi');
    INSERT INTO `buku` VALUES ('A0003', 'Pengantar Jaringan Komputer', 'Faza Fauzan Kh.', 'Referensi');
    INSERT INTO `buku` VALUES ('A0004', 'Teknologi Digital', 'Nada Hasanah', 'Referensi');
    INSERT INTO `buku` VALUES ('A0005', 'Pemrograman ASP .NET', 'Muh. Ahsani Taqwim', 'Referensi');
    OK… dari SQL di atas dapat Anda lihat terdapat sampel buku sejumlah 5 buah. He.. 3x semua anggota keluarga saya ternyata para pengarang buku

    Nah… bagaimana cara membuat script PHP untuk mengenerate label barcode dari kode semua buku di atas? Bentar… sabar… sebelum kita mulai buat, kita hendaknya pikirkan desain tampilan outputnya.

    Misalkan kita ingin nantinya muncul label barcode kode buku yang disusun seperti halnya tabel yang memiliki 2 kolom. Trus… untuk setiap barcode, bagian bawahnya akan terdapat kode buku yang dapat dibaca oleh kita. Mengapa kode buku ini ikut tercantum di bawah barcodenya? Ya… siapa tahu suatu saat barcode scannernya rusak. Bila label barcode ini tidak ada kode buku yang bisa kita baca, bisa-bisa kesulitan untuk mendatanya ketika transaksi di perpustakaan berlangsung.

    OK… itu desain tampilannya. Now… let’s start to create the script!

    view sourceprint?
    <?php

    // koneksi ke database
    mysql_connect("dbhost", "dbuser", "dbpass");
    mysql_select_db("dbname");

    $query = "SELECT * FROM buku";
    $hasil = mysql_query($query);

    // setting banyaknya kolom
    $kolom = 2;

    // membuat tabel berisi label barcode
    echo "<table border='1'>";

    $counter = 1;
    while ($data = mysql_fetch_array($hasil))
    {
    if (($counter-1) % $kolom == 0) echo "<tr>";
    echo "<td align='center' style='padding: 5px'><font face='Free 3 of 9' size='20'>*".$data['kodeBuku']."*</font><br />".$data['kodeBuku']."</td>";
    if ($counter % $kolom == 0) echo "</tr>";
    $counter++;
    }

    echo "</table>";
    ?>
    Konsep membuat label berisi barcode ke dalam bentuk tabel 2 kolom ini adalah sebagai berikut

    Secara umum, apabila kita membuat tabel dengan n buah kolom maka struktur htmlnya adalah seperti di bawah ini:

    view sourceprint?
    <table>
    <tr><td>...</td><td>...</td><td>...</td>... (akan diulang n kali)</tr>
    <tr><td>...</td><td>...</td><td>...</td>... (akan diulang n kali)</tr>
    .
    .
    .
    </table>
    Nah… dari struktur di atas tampak bahwa untuk setiap baris tabel terdapat n kali tag <td>…</td> yang diulang. Begitu sudah terdapat n kali, maka akan membentuk baris baru dan langkah yang sama akan diulangi lagi.

    Dalam pemrograman, untuk mengimplementasikan ide di atas, kita bisa menggunakan bantuan suatu counter. Counter ini akan berjalan mulai dari 1, 2, 3, … hingga sejumlah data yang diinginkan. Bila counter telah mencapai bilangan yang merupakan kelipatan n, maka baris tabel akan diakhiri (cetak </tr>) dan selanjutnya membuat baris tabel baru (cetak <tr>), lihat baris 19 dan 21 pada script PHP di atas.

    Oya… untuk mengenerate suatu string menjadi kode barcode Code 39 menggunakan TTF yang telah kita download di atas caranya adalah gunakan tag

    view sourceprint?
    1.
    <font face="Free 3 of 9" size="...">...</font>
    dengan atribut ‘size’ nantinya akan diisi dengan value yang menyatakan ukuran simbol barcode. Pada contoh script di atas menggunakan size=”20″.

    Oya… jangan lupa mengaapit string yang akan dibuat barcodenya dengan tanda asterisk (*).
    Fiuh… jadi juga akhirnya script dan artikel ini. Panjang sekali yah artikelnya…. but anyway… mudah-mudahan ada manfaatnya buat Anda semuanya.

    PHP Forms

    This is the fun part! sooner or later, we all want to collect user data. We can build nifty data collector using HTML and PHP GET/POST .

    Create a HTML form Something like.. Easier if you do it in HTML editor. and name it "myform.html"

    <form name="form1" method="post" action="collect.php">
    Name :
    <input type="text" name="name">
    <br>
    Email :
    <input type="text" name="email">
    <br>
    <input type="submit" name="Submit" value="Submit">
    </form>

    Now Create another file, this time "collect.php"
    <?php
    echo $_POST["name"] ."<br />";
    echo $_POST["email"];
    ?>
    That's it... After collecting user information, you can do whatever you want In "collect.php" , send email.. send data to database, evaluate.. or just flush it.. etc

    See the output :

    myform.html :




    collect.php :