วันจันทร์ที่ 18 สิงหาคม พ.ศ. 2557

Query data while

<?
include_once("../includes/class_mysql.php");
include_once("../includes/config.php");

 $db=new DB();
 $db->connectdb(DB_NAME,DB_USER,DB_PASS);
 $res=$db->querydb("SELECT * FROM ".TB_CATEGORY." order by seq asc ");

?>
</br>
<H3 class="text-primary">&nbsp;&nbsp;Edit Drop</H3>
<table class="table table-hover">
<tr>
<th width="59" bgcolor="#f5f5f5"><center><b>ลำดับ</b></center></th>
<th width="300" bgcolor="#f5f5f5"><center><b>ชื่อหัวข้อ</b></center></th>
<th width="85" bgcolor="#f5f5f5"><center><b>เรียงลำดับ</b></center></th>
<th width="62" bgcolor="#f5f5f5"><center><b>Edit</b></center></th>
<th width="64" bgcolor="#f5f5f5"><center><b>Delete</b></center></th>
</tr>
<?
while($arr=$db->fetch($res)){
?>
<tr>
<td><center><?=$arr[id];?></center></td>
<td><center><?=$arr[name];?></center></td>
<td><center><?=$arr[seq];?></center></td>
<td><center><a href="F_Edit.php?id=<?=$arr[id];?>"><img src="../images/b_edit.png" width="16" height="16"></a></center></td>
<td width="64"><center><a href="Delete.php?id=<?=$arr[id];?>" onClick="return confirm('Are you sure you want to delete this item?');"><img src="../images/b_drop.png" width="16" height="16" /></a></center>
</tr>
<?
}
?>
</table>
<a href="../Category">&nbsp;&nbsp;<code>..Back</code></a>
<?
$db->closedb();
?>

PHP update data

<?

//*** Update Record ***//
include_once("../includes/class_mysql.php");
include_once("../includes/config.php");
$db=new DB();
$db->connectdb(DB_NAME,DB_USER,DB_PASS);

$arr["update"]=$db->update(TB_CATEGORY,array("name"=>"$_POST[name]",

"seq"=>"$_POST[seq]"

),"id='$_GET[id]'");


?>
<a href="Edit.php">Edit Successful</a>
<Meta http-equiv="refresh"content="1;URL=Edit.php">

PHP ลบข้อมูลใน table

<?
include_once("../includes/class_mysql.php");
include_once("../includes/config.php");
$db=new DB();
$db->connectdb(DB_NAME,DB_USER,DB_PASS);
$db->del(TB_CATEGORY,"id=".$_GET["id"]."");/*หรือ ,"mem_id=".$_POST[id]."" ||  "mem_id='$id'"  */

echo "Delete Successful";

?>
<Meta http-equiv="refresh"content="1;URL=Edit.php">

PHP add ข้อมูลลง table

 <?
    include_once("../includes/class_mysql.php");
include_once("../includes/config.php");

$db=new DB();
$db->connectdb(DB_NAME,DB_USER,DB_PASS);

$name=$_POST[name];
$seq=$_POST[seq];


$res = $db->add(TB_CATEGORY,array(

"name"=>"".$name."",
"seq"=>"".$seq.""

));

if($res){
echo "OK"."<br>";
echo "$res"."<br>";
echo "$name"."<br>";

}
$db->closedb();




?>
<a href="Edit.php">View files</a>
<Meta http-equiv="refresh"content="1;URL=Edit.php">

ทำ from add data

<form name="form1" method="post" action="Add.php" enctype="multipart/form-data" onSubmit="return verify()">
    <table width="1500">
    <tr>
    <td width="1500" height="70">
        <div class="col-xs-4">
            <label for="exampleInputEmail1">ชื่อหัวข้อ</label>  
            <input type="text" class="form-control" id="name" name="name" placeholder="ชื่อหัวข้อ">
        </div>
        </td>
      </tr>
      <tr>
      <td height="70">
        <div class="col-xs-4">
            <label for="exampleInputEmail1">ลำดับ</label>  
            <input type="text" name="seq" class="form-control" placeholder="ลำดับ">
          </div>
          </td>
