 |
ต้องการแก้ไข query เพื่อไปทำโค้ตแบ่งหน้า แต่แก้ไม่เป็น งง กับ oop อ่ะครับ รบกวนด้วยครับ |
|
 |
|
|
 |
 |
|
classdb.php
<?php
class CReturn {
const FAIL = false;
const SUCCESS = true;
public static function error($error_msg){
return array(self::FAIL, $error_msg);
}
public static function success($value){
return array(self::SUCCESS, $value);
}
}
class initDb {
private static $host = "localhost";
private static $user = "root";
private static $pass = "mysql";
private static $db = "db_test1";
public static function connect(){
$db_conn = mysql_pconnect(self::$host, self::$user, self::$pass) or die(mysql_error());
mysql_select_db(self::$db, $db_conn) or die(mysql_error());
mysql_query("SET NAMES UTF8;");
return $db_conn;
}
}
class DbAbstract {
protected $db_conn;
protected $table_name;
protected $jointable_name
public $_data;
public function __construct($db_conn){
$this->db_conn = $db_conn;
}
protected function _get($sql){
$this->_data = null;
$result = mysql_query($sql);
if(mysql_errno($this->db_conn) !== 0)
return CReturn::error("Mysql error [".mysql_errno($this->db_conn)."] : ".$sql."<br />".mysql_error($this->db_conn));
while($res = mysql_fetch_assoc($result)) {
$this->_data[] = $res;
}
return CReturn::success(mysql_num_rows($result));
}
protected function getTableName(){
return $this->table_name;
}
protected function getJoinTableName(){
return $this->jointable_name;
}
}
?>
classemployee.php
class DbEmployee extends DbAbstract {
protected $table_name = "t_employee";
protected $jointable_name = "t_position";
public function getEmployee(){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()}
LEFT JOIN {$this->getJoinTableName()} ON {$this->getTableName()}.position_id = {$this->getJoinTableName()}.position_id");
}
public function searchEmployee($value){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()}
LEFT JOIN {$this->getJoinTableName()} ON {$this->getTableName()}.position_id = {$this->getJoinTableName()}.position_id WHERE {$this->getTableName()}.emp_name LIKE '%$value%' OR {$this->getTableName()}.lastname LIKE '%$value%' OR {$this->getJoinTableName()}.position_name LIKE '%$value%'");
}
public function getEmployeeById($id){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()} WHERE emp_id ='$id'");
}
}
ส่วนที่นำโค้ตมาใช้ โค้ตแบ่งหน้าผมใช้โค้ต
https://www.thaicreate.com/community/php-mysql-pagination.html
employeeindex.php
<?php
$search = $_POST['keyword'];
if($search == ""){
$Tbemployees = new DbEmployee($db_conn);
list($result, $value) = $Tbemployees->getEmployee();
$num_rows = var_export($value,true); //เช็คจำนวนแถว
}else{
$Tbemployees = new DbEmployee($db_conn);
list($result, $value) = $Tbemployees->searchEmployee($search);
$num_rows = var_export($value,true); //เช็คจำนวนแถว
//print "<b>Query result</b> = ".var_export($result,true)."<br />";
//print "<b>Num rows/Error msg</b> = '".$value."'<br />";
//print "<b>data</b> = ".var_export($Tbemployees->_data,true)."<br />";
//print "<br />";
}
if($num_rows > 0){
$Per_Page = 10; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($num_rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($num_rows % $Per_Page)==0)
{
$Num_Pages =($num_rows/$Per_Page) ;
}
else
{
$Num_Pages =($num_rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by employee_id ASC LIMIT $Page_Start , $Per_Page"; // ตรงนี้อ่ะครับที่ผมงง ผมจะแก้ไข queryที่ผมได้สร้าง จาก classemployee.php
$objQuery = mysql_query($strSQL); // ตรงนี้ก็ด้วยครับ
foreach($Tbemployees->_data as $city => $detail)
{
print "<tr>";
print "<td> $detail[emp_id]</td>";
print "<td> $detail[emp_name]</td>";
print "<td> $detail[lastname]</td>";
print "<td> $detail[position_name]</td>";
print "<td> $detail[emp_phone]</td>";
print "<td> $detail[email]</td>";
print "<td><a href=\"formeditemployee.php?emp_id=$detail[emp_id]\"><img src=\"../images/onebit_20.png\" width=\"24\" height=\"24\"></a></td>";
print "<td><a href=\"deleteemployee.php?emp_id=$detail[emp_id]\"><img src=\"../images/onebit_35.png\" width=\"24\" height=\"24\"></a></td>";
print "</tr>";
}
}
?>
</tbody>
<thead>
<tr height:"2%">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
</table>
<br class="spacer" />
</div>
<?php
initDb::Close();
?>
ขอบคุณครับ
Tag : PHP, MySQL, HTML/CSS, JavaScript, jQuery
|
|
 |
 |
 |
 |
Date :
2011-10-22 12:56:33 |
By :
GhostLocal |
View :
1300 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หากทำ OOP มันยาก ก็ใช้แบบธรรมดานี่แหละ ง่ายดีกว่าครับ 
|
 |
 |
 |
 |
Date :
2011-10-23 20:57:01 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไปไม่ถูกเลย พอดีว่าจะลอง oop อ่ะครับพี่วิน
|
 |
 |
 |
 |
Date :
2011-10-25 11:00:23 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
class CReturn {
const FAIL = false;
const SUCCESS = true;
public static function error($error_msg){
return array(self::FAIL, $error_msg);
}
public static function success($value){
return array(self::SUCCESS, $value);
}
ผมยากทราบว่า บรรทัดนี้อ่ะครับ return array(self::FAIL, $error_msg); ตัวแปร self กับ value คือค่าอะไร
ช่วยตอบทีนะครับพอดีผมไม่ค่อยรู้เรื่องนี้เท่าไรอ่ะครับ
|
 |
 |
 |
 |
Date :
2011-11-25 23:35:08 |
By :
ซายส์ครับ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|