RSS
email

menampilkan record dan query pada PHP

Jika Anda ingin menampilkan hasil query yang berbeda, seperti jika banyak record ditemukan, maka dengan source code PHP berikut ini dapat menampilkan :

First ....4,5,6,7,8... Last
Atau 1,2,3,4,5...Last
Atau ...15,16,17,18,19

Source code :

$global_db = mysql_connect('localhost', $username, $password);
mysql_select_db($dbname, $global_db);
// query to get the number of records
$query = "SELECT Field1, Field2 FROM $TABLE";
$result = mysql_query($query) or die("ERROR");
$num_record = mysql_num_rows($result);
if($num_record > $display) { // Only show 1,2,3,etc. when there are more records found that fit on 1 page
// when the page is loaded first...
if(empty($pagenr)) {
$pagenr = 1;
}
// some variables
$display = 10; // number of records to display per page
$max_pages_to_print = 7; // number of pages to show, if you change this you also have to change the variable 'middlenumber', for example:increase this one with two, increase middlenumber with one
$startrecord = $pagenr * $display; // first record to show from the queryresult
$num_pages = intval($num_record / $display) + 1; // total number of pages
$loopcounter = 0; // counter for whileloop
$currentpage = $pagenr; // Page where we are at the moment
$middlenumber = 3; // Number will be decreased from variable currentpage in order to get the currentpage always in the middle
$colourcounter = 0; // Variable to change the background-color of the
$i = 1; // variable that will print 1,2,3,etc..
$x = 0; // variable i use to put always the current, marked page in the middle
// actual stuff starts here
print("");
if($currentpage >= $max_pages_to_print) {
print("");
}
//BEGIN LOOP
while($loopcounter < $max_pages_to_print) { if($currentpage >= $max_pages_to_print) { // If user clicks om page higher than $max_pages_to_print
// Mark current page
if($currentpage == $i) {
print(""); // print pagenumbers
$i += 1; //increase pagenr
$loopcounter += 1; // increase loopcounter
}
// End marking
if($i > $num_pages) { // if last page has been printed, exit loop
break;
}
if($x == 0) {
$i = $currentpage - $middlenumber; // current page will always be printed in the middle
}
print(""); // print pagenumbers
$x = $x + 1;
$i += 1; //increase pagenr
$loopcounter += 1; // increase loopcounter
}
else { // Else user clicks on a pagenumber lower $max_pages_to_print
// Mark current page
if($currentpage == $i) {
print(""); // print pagenumbers
$i += 1; //increase pagenr
$loopcounter += 1; // increase loopcounter
}
// End marking
if($i > $num_pages) { // if less than $max_mages_to_print, exit loop
break;
}
print(""); // print pagenumbers
$i += 1; //increase pagenr
$loopcounter += 1; // increase loopcounter
} // End if
}
// END LOOP
if(($num_pages > $max_pages_to_print AND // notice the user that there are more pages
$i <= $num_pages)) { print("");
}
print("
First...$i $ i $i $ i ...
");
$startrecord = $startrecord - $display; // Set startrecord to the right position
// Some calculation for the lastrecord
if($currentpage == $num_pages) { // Last page...
$lastrecord = $num_record; // so $lastrecord = $num_record
}
else {
$lastrecord = ($currentpage * $display);
}
} // End of the first if-statement
// Some info
print("




You are now on page $currentpage
There are $num_pages pages in total
$num_record records are spread over $num_pages pages
Current display : $startrecord - $lastrecord
");
// End info
// actual query, watch the end($startrec
// ord, $display)
$query2 = "SELECT Field1, Field2 FROM $TABLE LIMIT $startrecord, $display";
$result2 = mysql_query($query2) or die("ERROR");

Queryresult PHP

// print results on screen
print("");
while(list($Field1, $Field2) = mysql_fetch_row($result2)) {
if ($colorcounter == 0) {
$colorbg = "#ECE28B";
}
else {
$colorbg = "#F4EFC1";
$colorcounter = $colorcounter - 2;
}
print("
< color="#FFFFFF">Fieldname1Fieldname2
$Field1$Field2
");

Sebelum menulis coding, ubahlah nama username, password dan dbname, untuk koneksi ke database Anda
Keterangan variabel :

* $display : jumlah dari record yang akan ditampilkan per halaman
* $max_pages_to_print : jumlah halaman yang akkan dicetak(1,2,3,dsb)
* $middlenumber : hanya diubah ketika Anda mengubah variable $max_pages_to_print. Sebagai contoh, jika Anda menambah $max_pages_to_print dengan 2, maka tambahkan juga $middlenumber dengan 1. Begitu pula jika $max_pages_to_print dikurangi menjadi 2, maka kurangilah $middlenumber dengan 1



Bookmark and Share

2 comments:

rha2 said...

,,addu...kok ribet ci,,
,,akku mce gag tw makcutnya,,,pendekar berkuda,,
,
,bhahahahaha,

khafi said...

wah... dah jago PHP nih kayaknya...

Post a Comment

leave comments..!!