17 กันยายน 2553

SQL เลือก ฟิววันที่ล่าสุด ของ อุปกรณ์แต่ละไอดี ที่ต้องการ

 จากภาพข้างบน หากจะเอาวันที่่ล่าสุดของ product_bind_id ซึ่งมันจะนำวันที่ล่าสุดมา แต่จะไม่เอา status_id ตามวันที่ล่าสุดมา
  แต่่หากจะเอามาต้องจะต้องมี SQL ดังข้างล่างครับ


SELECT tbl_check.check_date, tbl_check.product_bind_id, tbl_check.status_id
FROM tbl_check INNER JOIN tbl_product_bind  ON (tbl_check .product_bind_id = tbl_product_bind.product_bind_id)
WHERE  (tbl_check.product_bind_id, check_date) IN
 ( SELECT product_bind_id, MAX(check_date) FROM tbl_check GROUP BY product_bind_id)

จาก : http://stackoverflow.com/questions/121387/sql-fetch-the-row-which-has-the-max-value-for-a-column

หรืออีกหนึ่งทางเลือกจากพี่ปลาทู


select * FROM tbl_check WHERE tbl_check.check_date IN ( SELECT MAX(tcheck.check_date) FROM tbl_check tcheck GROUP BY tcheck.product_bind_id  );
 
 

4 สิงหาคม 2553

Jquery เพิ่มแถวใหม่

ส่วน โค้ด ของ Jquery
**************************************************************************
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
        var id = document.getElementById("id").value;       
    $('#add-row').click(function(){
       
        var str = "";
        $("#select option:selected").each(function () {
            str = $(this).text();
            //alert(str);
        });
        var tr = $('<tr width="343" id="row'+id+'">#</tr>');
        var td1 = $('<td width="83"><input type="hidden" name="branch_id[]" id="branch_id[]" value="'+$('#select').val() +'"/></td>');
        var td2 = $('<td width="253">'+str+'</td>');
        var td3 = $('<td><a href="#" onClick="removeFormField(\'#row'+id+'\'); return false;">Remove</a></td>');
       
        tr.append(td1);
        tr.append(td2);
        tr.append(td3);

        $('#tbl-product').append(tr);
        id = (id - 1) + 2;
        document.getElementById("id").value = id;

    });
   
    $("#delete-row").click(function(){
        if($("#tbl-product tr").size()>1){
            $("#tbl-product tr:last").remove();
        }else{
            alert("Have Mor 1 !");
        }
    });   
   
})

function removeFormField(id) {
    $(id).remove();
}
   
</script>

**************************************************************************
ส่วนที่ทดลองใช้งาน

<?php if(empty($_POST)){ ?>
<input type="hidden" id="id" value="1">
<form id="form1" name="form1" method="post" action="">
  <table width="348" id="tbl-product">
    <tr id="firstTr">
      <th width="83">ID</th>
      <th width="253">product</th>
      <th width="253"> </th>
    </tr>
  </table>
  <table width="348" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="68"><input type="button" name="add-row" id="add-row" value="ADD" /></td>
      <td width="69"><input type="button" name="delete-row" id="delete-row" value="DELETE" /></td>
      <td width="211">
          <select name="select" id="select">
            <option value="1">test 1</option>
            <option value="2">test 2 </option>
            <option value="3">test 3 </option>
            <option value="4">test 4</option>
            <option value="5">test 5</option>
           
          </select>
      </td>
    </tr>
  </table>
  <input type="submit" name="button" id="button" value="Submit" />
</form>

<?php } else { ?>

    <?php
        print_r($_POST['branch_id']);
    ?>

<?php } ?>

20 กรกฎาคม 2553

ลบ SESSION แบบเกลี้ยง

session_name('newname');
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?> 

10 กรกฎาคม 2553

คำสั่งที่ใช้ในการเปลี่ยนรูปแบบวันที่ ใน SQL

เมื่อเราเก็บวันที่ในรูปแบบ 10-05-2010 หากเราทำการค้นหา โปรแกรมจะมองเห็นแค่ 2 ตัวแรก

