Freelancer's Playground! New knowledges every now and then

20Feb/0824

Mimic Oracle’s LOV Feature for Web Application Part 2: Add Database Support

On my previous tutorial, you have seen how to create a Lookup Grid. That's just basic, without database support.
In this tutorial, I'll enhance the script with database and search support. What you'll need first is obviously a database server. If you don't have the tutorial file, please download it from my previous tutorial. Next think to do is, create a file which contain database information. Let's call it db_conn.php.

<?php
$conn = mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('database_name');
?>

With database information obtained, now we'll move to lov.php file. Open it and include db_conn.php.

include('db_conn.php');

Next, delete all <row></row> xml data. Add this code right after header("Content-Type: text/xml");.

$x = isset($_GET['query']) ? $_GET['query'] : '';
$addSQL = "";
if(!empty($x)){
	$addSQL = "WHERE name LIKE '%".mysql_escape_string($x)."%'";
}
$sql = "SELECT * FROM friends $addSQL";
$rs = mysql_query($sql, $conn);

On place where <row></row> was, add this code:

<?php
	$select = 'selected="1"';
	while(false !== ($r = mysql_fetch_array($rs))){
		echo '
	<row id="'.$r['id'].'" '.$select.'>
      <cell>'.$r['id'].'</cell>
      <cell>'.$r['name'].'</cell>
      <cell>'.$r['city'].'</cell>
      <cell>'.$r['phone'].'</cell>
      <cell>'.$r['email'].'</cell>
    </row>
		';
		$select = "";
	}
?>

Now the XML generator part is ready, now we'll head to HTML part.
Please add this code right before <div id="gridbox" height="100%"></div>

<div id="searchbox">
	Search&nbsp;<input type="text" id="searchText" value="">&nbsp;
	<input type="button" onclick="doLimit(document.getElementById('searchText').value)" value="Search">
</div>

And this javascript function inside HTML HEAD tag.

<script language="javascript" type="text/javascript">
	var mygrid = null;
	function doLimit(txt){
		mygrid.clearAll();
		mygrid.loadXML("lov.php?reqType=getXML&query="+txt);
	}
</script>

That's it, save your lov.php file and run it to test. You can also try in online here
and download the example file here. Any question you might have, you can write in here in comment form below.
I'll try to answer when I have time.

  • http://www.abuaisyah.org/ exblopz

    Rief, umur loe koq nggak nambah-nambah? Tetep aja 23 dari dulu :D

  • http://www.abuaisyah.org/ exblopz

    Rief, umur loe koq nggak nambah-nambah? Tetep aja 23 dari dulu :D

  • http://bayu.freelancer.web.id/ ariefbayu

    huehuehue, emang masih 23 koq. Masih 8 bulan lagi untuk jadi 24.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    huehuehue, emang masih 23 koq. Masih 8 bulan lagi untuk jadi 24.

  • http://pedapa.com/ nananias

    Next think to do is, create a file which contain database information.

    next think to do?

    please note that this blog use a first comment moderation system?

    you haven’t post

    errr… sayang pedapa ngga cut the ice ya ;)

  • http://pedapa.com nananias

    Next think to do is, create a file which contain database information.

    next think to do?

    please note that this blog use a first comment moderation system?

    you haven’t post

    errr… sayang pedapa ngga cut the ice ya ;)

  • http://bayu.freelancer.web.id/ ariefbayu

    #nana:asik, pancingan saya nyampe. Trus yang bener gimana bu guru?

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    #nana:asik, pancingan saya nyampe. Trus yang bener gimana bu guru?

  • http://mobihack007.blogspot.com/ mobiheker

    ilmu baru, slam kenal

  • http://mobihack007.blogspot.com mobiheker

    ilmu baru, slam kenal

  • http://rlyna.wordpress.com/ rlyna

    di sini tesnya ga jalan om. ntu mesti ngidupin js yah?

    *polos*

  • http://rlyna.wordpress.com rlyna

    di sini tesnya ga jalan om. ntu mesti ngidupin js yah?

    *polos*

  • http://bayu.freelancer.web.id/ ariefbayu

    #rlyna:gak, tapi mesti pake elinks.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    #rlyna:gak, tapi mesti pake elinks.

  • ocidive

    Mau tanya donk kk, aku punya tugas kuliah nih tentang VB 6. pertanyaanya seperti ini ” Apakah memungkinkan VB6 untuk menggenerate aplikasi wireless, dengan menggunakan sarana bluetooth. Bagaimana menurut anda, dan berikan beberapa contoh syntax yang sederhana dari aplikasi tersebut. ”

    Kalo bisa dibalas secepatnya ya kk, thx be4 ^_*

  • ocidive

    Mau tanya donk kk, aku punya tugas kuliah nih tentang VB 6. pertanyaanya seperti ini ” Apakah memungkinkan VB6 untuk menggenerate aplikasi wireless, dengan menggunakan sarana bluetooth. Bagaimana menurut anda, dan berikan beberapa contoh syntax yang sederhana dari aplikasi tersebut. ”

    Kalo bisa dibalas secepatnya ya kk, thx be4 ^_*

  • http://bayu.freelancer.web.id/ ariefbayu

    #ocidive:jawaban saya bisa, tapi untuk source code sepertinya anda mesti googling dulu deh, soalnya saya sudah setahun lebih gak bermain di windows.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    #ocidive:jawaban saya bisa, tapi untuk source code sepertinya anda mesti googling dulu deh, soalnya saya sudah setahun lebih gak bermain di windows.

  • ocidive

    Owh..Thx ya kk arief. Btw ada link panduannya ga? saya ud search di google ttg VB 06 ini. tp rata2 make bhs inggris & ribet. mungkin kk tau dimana saya bisa mencarinya. Thx be4

  • ocidive

    Owh..Thx ya kk arief. Btw ada link panduannya ga? saya ud search di google ttg VB 06 ini. tp rata2 make bhs inggris & ribet. mungkin kk tau dimana saya bisa mencarinya. Thx be4

  • http://ariopunya.web.id/ ario dipoyono

    wah… berguna sekali ilmunya.. matur nuwun

  • http://ariopunya.web.id ario dipoyono

    wah… berguna sekali ilmunya.. matur nuwun

  • fl!x

    wah…… makasih banget ilmunya.

    kenapa ya kalo pake ie7 hasilnya bagus sedangkan pake ff3 ada yang kepotong?

  • fl!x

    wah…… makasih banget ilmunya.

    kenapa ya kalo pake ie7 hasilnya bagus sedangkan pake ff3 ada yang kepotong?