 |
IF ELSEIF ทำไมเงื่อนไขเข้าแค่เงื่อนไขเดียว รบกวนช่วยดูทีครับ |
|
 |
|
|
 |
 |
|
อันนี้ลอง 3 เงื่อนไขเลย ออกเงื่อนแรกอันเดียว ตัวแปรก็มาครบ งง  
Code (PHP)
<?php
include 'connect.php';
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
if ($year != "")
{
$data = $db->prepare('SELECT YEAR(start)+543 AS dtYear, COUNT((YEAR(start))+543) AS count_year
FROM tbl_logs
WHERE YEAR(start) = :y
GROUP BY dtYear
HAVING count_year
ORDER BY dtYear DESC');
$data->bindParam(':y',$year);
$data->execute();
while ($rows = $data->fetch(PDO::FETCH_ASSOC))
{
echo $rows['dtYear'].'-'.$rows['count_year'];
}
}
else if ($month != "" && $year != "")
{
$data1 = $db->prepare('SELECT YEAR(start)+543 AS dtYear, COUNT((YEAR(start))+543) AS count_year
FROM tbl_logs
WHERE YEAR(start) = :y AND MONTH(start) = :m
GROUP BY dtYear
HAVING count_year
ORDER BY dtYear DESC');
$data1->bindParam(':y',$year);
$data1->bindParam(':m',$month);
$data1->execute();
while ($rows1 = $data1->fetch(PDO::FETCH_ASSOC))
{
echo $rows1['dtYear'].'-'.$rows1['count_year'];
}
}
else if($day !="" && $month !="" && $year != "")
{
$data1 = $db->prepare('SELECT YEAR(start)+543 AS dtYear, COUNT((YEAR(start))+543) AS count_year
FROM tbl_logs
WHERE YEAR(start) = :y AND MONTH(start) = :m
GROUP BY dtYear
HAVING count_year
ORDER BY dtYear DESC');
$data1->bindParam(':y',$year);
$data1->bindParam(':m',$month);
$data1->execute();
while ($rows1 = $data1->fetch(PDO::FETCH_ASSOC))
{
echo $rows1['dtYear'].'-'.$rows1['count_year'];
}
}
?>
|
 |
 |
 |
 |
Date :
2015-07-02 22:46:03 |
By :
littlebeer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
if ($year != "") {
//ทำ เมื่อ year มีค่า year != ""
} else if ($month != "" && $year != "") {
// เอาสีแดงออกไป เพราะไม่มีโอกาสมีค่าแล้ว จากเงื่อนไขแรก
// ก็ตรวจสอบตัวแปรอื่นต่อไป
}
น่าจะเป็น
Code (PHP)
if( $year && $month){
// ทำ ถ้า year มีค่า และ month มีค่า
}else if( $year){
// ทำ ถ้า year มีค่า และ month ไม่มีค่า
}else if( $month){
// ทำ ถ้า year ไม่มีค่า และ month มีค่า
}else{ // ทำ ถ้า year ไม่มีค่า และ month ไม่มีค่า }
ก็ลองเช็ค flow ให้ดีครับว่าต้องการแบบไหน
|
ประวัติการแก้ไข 2015-07-03 02:10:49
 |
 |
 |
 |
Date :
2015-07-03 02:05:55 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|