ช่วยดูการคิวรี่ข้อมูลใส่ปฏิทินด้วยครับ ท่านเทพทั้งหลาย

ช่วยดูการคิวรี่ข้อมูลใส่ปฏิทินด้วยครับ ท่านเทพทั้งหลาย

ผมพยายามตัด  code ทีุ่ไม่จำเป็นออกแล้วน่ะคร้าบ 


 


 


[head]Code (PHP)[/head]


[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>ปฏิทินกิจกรรม</title>


</head>


<style type="text/css">


.butmonth{


<strong></strong>


width:150px;


height:25px;


text-align:center;


font-weight:700;


font-size:16px;


vertical-align:top;


}


.day_block{


height:30px;


width:14%;


font-family:Tahoma, Geneva, sans-serif;


font-size:16px;


}


.day_block .but{


width:60px;


font-size:14px;


font-weight:700;


margin:auto;


display:block;


}


.btd{


height:20px;


width:20px;


font-family:Tahoma, Geneva, sans-serif;


font-size:14px;


margin-top:5px;


margin-right:5px;


display:block;


font-weight:700;


border:none;


background:#FFF;


}


</style>


<body>


 


 <?PHP


 


class Calendar


{


     function getDayNames()


    {


        return $this->dayNames;


    }


    function setDayNames($names)


    {


        $this->dayNames = $names;


    }


    function getMonthNames()


    {


        return $this->monthNames;


    }


    function setMonthNames($names)


    {


        $this->monthNames = $names;


    }


    function getStartDay()


    {


        return $this->startDay;


    }


    function setStartDay($day)


    {


        $this->startDay = $day;


    }


    function getStartMonth()


    {


        return $this->startMonth;


    }


    function setStartMonth($month)


    {


        $this->startMonth = $month;


    }


     function getCalendarLink($month, $year)


    {


        return "";


    }


     function getCurrentMonthView()


    {


        $d = getdate(time());


        return $this->getMonthView($d["mon"], $d["year"]);


    }


     function getCurrentYearView()


    {


        $d = getdate(time());


        return $this->getYearView($d["year"]);


    }


     function getMonthView($month, $year)


    {


        return $this->getMonthHTML($month, $year);


    }


     function getYearView($year)


    {


        return $this->getYearHTML($year);


    }


    


        //เกี่ยวกับวันที่


     function getDaysInMonth($month, $year)


    {


        if ($month < 1 || $month > 12)


        {


            return 0;


        }


   


        $d = $this->daysInMonth[$month - 1];


   


        if ($month == 2)


        {


            


            if ($year%4 == 0)


            {


                if ($year%100 == 0)


                {


                    if ($year%400 == 0)


                    {


                        $d = 29;


                    }


                }


                else


                {


                    $d = 29;


                }


            }


        }


    


        return $d;


    }


 


 


    function getMonthHTML($m, $y, $showYear = 1)


    {


        $s = "";


        $a = $this->adjustDate($m, $y);


        $month = $a[0];


        $year = $a[1];        


        $daysInMonth = $this->getDaysInMonth($month, $year);


    $date = getdate(mktime(12, 0, 0, $month, 1, $year));


    $first = $date["wday"];


    $monthName = $this->monthNames[$month - 1];


    $prev = $this->adjustDate($month - 1, $year);


    $next = $this->adjustDate($month + 1, $year);


       if ($showYear == 1)


    {


       $prevMonth = $this->getCalendarLink($prev[0], $prev[1]);


       $nextMonth = $this->getCalendarLink($next[0], $next[1]);


    }


    else


    {


       $prevMonth = "";


       $nextMonth = "";


    }


   


    $header = $cs=$monthName . $c=(($showYear > 0) ? " " . ($year+543) : "");


$s .= "<div style=\"border:solid 2px #B0E2FF;width:490px;float:left;margin-left:10px;\">";


$s .= "<table width=\"490\" border=\"0\"cellpadding=\"0\" cellspacing=\"0\">";


$s .= "  <tr>";


    $s .= "    <td>";


$s .= "<table class=\"calendar\" border=\"1\" bgcolor=\"#FFFFFF\" bordercolor=\"white\"         width=\"100%\"  cellpadding=\"0\" cellspacing=\"0\">\n";


    $s .= "<tr >\n";


    $s .= "<td  height=\35\" colspan=\"7\" align=\"center\"  bgcolor=\"#B2DFEE\">


 


 


". (($prevMonth == "") ? "&nbsp;" : "


<a href=\"$prevMonth\" title=\"เดือนก่อนนี้\"><img src=\"piccal/prev.png\"></a>")  .        "


   <button class=butmonth >$header</button>


" . (($nextMonth == "") ? "&nbsp;" : "


<a href=\"$nextMonth\" title=\"เดือนถัดไป\"><img src=\"piccal/next.png\"></a>")  .        "


</td>\n"; 


 


    $s .= "</tr>\n";


    $s .= "<tr hieght=40 bgcolor=\"#FFFFFF\">\n";


    $s .= "<td valign=\"middle\" class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay)%7]."</button>


</td>\n";


$s .= "<td valign=\"middle\" class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay+1)%7]."</button>


</td>\n";


    $s .= "<td valign=\"middle\" class=\"day_block\">


<button class=\"but\">" .$this->dayNames[($this->startDay+2)%7]."</button>


</td>\n";


$s .= "<td valign=\"middle\"class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay+3)%7]."</button>


</td>\n";


$s .= "<td valign=\"middle\"class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay+4)%7] ."</button>


</td>\n";


$s .= "<td valign=\"middle\"class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay+5)%7] ."</button>


</td>\n";


$s .= "<td valign=\"middle\"class=\"day_block\">


<button class=\"but\">".$this->dayNames[($this->startDay+6)%7] ."</button>


</td>\n";


//########################################################################


//                          จบการกำหนดช่องวันที่่ของแต่ละเดือน                  


    $s .= "</tr>\n";


    $d = $this->startDay + 1 - $first;


    while ($d > 1)


    {


       $d -= 7;


    }


        $today = getdate(time());


    while ($d <= $daysInMonth)


    {


       $s .= "<tr>\n";  


 


for ($i = 0; $i < 7; $i++)


       {


        $class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ?         "calendarToday" : "calendar";


 


  $my_date=(date('d')+$daysInMonth)-$daysInMonth;


      $my_month=(date('m')+$month)-$month;


  $my_year=(date('Y')+$year)-$year;


        $my_st_date=$my_year."".$my_month."".$my_date; // สร้างวันที่ปัจจุบัน yyyymmdd


      $make_st_date=$year."".$month."".$d; // สร้างวันที่จากสคริปต์ yyyymmdd


    if($d >=1 and $d <=31 )


    


{   


$event_array=array();


$i_event=0;   


#################


include("connect.php");


$sql="select * from activities where ACT_START ='$year-$month-$d'";


            $query=mysql_query($sql);


$count=mysql_num_rows($query);


            $result=mysql_fetch_array($query);


 


/*


   $sql1="select * from add_one where 


year ='$year-$month-$d'  ";


            $query1=mysql_query($sql1);


$count1=mysql_num_rows($query1);


            $result1=mysql_fetch_array($query1);  


*/


if($result)


{


$bgcolor="piccal/holiday.png";


$show="<center> $count <br> $result[ACT_NAME] </center>";


$link="index.php?file=sh_act&day=$year-$month-$d";


 


}


else if($result1){


 


$link="index.php?file=sh_act_one&day=$year-$month-$d";


$bgcolor="piccal/DAY.png";


 


if($count1>1){


$show="<center>เต็ม</center>";


}


else{


$show="<center>$count1</center>";


}


}


                   


 


else


{


$link="";


 


if($d > 0 && $d <= $daysInMonth){


if($my_st_date == $make_st_date){


$bgcolor="piccal/today.png";


}


else if(($i ==1)){


{


$bgcolor="piccal/DAY1.png ";


}


}


else{


 


$bgcolor="piccal/DAY1.png";


}


}


 


else


{


$bgcolor="";


}


}


}


 


//#########################################


 


$s .= "<td class=\"$class\" align=\"right\" valign=\"top\" height=\"70\" background=\"$bgcolor\">";


           if ($d > 0 && $d <= $daysInMonth)


           {  


$s .= "<div class=\"btd\">".(($link == "") ? $d : "$d</div><fontcolor=\"blue\"><div style=\"margin-top:10px;margin-left:5px;color:#000;font-weight:700; font-size:18px;\">$show </div>"); 


           


}///


 


           else


           {


               $s .= "";


           }


             $s .= "<center></font></td>\n";     


           $d++;


       }  


       $s .= "</tr>\n";    


    }


    $s .= "</table>\n";


    $s .= "</td>";


    $s .= "</tr>";


    $s .= "</table>";


$s .= "</div>";


$s .="<div 


style=


\"float:right;width:180px;height:200px;margin-right:0px;\">";


 


return $s;  


    }


    function adjustDate($month, $year)


    {


        $a = array();  


        $a[0] = $month;


        $a[1] = $year;


         while ($a[0] > 12)


        {


            $a[0] -= 12;


            $a[1]++;


        }


         while ($a[0] <= 0)


        {


            $a[0] += 12;


            $a[1]--;


        }


        return $a;


    }


 


