29 พฤศจิกายน 2553

วิธี Random คำตอบ โดยใ้ช้ array

มีอยู่ 3 วิธี  ดังต่อไปนี้


วิธีที่ 1

srand((float) microtime() * 10000000);

$answers = array();

$answers[1] = "5 ธันวาคม" ;
$answers[2] = "11 ธันวาคม" ;
$answers[3] = "15 ธันวาคม" ;
$answers[4] = "19 ธันวาคม" ;

$random_key = array_rand($answers  , 4);

foreach( $random_key as $key=>$value )
{
    echo '<input name="answer" type="radio" value="' . $value . '" />' . $answers[$value] .' <br/>' ;

}


แนะนำโดย พี่ปลาทู  แห่ง select2web.com


วิธีที่  2


$input = array();
   
    $input[0][1] = "5 ธันวาคม" ;
    $input[1][2] = "11 ธันวาคม" ;
    $input[2][3] = "15 ธันวาคม" ;
    $input[3][4] = "19 ธันวาคม" ;

    shuffle($input);  //คำสั่งนี้ สลับตำแหน่งใน array เหมือน  ipod shuffle สุ่มเพลง
   
    $rand_keys1 = array_slice($input, 0, 4);  // วันลูปตัด array
       
    foreach($rand_keys1 as $rand_keys2){
           
        foreach($rand_keys2 as $key => $value){
           
           echo '<input name="answer" type="radio" value="' . $key . '" />' . $value .' <br/>' ;
       
        }
       
    }

วิธีที่ 3

$answers = array();

$answers[1] = "5 ธันวาคม" ;
$answers[2] = "11 ธันวาคม" ;
$answers[3] = "15 ธันวาคม" ;
$answers[4] = "19 ธันวาคม" ;

$string = str_shuffle('1234');


for($i= 0; $i < count($answers); $i++){
    echo '<input name="answer" type="radio" value="' .$string{$i}. '" />' . $answers[$string{$i}] .'<br/>' ;
}

แนะนำโดย พี่ปลาทู  แห่ง select2web.com

12 พฤศจิกายน 2553

วิธี แปลง พ.ศ. เป็น ค.ศ.

 $text="08/02/2559"; // นี่คือรูปแบบเดิมที่เรามี

 $text=preg_split("/\//",$text, -1, PREG_SPLIT_NO_EMPTY);
 $text=$text[0]."/".$text[1]."/".($text[2]-543);
 echo $text;

 เมื่อทำการแปลง ค.ศ. เราก็จะได้รูปแบบดังนี้ 08/02/2016

1 พฤศจิกายน 2553

PHP Function uniqid();

 uniqid(); 
    เป็นฟังชั่น ที่ใช้ในการสร้าง ID ที่ไม่เหมือนกันในการโหลดแต่ละครั้ง  เพื่อช่วยในการตั้งชื่อไฟล์อัพโหลดของเราไม่ให้มีชื่อซ้ำกัน  หรือใช้เพื่อการอื่นก็ได้

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();
?>