</tr>
      <tr>
      <td>
        <div class="col-xs-4">
        <div class="form-group"></div>
<button type="submit" name="btnSubmit" value="Submit" class="btn btn-default">บันทึก</button>
          </div>
         </td>
         </tr>        
    </table>
</form>

Create new file 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>Untitled Document</title>
</head>

<body>
</body>
</html>

PHP OOP - Code connect and basic function

Flie : config.php

<?php
if (preg_match("/config.php/i",$PHP_SELF)) {
//if(eregi("config.php",$_SERVER['PHP_SELF'])){

header("Location: ../index.php");
}


#--> config connect Mysql

define("DB_HOST","localhost");//define ค่าคงที่
define("DB_USER","root");
define("DB_PASS","1234");
define("DB_NAME","enmholding_db");

#--> Table
define("TB_HOME","tbl_home");
define("TB_PRODUCT","tbl_product");
define("TB_MEM","tbl_member");
define("TB_PROUDCT1","product");
define("TB_CATEGORY","category");
define("TB_BRAND_TH","brand_th");
define("TB_SUB_CATEGORY","sub_category");
?>

File : class_mysql.php

<?php
//if(eregi("class_mysql.php",$_SERVER['PHP_SELF'])){//eregi ไม่ว่าจะเขียนตัวเล็กหรือตัวใหญ่ก้อไม่สามารถเข้ามาได้
if (preg_match("/config.php/i",$PHP_SELF)) {
header("Location: ../index.php");

}

class DB{

var $host = DB_HOST;
var $conndb;
var $db;
var $result;
var $sql;

#--> Connect Database
function connectdb($db_name="database",$user="username",$pass="password"){

$this->username=$user;
$this->password=$pass;
$this->database=$db_name;

$this->conndb=mysql_pconnect($this->host,$this->username,$this->password) or die (mysql_error());
$this->db=mysql_select_db($this->database,$this->conndb)  or die (mysql_error());
mysql_query("SET NAMES UTF8");

//echo "OK"; //ทดสอบการใช้งานว่าใช้ได้หรือไม่

}

#--> Close Database
function closedb(){

mysql_close($this->conndb) or die (mysql_error());
}

#--> Query Database
function querydb($sql="sql"){

$this->sql=$sql;

if($result=mysql_query ($this->sql)){
return $result;
}else{
mysql_error();
return false;
}
}

#-->fetch
function fetch($sql="sql"){

if($result=mysql_fetch_array($sql)){
return $result;
}else{
mysql_error();
return false;
}
}

#-->rows
function row($sql){
return mysql_num_rows($sql);
}

#-->Add
function add($table="table",$data="data"){

$a_key=array_keys($data);
$v_valus=array_values($data);
$sum=count($a_key);

for($i=0; $i<$sum; $i++){

if(empty($add)){
$add="(";
}else{
$add=$add.",";
}
if(empty($val)){
$val="(";
}else{
$val=$val.",";
}

$add=$add.$a_key[$i];
$val=$val."'".$v_valus[$i]."'";
}

$add=$add.")";
$val=$val.")";

$sql=" INSERT INTO ".$table." ".$add." VALUES ".$val;

if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}

}

#--> UPDATE
function update($table,$daat,$where){
/*
$key=array_keys($data);
$valus=array_values($data);
$sum=count($key);
$set="";
*/
$key=array_keys($daat);
$value=array_values($daat);
$sum=count($key);
$set="";

for($i=0; $i<$sum; $i++){
if(!empty($set)){
$set=$set.",";
}
$set=$set.$key[$i]."='".$value[$i]."'";
}

$sql="UPDATE ".$table." SET ".$set." WHERE ".$where;
if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}
}

#--> Del
function del($table,$where){
$sql ="DELETE FROM ".$table." WHERE ".$where;
if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}
}

#--> Num
function num($field,$table,$where){

if($where==""){
$where="";
}else{
$where="WHERE ".$where;
}

$sql="SELECT $field FROM $table $where";

if($result=mysql_query($sql)){
return mysql_num_rows($result);
}else{
mysql_error();
return false;
}
}
}
?>