var $startDay = 0;


    var $startMonth = 1;


    var $dayNames = array("อาทิตย์", "จันทร์", "อังคาร", "พุธ", "พฤหัส", "ศุกร์", "เสาร์");


    var $monthNames = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน",


                            "กรกฏาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");


    var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);


    }


class MyCalendar extends Calendar


{ function getCalendarLink($month, $year)


    {


        $s = getenv('SCRIPT_NAME');


        return "?form=calendartkpark&month=$month&year=$year";


    }


}


 


 $d = getdate(time());


 if ($month == "")


{


    $month = $d["mon"];


}


if ($year == "")


{


    $year = $d["year"];


}


$cal = new MyCalendar;


echo $cal->getMonthView($month, $year);


 


?>


 


<br />


</body>


</html>


 


[/php]


 


ช่วยดูหน่อยน่ะครับว่าทำไมมันถึงแสดงข้อมูลได้เพียงแค่บรรทัดเดียว

30 ก.ย. 2554 1 2,246

55+++ ดุไม่รู้เริ่อง

จริงๆแล้วผมเคยอธิบายเกี่ยวกับปฏิทินเหตุการณ์ไว้หลายครั้งมากครับ ลองหาอ่านบนเว็บดู



จากคำตอบก่อนหน้า ผมแนะนำให้ศึกษาจาก GCMS ครับ คือ Widget Calendar ครับ



