|
PHP Class create table and search |
create_table.class.php
<?
<?php
#################################################################################################
# --- Please do not delete this header ----
# DS CreateTable v1.1.0
# Class Name : createTable
# Version : v1.1.0
# Requirement : PHP5 >
# Build Date : December 4, 2009 - Friday
# Developer : Narong - [email protected]
# Licence : Free License (c) 2009
#
#################################################################################################
/* --- Configuration connect to server ---- */
# Connect to localhost
$serv_name = "localhost";
$dbas_name = "test";
$user_name = "root";
$user_pass = "1234";
$conn = @mysql_connect($serv_name, $user_name, $user_pass) or die
("ERROR : ไม่สามารถติดต่อเซิฟเวอร์ ได้ค่ะ!!!<br /> Mysql report : ".mysql_error()); # Create connection
@mysql_select_db($dbas_name, $conn) or die
("ERROR: ไม่สามารถเลือกฐานข้อมูล ได้ค่ะ!!!<br /> Mysql report : ".mysql_error()); # Select database
@mysql_query("SET NAMES UTF8") or die
("ERROR : ไม่สามารถเซ็ตอ็นโค๊ดดิ้ง ได้ค่ะ!!!<br /> Mysql report : ".mysql_error()); # SET database encoding
class table{
protected $table;
protected $headers = array();
protected $data = array();
protected $attr = array();
protected $css_class;
# set table attribute
public function setTableAttribute($attribute) {
$this->attr = $attribute;
}
# set table header
public function setTableHeader($header) {
$this->headers = $header;
}
# set table body
public function setTableData($data) {
$this->data = $data;
}
# print table
public function createTable() {
# open table tag
$this->table = '<table id="'.$this->attr[0].'" border="1" width="'.
$this->attr[1].'">';
#begin table head
$this->table .= '<thead>';
$this->table .= '<tr>';
foreach($this->headers as $val) {
$this->table .= '<th>'.$val.'</th>';
}
$this->table .= '</tr>';
$this->table .= '</thead>';
#end table head
# begin table body
$this->table .= '<tbody>';
if($this->data) {
foreach($this->data as $index=>$rows) {
($index%2 == 1) ? $this->css_class = $this->attr[2]
: $this->css_class = $this->attr[3];
$this->table .= '<tr '.$this->css_class.'>';
foreach ($rows as $key=>$val)
$this->table .= '<td>'.$rows[$key].'</td>';
$this->table .= '</tr>';
}
}
else
echo 'ไม่พบข้อมูล';
$this->table .= '</tbody>';
# end table body
# close table tag
$this->table .= '</table>';
echo $this->table;
}
# print array
public function show($arr) {
echo '<pre>'; print_r($arr); echo '</pre>';
}
}
?>
?>
create_table.class.php
<?PHP
########################################################################################################################
#
# KG Pager v2.0.1
# Class Name : KG Pager Class
# Version : 2.0.1
# Requirement : PHP4 >
# Build Date : December 17, 2007 - Monday
# Developer : Muharrem ERİN (TÜRKİYE) - [email protected] - muharremerin.com - mhrrmrnr.com - kisiselgunce.com
# Licence : GNU General Public License (c) 2007
#
########################################################################################################################
// pager class
class kgPager {
var $total_records = NULL;
var $start = NULL;
var $scroll_page = NULL;
var $per_page = NULL;
var $total_pages = NULL;
var $current_page = NULL;
var $page_links = NULL;
// total pages and essential variables
function total_pages ($pager_url, $total_records, $scroll_page, $per_page, $current_page) {
$this->url = $pager_url;
$this->total_records = $total_records;
$this->scroll_page = $scroll_page;
$this->per_page = $per_page;
if (!is_numeric($current_page)) {
$this->current_page = 1;
}else{
$this->current_page = $current_page;
}
if ($this->current_page == 1) $this->start = 0; else $this->start = ($this->current_page - 1) * $this->per_page;
$this->total_pages = ceil($this->total_records / $this->per_page);
}
// page links
function page_links ($inactive_page_tag, $pager_url_last) {
if ($this->total_pages <= $this->scroll_page) {
if ($this->total_records <= $this->per_page) {
$loop_start = 1;
$loop_finish = $this->total_pages;
}else{
$loop_start = 1;
$loop_finish = $this->total_pages;
}
}else{
if($this->current_page < intval($this->scroll_page / 2) + 1) {
$loop_start = 1;
$loop_finish = $this->scroll_page;
}else{
$loop_start = $this->current_page - intval($this->scroll_page / 2);
$loop_finish = $this->current_page + intval($this->scroll_page / 2);
if ($loop_finish > $this->total_pages) $loop_finish = $this->total_pages;
}
}
for ($i = $loop_start; $i <= $loop_finish; $i++) {
if ($i == $this->current_page) {
$this->page_links .= '<span '.$inactive_page_tag.'>'.$i.'</span>';
}else{
$this->page_links .= '<span><a href="'.$this->url.$i.$pager_url_last.'">'.$i.'</a></span>';
}
}
}
// previous page
function previous_page ($previous_page_text, $pager_url_last) {
if ($this->current_page > 1) {
$this->previous_page = '<span><a href="'.$this->url.($this->current_page - 1).$pager_url_last.'">'.$previous_page_text.'</a></span>';
}
}
// next page
function next_page ($next_page_text, $pager_url_last) {
if ($this->current_page < $this->total_pages) {
$this->next_page = '<span><a href="'.$this->url.($this->current_page + 1).$pager_url_last.'">'.$next_page_text.'</a></span>';
}
}
// first page
function first_page ($first_page_text, $pager_url_last) {
if ($this->current_page > 1) {
$this->first_page = '<span><a href="'.$this->url.'1'.$pager_url_last.'">'.$first_page_text.'</a></span>'; // :)
}
}
// last page
function last_page ($last_page_text, $pager_url_last) {
if ($this->current_page < $this->total_pages) {
$this->last_page = '<span><a href="'.$this->url.$this->total_pages.$pager_url_last.'">'.$last_page_text.'</a></span>';
}
}
// pages functions set
function pager_set ($pager_url, $total_records, $scroll_page, $per_page, $current_page, $inactive_page_tag, $previous_page_text, $next_page_text, $first_page_text, $last_page_text) {
$this->total_pages($pager_url, $total_records, $scroll_page, $per_page, $current_page);
$this->page_links($inactive_page_tag, $pager_url_last);
$this->previous_page($previous_page_text, $pager_url_last);
$this->next_page($next_page_text, $pager_url_last);
$this->first_page($first_page_text, $pager_url_last);
$this->last_page($last_page_text, $pager_url_last);
}
}
?>
create_table.class.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Class Create Table</title>
<style type="text/css">
body { font-size:12px; font-family:Tahoma, Geneva, sans-serif; }
#ex { border-collapse:collapse; border-color:#bbb; font-size:11px; font-family:Tahoma, Geneva, sans-serif; }
#ex thead { background-color:#666; color:#FFF; }
#ex thead th { background-color:#666; color:#FFF; height:30px; border-color:#eee;}
#ex tbody td { text-align:center; height:25px; padding-left:10px; padding-right:10px; border-color:#bbb;}
#ex tbody td span.name { color:#444; }
#ex tbody td span.sname { color:#444; }
#ex tbody tr:hover { background-color:#F93;}
.odd { background-color:#fff; }
.even { background-color:#eee; }
.pager_links a { text-decoration:none; color:#ff3300; background:#fff; border:1px solid #e0e0e0;
padding:1px 4px 1px 4px; margin:2px; }
.pager_links a:hover { text-decoration:none; color:#3399ff; background:#f2f2f2; border:1px solid #3399ff;
padding:1px 4px 1px 4px; margin:2px; }
.current_page { border:1px solid #333; padding:1px 4px 1px 4px; margin:2px; color:#333; }
a:link { text-decoration: none; }
a:hover { text-decoration: none; color: #FFF; }
a:active { text-decoration: none; color: #F00; }
</style>
</head>
<body onload="document.getElementById('txtSearch').focus()">
<?php
require_once('./class/create_table.class.php');
require_once('./class/kgPager.class.php');
# สร้าง object ตราราง
$table = new table();
#
$str_search = ($_POST['txtSearch']) ? $_POST['txtSearch'] : '';
$key_search = ($_GET['keyword']) ? $_GET['keyword'] : '';
$curr_page = ($_GET['page']) ? $_GET['page'] : '';
# คำสั่ง query
$command = 'SELECT * FROM test_table';
# ลิงค์สำหรับการแบ่งหน้า
$pager_url = 'index.php?page=';
if(isset($str_search) && empty($key_search)) {
$command = $command." WHERE name like '%{$str_search}%'";
$pager_url = 'index.php?keyword='.$str_search.'&page=';
}
else if(isset($key_search) && isset($curr_page) && !isset($_POST['txtSearch'])) {
$command = $command." WHERE name like '%{$key_search}%'";
$pager_url = 'index.php?keyword='.$key_search.'&page=';
}
else if(isset($str_search) && isset($key_search)) {
$command = $command." WHERE name like '%{$str_search}%'";
$pager_url = 'index.php?keyword='.$str_search.'&page=';
}
# get object result from database
$result = mysql_query($command);
# จำนวน record ทั้งหมด
$total_records = mysql_num_rows($result);
# จำนวน scroll page
$scroll_page = 5;
# จำนวน record ต่อหนึ่ง page
$per_page = 7;
# หน้าปัจจุบัน
($str_search) ? $current_page = 1 : $current_page = $_GET['page'];
# รูปแบบปุ่มของหน้าปัจจุบัน
$inactive_page_tag = 'class="current_page"';
# หน้าก่อนหน้า
$previous_page_text = '< ก่อนหน้า';
# หน้าถัดไป
$next_page_text = 'หน้าถัดไป >';
# ไปหน้าแรก
$first_page_text = '<< หน้าแรก';
# ไปหน้าสุดท้าย
$last_page_text = 'หน้าสุดท้าย >>';
# สร้าง object ในการแบ่งหน้า
$pager = & new kgPager();
# เรียกเมธอด pager_set เพื่อกำหนดค่าต่างๆ ให้กับ class
$pager->pager_set($pager_url, $total_records, $scroll_page, $per_page, $current_page, $inactive_page_tag, $previous_page_text, $next_page_text, $first_page_text, $last_page_text);
# form search box
echo '<form action="" method="post">
<label>ค้นหา : </label><input type="text" name="txtSearch" id="txtSearch" /> <input type="submit" name="submit" value="ค้นหา" />
</form><br />';
echo '<p><strong>จำนวนทั้งหมด : </strong>'.$total_records.' แถว '.$pager->total_pages;
$result = mysql_query($command." ORDER BY id ASC LIMIT ".$pager->start.", ".$pager->per_page) or die (mysql_error());
# พิมพ์เลขหน้า
echo '<p class="pager_links">';
echo isset($pager->first_page) ? $pager->first_page : '';
echo isset($pager->previous_page) ? $pager->previous_page : '';
echo isset($pager->page_links) ? $pager->page_links : '';
echo isset($pager->next_page) ? $pager->next_page : '';
echo isset($pager->last_page) ? $pager->last_page : '';
echo '</p>';
# fetch result store to array data
while($rs = mysql_fetch_assoc($result)) {
$data[] = array(
$rs['id'],
'<span class="name">'.$rs['name'].'</span>',
'<span class="sname">'.$rs['surname'].'</span>',
'<a href="mailto:[email protected]">'.$rs['email'].'</a>',
'<a href="javascript:alert(\''.$rs['id'].'\');"><img src="images/icons/User-Edit.png" border="0" /></a>',
'<a href="f_editmember.php?id='.$rs['id'].'"><img src="images/icons/User-Cancel.png" border="0" /></a>');
}
# show array data
# $table->show($data);
# parameter of attribute
$attr = array('ex', '', 'class="odd"', 'class="even"');
# parameter of header
$head = array('ID', 'Name', 'Surname', 'Email', 'Edit','Delete');
# call member function to set table attribute
$table->setTableAttribute($attr);
# call member function to set table header
$table->setTableHeader($head);
# call member function to set table body
$table->setTableData($data);
# print table
$table->createTable();
?>
<body>
</body>
</html>
DEMO-:Download
Output
|
|
|
|
|
|
|
|
By : |
ranarong
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
|
|
Create Date : |
2010-01-04 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|