ซึ่งเราต้องทำให้มันอยู่ในรูปแบบของฟอร์แมต เครื่อง คือ 2010-05-10 เสียก่อน ถึงจะทำการค้นหาได้ถูกต้อง

คำสั่งข้างล่างจะทำการเปลี่ยนรูปแบบวันที่ก่อนนำไปค้นหา

จาก text_date_field = 10-05-2010

CONCAT(SUBSTRING(`text_date_field` ,7 ,4) ,'-' ,SUBSTRING(`text_date_field` ,4 ,2) ,'-' ,SUBSTRING(`text_date_field` ,1 ,2))

เมื่อแปลงแล้วจะได้ 2010-05-10

25 มิถุนายน 2553

การบันทึกเวลาใน Cakephp

    ในเวลาเราจะบันทึกข้อมูลลงในฐานข้อมูล cakephp จะบันทึกเวลให้อัตโนมัติ ในฟิลที่มีชื่อว่า
created และ มีชนิดของฟิลเป็น datetime 
    ซึ่งในขณะเดียวกัน เวลาเราทำการแก้ไข เรคคอร์ด ก็จะบันทึกเวลาที่เราแก้ไขให้อัตโนมัติเช่นเดียวกัน ในฟิลที่มีชื่อว่า  modified  และมีชนิดของฟิลเป็น datetime

23 มิถุนายน 2553

ฟังชั่นคำนวณวัน PHP

 // ฟังชั่นคำนวณวัน
    function dateDiv($t1,$t2){ // ส่งวันที่ที่ต้องการในรูปแบบ  27-03-2006 21:20:00

        $t1Arr=splitTime($t1);
        $t2Arr=splitTime($t2);
  
        $Time1=mktime($t1Arr["h"], $t1Arr["m"], $t1Arr["s"], $t1Arr["M"], $t1Arr["D"], $t1Arr["Y"]);
        $Time2=mktime($t2Arr["h"], $t2Arr["m"], $t2Arr["s"], $t2Arr["M"], $t2Arr["D"], $t2Arr["Y"]);
        $TimeDiv=abs($Time2-$Time1);
  
        $Time["D"]=intval($TimeDiv/86400); //  จำนวนวัน
        $Time["H"]=intval(($TimeDiv%86400)/3600); // จำนวน ชั่วโมง
        $Time["M"]=intval((($TimeDiv%86400)%3600)/60); // จำนวน นาที
        $Time["S"]=intval(((($TimeDiv%86400)%3600)%60)); // จำนวน วินาที
      
         return $Time;
}



    function splitTime($time){ // เวลาในรูปแบบ 27-04-2006 22:30:50
         $timeArr["Y"]= substr($time,8,2);
         $timeArr["M"]= substr($time,3,2);
         $timeArr["D"]= substr($time,0,2);
         $timeArr["h"]= substr($time,11,2);
         $timeArr["m"]= substr($time,14,2);
         $timeArr["s"]= substr($time,17,2);

         return $timeArr;
    }

//------------------------------  ตัวอย่างการใช้งาน
$t1="27-03-2006 21:20:00";
$t2="27-04-2006 22:30:50";

$time=dateDiv($t1,$t2);
print_r($time);

Javascript ปิดหน้าต่างลูกแล้ว refresh หน้าต่างหลัก

javascript:window.parent.opener.document.location.href='index.php'; window.close();

วิธีการเข้าใช้งาน ก่อนอื่นทำการสร้างหน้า ที่ทำการเปิด หน้า Popup

<!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>Open A New Window</title>
</head>

<body>
<a href="" onclick="window.open('test.php','welcome','width=300,height=200')">Open a new window</a>
</body>
</html>



ไฟล์ test.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>Close Open A New Window</title>
</head>

<body>
<a href="#" onclick="javascript:window.parent.opener.document.location.href='http://www.google.co.th'; window.close();">Close</a>
</body>
</html>


ซึ่งในไฟล์ test.php นี้จะเป็นหน้าสำหรับหน้า Popup และจะมีปุ่ม Close เพื่อปิดหน้า ซึ่งเมื่อเรากดปุ่ม Close หน้าหลักจะเด้งไปที่ www.google.co.th ให้อัตโนมัติ