ในกรณีที่ไม่รู้อะไรเลย วิธีที่ง่ายที่สุดคือการ query ตามวันครับ เช่น ตอนวนลูปสร้างวัน ก้ไป query เอาข้อมูลของวันนั้นออกมา เช่น

<?php

for($i = 1 ; $i <32 ; i++){

    $sql = "SELECT ...... FROM event WHERE DAY(date)=$i"; // query ข้อมูลของวันที่กำลังแสดง

    ---------

    if($result) {

        // มีกิจกรรม

        echo "<b>$i</b>";

    } else {

        // ไม่มีกิจกรรม แสดงวันที่ตามปกติ

        echo $i;

    }

}

?>




ตัวอย่างด้านบน เป็นตัวอย่างในแนวคิดพื้นฐาน ซึ่งถ้าเข้าใจหลักการก็จะสามารถทำได้ง่ายๆ แต่วิธีด้านบนมีข้อเสียคือมันอาจจะช้า เพราะ ต้องมีการ query ถีง 31 ครั้ง วิธีที่ดีกว่า คือการ query ข้อมูลออกมาก่อน โดยการตวจสอบทั้งเดือน แล้วใส่ array ไว้ จากนั้นค่อยนำมาใช้เปรียบเทียบตอนแสดงผลปฏิทินครับ



#1
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^