Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > อยากจะแยกข้อมูลในเรคอร์เป็นรายอาทิตย์ ทำอย่างไงค่ะ



 

อยากจะแยกข้อมูลในเรคอร์เป็นรายอาทิตย์ ทำอย่างไงค่ะ

 



Topic : 099140



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์




สมมุติจากการดึงค่าฐานข้อมูลดังนี้

457453473465345

ตามปฏิทิน
458484984984784

แยกข้อมูลในเรคอร์เป็นรายสัปดาห์(ไม่เอาวันอาทิตย์ เท่านั้น)
51654

แบบนี้พอจะเป็นไปได้ไหมค่ะ

ช่วยแนะนำหน่อยค่ะ จะเพิ่ม จะเสริม จะแต่ง ได้เลยค่ะ



Tag : PHP, MySQL, Ms SQL Server 2008, HTML/CSS, JavaScript, CakePHP









ประวัติการแก้ไข
2013-08-15 15:35:49
2013-08-15 15:37:50
2013-08-16 13:48:10
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-08-15 15:34:36 By : phoenix10 View : 4150 Reply : 34
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

หา Link มาให้ครับ คือจะต้องหา StartDate,EndDate ของ Week ก่อนครับ จากนั้นค่อยนำไป BETWEEN ได้ไม่ยาก

http://stackoverflow.com/questions/923925/get-start-and-end-days-for-a-given-week-in-php







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 09:22:18 By : mr.win
 


 

No. 2

Guest


1

classes\test.php
<?php
namespace classes;

use \PDO as PDO;

class test {
    private $dns;
    private $user;
    private $password;

    public function __construct() {
        $this->dns = 'sqlsrv:Server=localhost\sqlexpress;Database=test;';
        $this->user = '';
        $this->password = '';

        if (!$this->exist())
            $this->create();
    }

    protected function exist() {
        $dbh = new PDO($this->dns, $this->user, $this->password);

        $query = "IF EXISTS (SELECT * FROM sys.tables WHERE name = 'MyTable') SELECT 'true' ELSE SELECT 'false';";
        $stmt = $dbh->prepare($query);
        $stmt->execute();

        return  ($stmt->fetchColumn() === 'true');
    }

    protected function create() {
        if ($this->exist())
            $this->drop();
        
        $dbh = new PDO($this->dns, $this->user, $this->password);

        $query = 'CREATE TABLE MyTable (ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, MyDate SMALLDATETIME, MyValue FLOAT);';
        $stmt = $dbh->prepare($query);
        $stmt->execute();

        $this->fill();
    }

    protected function drop() {
        $dbh = new PDO($this->dns, $this->user, $this->password);

        $query = 'DROP TABLE MyTable;';
        $stmt = $dbh->prepare($query);
        $stmt->execute();
    }

    protected function fill() {
        $dbh = new PDO($this->dns, $this->user, $this->password);

        $data = $this->data();

        for ($i = 0; $i < sizeof($data); $i++) {
            $query = 'INSERT INTO MyTable (MyDate, MyValue) VALUES (:MyDate, :MyValue);';
            $stmt = $dbh->prepare($query);
            $stmt->execute(array(':MyDate' => $data[$i]['Date'], ':MyValue' => $data[$i]['Value']));
        }
    }

    protected function data() {
        $data = array(
            array('Date' => '2013-10-1', 'Value' => 1.52),
            array('Date' => '2013-10-2', 'Value' => 33.33),
            array('Date' => '2013-10-4', 'Value' => 46.97),
            array('Date' => '2013-10-7', 'Value' => 0.66),
            array('Date' => '2013-10-8', 'Value' => 34.21),
            array('Date' => '2013-10-9', 'Value' => 51.97),
            array('Date' => '2013-10-12', 'Value' => 13.16),
            array('Date' => '2013-10-14', 'Value' => 0.01),
            array('Date' => '2013-10-16', 'Value' => 36.06),
            array('Date' => '2013-10-16', 'Value' => 48.44),
            array('Date' => '2013-10-19', 'Value' => 11.98),
            array('Date' => '2013-10-21', 'Value' => 0.05),
            array('Date' => '2013-10-23', 'Value' => 35.00),
            array('Date' => '2013-10-26', 'Value' => 43.00),
            array('Date' => '2013-10-26', 'Value' => 19.00),
            array('Date' => '2013-10-28', 'Value' => 0.91),
            array('Date' => '2013-10-29', 'Value' => 37.27),
            array('Date' => '2013-10-30', 'Value' => 52.27),
            array('Date' => '2013-10-31', 'Value' => 9.55));

        return $data;
    }

    public function get($year, $month) {
        $dbh = new PDO($this->dns, $this->user, $this->password);

        $query = "SELECT MyDate, MyValue FROM MyTable WHERE YEAR(MyDate)=:Year AND MONTH(MyDate)=:Month ORDER BY MyDate ASC;";
        $stmt = $dbh->prepare($query);
        $stmt->execute(array(':Year' => $year, ':Month' => $month));

        return  $stmt;
    }
}
?>


index.php
<?php
    spl_autoload_extensions('.php');
    spl_autoload_register();

    use \DateTime as DateTime;
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test PHP</title>
    </head>
    <body>
        <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<?php
    $year = 2013;
    $month = 10;
    $date = new DateTime("$year-$month-1");

    $firstDayOfMonth = intval($date->format('w')); // วันอะไร
    $daysInMonth = intval($date->format('t')); // เดือนนี้มีกี่วัน
    $daysInMonthPlus = $daysInMonth + $firstDayOfMonth;
    $weeksInMonth = ($daysInMonthPlus % 7 === 0) ? $daysInMonthPlus / 7 : floor($daysInMonthPlus / 7) + 1;

    echo "<tr><th colspan=\"{$weeksInMonth}\" style=\"background-color:LightYellow;\">{$date->format('F')}</th></tr>\n";
    
    echo '<tr>';
    for ($i = 0; $i < $weeksInMonth; $i++) {
        $first = $i * 7 - $firstDayOfMonth + 1;
        $last = $i * 7 + 7 - $firstDayOfMonth;
        $firstDayOfWeek = ($first >= 1) ? (intval((new DateTime("$year-$month-$first"))->format('w')) === 0) ? $first + 1 : $first : 1;
        $lastDayOfWeek = ($last <= $daysInMonth) ? $last : $daysInMonth;

        echo "<th style=\"background-color:LightYellow;\">{$firstDayOfWeek}-{$lastDayOfWeek}</th>";
    }
    echo "</tr>\n";
   
    $test = new classes\test();
    $result = $test->get($year, $month);

    $data = array();
    $level = 0;
    $previous = 0;

    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $myDate = new DateTime($row['MyDate']);
        $day = intval($myDate->format('j'));
        $dayMod = floor(($day + $firstDayOfMonth) / 7);
        $week = ($dayMod === 0) ? $dayMod : $dayMod + 1;

        if (intval($myDate->format('w')) !== 0) {
            $level = ($previous === $week) ? $level + 1 : 1;
            $key = 'W' . $week . 'L' . $level;
            $data[$key] = number_format($row['MyValue'], 2, '.', ',');
        }

        $previous = $week;
    }

    for ($l = 1; $l <= $level; $l++) {
        echo '<tr>';
        for ($w = 1; $w <= $week; $w++) {
            $key = "W" . $w . "L" . $l;
            $value = isset($data[$key]) ? $data[$key] : '&nbsp';
            echo "<td>{$value}</td>";
        }
        echo "</tr>\n";
    }
?>
        </table>
    </body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 10:40:53 By : ห้ามตอบเกินวันละ 2 กระทู้
 

 

No. 3

Guest


เอาใหม่ ตะกี้ไม่ถูก

2

index.php
<?php
    spl_autoload_extensions('.php');
    spl_autoload_register();

    use \DateTime as DateTime;
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test PHP</title>
    </head>
    <body>
        <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<?php
    $year = 2013;
    $month = 10;
    $date = new DateTime("$year-$month-1");

    $firstDayOfMonth = intval($date->format('w')); // วันอะไร
    $daysInMonth = intval($date->format('t')); // เดือนนี้มีกี่วัน
    $daysInMonthPlus = $daysInMonth + $firstDayOfMonth;
    $weeksInMonth = ($daysInMonthPlus % 7 === 0) ? $daysInMonthPlus / 7 : floor($daysInMonthPlus / 7) + 1;

    echo "<tr><th colspan=\"{$weeksInMonth}\" style=\"background-color:LightYellow;\">{$date->format('F')} {$year}</th></tr>\n";
    
    echo '<tr>';
    for ($i = 0; $i < $weeksInMonth; $i++) {
        $first = $i * 7 - $firstDayOfMonth + 1;
        $last = $i * 7 + 7 - $firstDayOfMonth;
        $firstDayOfWeek = ($first >= 1) ? (intval((new DateTime("$year-$month-$first"))->format('w')) === 0) ? $first + 1 : $first : 1;
        $lastDayOfWeek = ($last <= $daysInMonth) ? $last : $daysInMonth;

        echo "<th style=\"background-color:LightYellow;\">{$firstDayOfWeek}-{$lastDayOfWeek}</th>";
    }
    echo "</tr>\n";
   
    $test = new classes\test();
    $result = $test->get($year, $month);

    $data = array();
    $level = 0;
    $previous = 0;

    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $myDate = new DateTime($row['MyDate']);
        $day = intval($myDate->format('j'));
        $dayPlus = $day + $firstDayOfMonth;
        $week = ($dayPlus % 7 === 0) ? floor($dayPlus / 7) : floor($dayPlus / 7) + 1;

        if (intval($myDate->format('w')) !== 0) {
            $level = ($previous === $week) ? $level + 1 : 1;
            $key = 'W' . $week . 'L' . $level;
            $data[$key] = $myDate->format('d/m/Y') . ' - ' . number_format($row['MyValue'], 2, '.', ',');
        }

        $previous = $week;
    }

    for ($l = 1; $l <= $level; $l++) {
        echo '<tr>';
        for ($w = 1; $w <= $week; $w++) {
            $key = "W" . $w . "L" . $l;
            $value = isset($data[$key]) ? $data[$key] : '&nbsp';
            echo "<td>{$value}</td>";
        }
        echo "</tr>\n";
    }
?>
        </table>
    </body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 10:57:37 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 4



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากค่ะ แต่ Error มันต้องมีไฟล์เป็น classes ป่าวค่ะ

namespace classes;
Parse error: syntax error, unexpected T_STRING in D:\AppServ\www\test2.php on line 2


use \DateTime as DateTime;
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in D:\AppServ\www\test3.php on line 6

Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in D:\AppServ_2556\www\test3.php on line 6


ประวัติการแก้ไข
2013-08-16 11:31:22
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 11:31:07 By : phoenix10
 


 

No. 5

Guest


classes เป็นชื่อ folder

ให้สร้าง folder ชื่อ classes แล้วเอา test.php ไปวางไว้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 11:50:19 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 6



โพสกระทู้ ( 1,819 )
บทความ ( 20 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Facebook

โค้ดของคุณห้ามตอบฯ เอาไปใช้กับเครื่องคุณ (และโฮสต์ในไทยส่วนใหญ่) ไม่ได้หรอกครับ เพราะ AppServ รุ่นล่าสุด (และโฮสต์ในไทยส่วนใหญ่) ยังใช้ PHP5.2 ยังไม่สนับสนุน namespace ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 12:04:51 By : cookiephp
 


 

No. 7

Guest


จริงๆ ด้วย แต่โชคดีที่เขียนแบบ oop ไว้

เลยแก้แค่บรรทัดที่ 38-39 กับตรง while loop ของ index.php

ให้เป็นการ query ก็นายเองก็ได้แล้วแหละ

ตัดตรงส่วน autoload class ด้วยก็ได้ เผื่อมันจะ error

ปล. เรามือใหม่มาเริ่ม php ก็ปาไป version 5.4.14 แล้ว ไม่ค่อยมีความรู้เรื่องโค้ดเก่าๆ เท่าไหร่
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 12:16:21 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 8



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


เสียดายเชียว ดูจากที่ คุณ ห้ามตอบเกินวันละ 2 กระทู้ Preview มันใช่ตามที่ต้องการเลย ความหวังมันเมื่อเจือจาง
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 13:43:13 By : phoenix10
 


 

No. 9



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


Quote:
นานๆ กลับมาเยี่ยมบ้านหลังนี้ซักครั้ง ก็เจอโจทย์น่าสนใจเลยครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 13:46:39 By : sakuraei
 


 

No. 10

Guest


มันก็ใช้ได้นะแก้นิดเดียวเอง เอาส่วน query มาใส่หน้า index ก็ได้แล้ว

แก้ตามนี้นะ

index.php
<?php
    //เอาออก
    //spl_autoload_extensions('.php');
    //spl_autoload_register();

    use \DateTime as DateTime;
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test PHP</title>
    </head>
    <body>
        <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<?php
    $year = 2013;
    $month = 10;
    $date = new DateTime("$year-$month-1");

    $firstDayOfMonth = intval($date->format('w')); // วันอะไร
    $daysInMonth = intval($date->format('t')); // เดือนนี้มีกี่วัน
    $daysInMonthPlus = $daysInMonth + $firstDayOfMonth;
    $weeksInMonth = ($daysInMonthPlus % 7 === 0) ? $daysInMonthPlus / 7 : floor($daysInMonthPlus / 7) + 1;

    echo "<tr><th colspan=\"{$weeksInMonth}\" style=\"background-color:LightYellow;\">{$date->format('F')} {$year}</th></tr>\n";
    
    echo '<tr>';
    for ($i = 0; $i < $weeksInMonth; $i++) {
        $first = $i * 7 - $firstDayOfMonth + 1;
        $last = $i * 7 + 7 - $firstDayOfMonth;
        $firstDayOfWeek = ($first >= 1) ? (intval((new DateTime("$year-$month-$first"))->format('w')) === 0) ? $first + 1 : $first : 1;
        $lastDayOfWeek = ($last <= $daysInMonth) ? $last : $daysInMonth;

        echo "<th style=\"background-color:LightYellow;\">{$firstDayOfWeek}-{$lastDayOfWeek}</th>";
    }
    echo "</tr>\n";
   
    //เอาตรงนี้ออก แก้เป็นโค้ด query database อย่าลืม sort ด้วย date นะ
    //$test = new classes\test();
    //$result = $test->get($year, $month);

    $data = array();
    $level = 0;
    $previous = 0;

    while (/* fatch array */) {
        $myDate = new DateTime(/*เอา column date มาใส่ */);
        $day = intval($myDate->format('j'));
        $dayPlus = $day + $firstDayOfMonth;
        $week = ($dayPlus % 7 === 0) ? floor($dayPlus / 7) : floor($dayPlus / 7) + 1;

        if (intval($myDate->format('w')) !== 0) {
            $level = ($previous === $week) ? $level + 1 : 1;
            $key = 'W' . $week . 'L' . $level;
            $data[$key] = $myDate->format('d/m/Y') . ' - ' . number_format(/*เอา column value มาใส่ */, 2, '.', ',');
        }

        $previous = $week;
    }

    for ($l = 1; $l <= $level; $l++) {
        echo '<tr>';
        for ($w = 1; $w <= $week; $w++) {
            $key = "W" . $w . "L" . $l;
            $value = isset($data[$key]) ? $data[$key] : '&nbsp';
            echo "<td>{$value}</td>";
        }
        echo "</tr>\n";
    }
?>
        </table>
    </body>
</html>


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 13:50:17 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 11



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


Quote:
ขออนุมานว่าข้อมูลเป็นดังภาพน่ะครับ ผมใช้วิธีสุ่มตัวเลขขึ้นมา

CREATE TABLE `number` (
`docdate` date default NULL,
`number` decimal(10,2) NOT NULL default '0.00',
UNIQUE KEY `docdate` (`docdate`),
KEY `number` (`number`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `number`
--

INSERT INTO `number` VALUES ('2013-08-01', 1.79);
INSERT INTO `number` VALUES ('2013-08-02', 7.35);
INSERT INTO `number` VALUES ('2013-08-03', 2.91);
INSERT INTO `number` VALUES ('2013-08-04', 8.77);
INSERT INTO `number` VALUES ('2013-08-05', 0.84);
INSERT INTO `number` VALUES ('2013-08-06', 2.63);
INSERT INTO `number` VALUES ('2013-08-07', 7.39);
INSERT INTO `number` VALUES ('2013-08-08', 1.11);
INSERT INTO `number` VALUES ('2013-08-09', 1.85);
INSERT INTO `number` VALUES ('2013-08-10', 5.78);
INSERT INTO `number` VALUES ('2013-08-11', 0.22);
INSERT INTO `number` VALUES ('2013-08-12', 3.56);
INSERT INTO `number` VALUES ('2013-08-13', 2.24);
INSERT INTO `number` VALUES ('2013-08-14', 11.71);
INSERT INTO `number` VALUES ('2013-08-15', 9.88);
INSERT INTO `number` VALUES ('2013-08-16', 2.62);
INSERT INTO `number` VALUES ('2013-08-17', 0.42);
INSERT INTO `number` VALUES ('2013-08-18', 8.04);
INSERT INTO `number` VALUES ('2013-08-19', 1.54);
INSERT INTO `number` VALUES ('2013-08-20', 3.85);
INSERT INTO `number` VALUES ('2013-08-21', 9.16);
INSERT INTO `number` VALUES ('2013-08-22', 13.19);
INSERT INTO `number` VALUES ('2013-08-23', 11.71);
INSERT INTO `number` VALUES ('2013-08-24', 10.39);
INSERT INTO `number` VALUES ('2013-08-25', 9.39);
INSERT INTO `number` VALUES ('2013-08-26', 1.60);
INSERT INTO `number` VALUES ('2013-08-27', 2.36);
INSERT INTO `number` VALUES ('2013-08-28', 4.62);
INSERT INTO `number` VALUES ('2013-08-29', 4.10);
INSERT INTO `number` VALUES ('2013-08-30', 1.90);
INSERT INTO `number` VALUES ('2013-08-31', 5.40);

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 13:52:16 By : sakuraei
 


 

No. 12



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


Code (PHP)
<?php
function isSunday($date) 
{
		return (date('N', strtotime($date)) > 6);
}
function genTable($date)
{
		$lastdayofmonth = date('t',strtotime($date));
		$lastweek = ceil($lastdayofmonth/7);
		$weeks = range(1,$lastweek);		

		$newdate = new Datetime();
		$dateGrouping = array();

		list($Y,$m,$d) = explode("-",$date);
		$newdate->setDate($Y,$m,'01');

		echo "<table id='summary' border='1'><thead><tr><td colspan='$lastweek' style='text-align:center'>",$newdate->format("F Y"),"</td></tr>\n";
		echo "<tr>";
		$n=1;
		for($i = 1; $i <= $lastdayofmonth; ++$i){
			$strdate = $newdate->format("Y-m-d");
			
			if(	isSunday($strdate) && $i > 1){
				$dateGrouping[$n] .= $newdate->format("/m/Y");
				$n++;			
			}elseif($i>1 && strlen($dateGrouping[$n])>0){
				$dateGrouping[$n] .= ",$i";
			}else{
				$dateGrouping[$n] .= "$i";
			}
			$newdate->modify("+1 day");
		}
		$dateGrouping[$n] .= $newdate->format("/m/Y");
		
		foreach($weeks as $numweek){
			$c = explode("," , $dateGrouping[$numweek]);
			$lastc = count($c);
			list($c[$lastc-1],,) = explode("/",$c[$lastc-1]);
			$c = implode(" ",$c);
			$s = explode("," , $dateGrouping[$numweek]);
			$last = count($s);
			echo "<td width='100px' week='$numweek' class='$c'>",$s[0],"-",$s[$last-1],"</td>";
		}
		echo "</tr></thead>\n<tbody></tbody></table>";
		
	}
//////////////////////////////////////////////////////////////
// การเรียกใช้งานฟังก์ชั่นครับ
	$date = "2013-10-10";
	genTable($date);
// เลือกใส่วันที่อะไรก็ได้
/////////////////////////////////////////////////////////////
	$lastdayofmonth = date('t',strtotime($date));
	$lastweek = ceil($lastdayofmonth/7);
?>



ประวัติการแก้ไข
2013-08-16 14:24:36
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:01:43 By : sakuraei
 


 

No. 13



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


ส่วนนี้จะเป็นโค้ด jquery ทำหน้าที่สร้างตารางส่วนคอนเทนต์และนำข้อมูลที่ดึงจากเดต้าเบสมาเก็บไว้ในตัวแปร javascript แล้วจึงนำมาใส่ในตารางโดยดูจากหัวตารางที่ทำคลาสเป็นกลุ่มของวันที่ในแต่ละคอลัมน์
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<script>
	$(document).ready(function(){
		var myNumber = [];
		var lastNumOfWeek = <?php echo $lastweek; ?>;
		<?php
		$link = mysql_connect("localhost","root","password") or die(mysql_error());
		mysql_select_db("test",$link) or die(mysql_error());
		$sql = "select day(docdate) as day,docdate,number from number order by docdate";
		$res = mysql_query($sql) or die(mysql_error());
		while($data = mysql_fetch_assoc($res)){
			echo "myNumber[{$data["day"]}] = '{$data["number"]}';\n";
		}
  		?>

		var classStack,classArray,outer,inner,dayperweek = [1,2,3,4,5,6];
		var allColumn='';
		for(i=1; i <= lastNumOfWeek; ++i)
			allColumn += '<td>&nbsp;</td>';
		
		inner = $("#summary tbody");
		$.each(dayperweek,function(i){			  		
			inner.append("<tr datarow='"+i+"'>"+allColumn+"</tr>");			
		});

		$("table#summary thead tr:nth-child(2) td").each(function(index){
			outer = $(this);
			classStack = outer.attr("class");		
			classArray = classStack.split(" ");		
			$.each(classArray,function(j){			
				$("tr:nth-child("+(j+1)+") td:nth-child("+(index+1)+")",inner).text(myNumber[classArray[j]]);	
			});				
		 });

	});
  </script>



ประวัติการแก้ไข
2013-08-16 14:10:29
2013-08-16 14:12:40
2013-08-16 14:36:20
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:05:18 By : sakuraei
 


 

No. 14



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


นี่เป็นผลลัพธ์จากการรันครับ
thaicreate-09914
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:08:07 By : sakuraei
 


 

No. 15



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


สงสัย บรรทัดที่ 32 ที่มีแทบสีแดง ต้องแก้ไขอะไรป่าวค่ะ

11611s1981
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:12:39 By : phoenix10
 


 

No. 16

Guest


32 ไม่ต้องแก้

39-41 (เขียนโค้ด query ใหม่)

47 (fetch assoc)

48 (เอา date ไปใส่)

56 (เอา value ไปใส่)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:19:35 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 17



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


เราจะลองรันโค๊ดดูนะค่ะ ขอบคุณมากๆค่ะ คุณ ห้ามตอบเกินวันละ 2 กระทู้ และคุณ Unidentifier
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:21:26 By : phoenix10
 


 

No. 18

Guest


คือโค้ดที่ให้ไปใหม่เรายังไม่มีส่วน query น่ะ

เพราะของเก่าเรายัดไว้ใน class แต่พอไม่เรียก class

นายจะต้องเพิ่มส่วนนี้เข้าไปเอง
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:22:08 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 19



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


เอาโครงสร้างตารางข้อมูลมาลงให้ดูหน่อยซิครับ จะได้ปรับแก้ให้ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:31:32 By : sakuraei
 


 

No. 20

Guest


แก้ให้เดียวไฟล์เดียวก็ประมาณนี้แหละ ของเก่าที่เป็น class มันตรวจ table ด้วย

ถ้าไม่มี table มันจะสร้างให้ โค้ดมันเลยยาว

3

index.php
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test PHP</title>
    </head>
    <body>
        <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<?php
    $year = 2013;
    $month = 10;
    $date = new DateTime("$year-$month-1");

    $thaiYear = $year + 543;

    $thaiMonth = array(
        'January' => 'มกราคม',
        'February' => 'กุมภาพันธ์',
        'March' => 'มีนาคม',
        'April' => 'เมษายน',
        'May' => 'พฤษภาคม',
        'June' => 'มิถุนายน',
        'July' => 'กรกฎาคม',
        'August' => 'สิงหาคม',
        'September' => 'กันยายน',
        'October' => 'ตุลาคม',
        'November' => 'พฤศจิกายน',
        'December' => 'ธันวาคม');
    
    $thaiDay = array(
        'Sunday' => 'อาทิตย์',
        'Monday' => 'จันทร์',
        'Tuesday' => 'อังคาร',
        'Wednesday' => 'พุธ',
        'Thursday' => 'พฤหัสบดี',
        'Friday' => 'ศุกร์',
        'Saturday' => 'เสาร์');

    $firstDayOfMonth = intval($date->format('w')); //0=sun, 6=sat
    $daysInMonth = intval($date->format('t'));
    $daysInMonthPlus = $daysInMonth + $firstDayOfMonth;
    $weeksInMonth = ($daysInMonthPlus % 7 === 0) ? floor($daysInMonthPlus / 7) : floor($daysInMonthPlus / 7) + 1;

    echo "<tr><th colspan=\"{$weeksInMonth}\" style=\"background-color:LightYellow;\">{$thaiMonth[$date->format('F')]} {$thaiYear}</th></tr>\n";
    
    echo '<tr>';
    for ($i = 0; $i < $weeksInMonth; $i++) {
        $week = $i + 1;
        $first = $i * 7 - $firstDayOfMonth + 1;
        $last = $i * 7 + 7 - $firstDayOfMonth;
        $firstDayOfWeek = ($first >= 1) ? (intval((new DateTime("$year-$month-$first"))->format('w')) === 0) ? $first + 1 : $first : 1;
        $lastDayOfWeek = ($last <= $daysInMonth) ? $last : $daysInMonth;

        echo "<th style=\"background-color:LightYellow;\">สัปดาห์ที่ {$week} ({$firstDayOfWeek}-{$lastDayOfWeek})</th>";
    }
    echo "</tr>\n";

    $data = array();
    $level = 0;
    $previous = 0;

    $dbh = new PDO('sqlsrv:Server=localhost\sqlexpress;Database=test;', '', '');

    $query = "SELECT MyDate, MyValue FROM MyTable WHERE YEAR(MyDate)=:Year AND MONTH(MyDate)=:Month ORDER BY MyDate ASC;";
    $stmt = $dbh->prepare($query);
    $stmt->execute(array(':Year' => $year, ':Month' => $month));

    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        $myDate = new DateTime($row->MyDate);
        $day = intval($myDate->format('j'));
        $dayPlus = $day + $firstDayOfMonth;
        $week = ($dayPlus % 7 === 0) ? floor($dayPlus / 7) : floor($dayPlus / 7) + 1;

        if (intval($myDate->format('w')) !== 0) {
            $level = ($previous === $week) ? $level + 1 : 1;
            $key = 'W' . $week . 'L' . $level;
            $data[$key] = 'วัน' . $thaiDay[$myDate->format('l')] . 'ที่ ' . $myDate->format('d') . ' - ' . number_format($row->MyValue, 2, '.', ',');
        }

        $previous = $week;
    }

    for ($l = 1; $l <= $level; $l++) {
        echo '<tr>';
        for ($w = 1; $w <= $week; $w++) {
            $key = "W" . $w . "L" . $l;
            $value = isset($data[$key]) ? $data[$key] : '&nbsp';
            echo "<td>{$value}</td>";
        }
        echo "</tr>\n";
    }
?>
        </table>
    </body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:39:21 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 21



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


ตัวอย่างจาก Excel ค่ะ คุณ Unidentifier

เราลองออกแบบตามนี้ดูค่ะ ขอบคุณนะค่ะ ที่สนใจ

518118191849561
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:47:33 By : phoenix10
 


 

No. 22



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


ชุดข้อมูลอยู่ในอะไรครับ mysql หรือ excel ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:49:39 By : sakuraei
 


 

No. 23



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


excel ค่ะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:50:53 By : phoenix10
 


 

No. 24



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


Quote:
เดี๋ยวผมแปะโค้ดแบบชุดเต็มไม่มีตัดต่อไว้ให้ดีกว่าครับ แล้วคุณ Lola ก็ไปแก้ในส่วนที่ติดต่อกับฐานข้อมูล(Excel)
เอาเองแล้วกันน่ะครับ อาจใช้ฟังก์ชั่น PDO แบบที่คุณ " ห้ามตอบเกินวันละ 2 กระทู้" เขียนไว้ข้างต้นน่ะครับ
กระทู้นี้น่าสนใจเพราะโจทย์มันยากพอสมควรครับ เลยมีสมาชิกอยากลับสมองกันหลายท่าน


Code (PHP)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>https://www.thaicreate.com/php/forum/099140.html</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
 </head>

 <body>
 <?php
	function isSunday($date) 
	{
		return (date('N', strtotime($date)) > 6);
	}
	function genTable($date)
	{
		$lastdayofmonth = date('t',strtotime($date));
		$lastweek = ceil($lastdayofmonth/7);
		$weeks = range(1,$lastweek);		

		$newdate = new Datetime();
		$dateGrouping = array();

		list($Y,$m,$d) = explode("-",$date);
		$newdate->setDate($Y,$m,'01');

		echo "<table id='summary' border='1'><thead><tr><td colspan='$lastweek' style='text-align:center'>",$newdate->format("F Y"),"</td></tr>\n";
		echo "<tr>";
		$n=1;
		for($i = 1; $i <= $lastdayofmonth; ++$i){
			$strdate = $newdate->format("Y-m-d");
			
			if(	isSunday($strdate) && $i > 1){
				$dateGrouping[$n] .= $newdate->format("/m/Y");
				$n++;			
			}elseif($i>1 && strlen($dateGrouping[$n])>0){
				$dateGrouping[$n] .= ",$i";
			}else{
				$dateGrouping[$n] .= "$i";
			}
			$newdate->modify("+1 day");
		}
		$dateGrouping[$n] .= $newdate->format("/m/Y");
		
		foreach($weeks as $numweek){
			$c = explode("," , $dateGrouping[$numweek]);
			$lastc = count($c);
			list($c[$lastc-1],,) = explode("/",$c[$lastc-1]);
			$c = implode(" ",$c);
			$s = explode("," , $dateGrouping[$numweek]);
			$last = count($s);
			echo "<td width='100px' week='$numweek' class='$c'>",$s[0],"-",$s[$last-1],"</td>";
		}
		echo "</tr></thead>\n<tbody></tbody></table>";
		
	}
	$date = "2013-10-10";
	genTable($date);
	$lastdayofmonth = date('t',strtotime($date));
	$lastweek = ceil($lastdayofmonth/7);
	?>
<script>
	$(document).ready(function(){
		var myNumber = [];
		var lastNumOfWeek = <?php echo $lastweek; ?>;
		<?php
		// เชื่อมต่อฐานข้อมูล และดึงข้อมูลมาเก็บไว้ในตัวแปร
		$link = mysql_connect("localhost","root","password") or die(mysql_error());
		mysql_select_db("test",$link) or die(mysql_error());
		$sql = "select day(docdate) as day,docdate,number from number order by docdate";
		$res = mysql_query($sql) or die(mysql_error());
		while($data = mysql_fetch_assoc($res)){
			echo "myNumber[{$data["day"]}] = '{$data["number"]}';\n";
		}
  		?>

		var classStack,classArray,outer,inner,dayperweek = [1,2,3,4,5,6];
		var allColumn='';
		for(i=1; i <= lastNumOfWeek; ++i)
			allColumn += '<td>&nbsp;</td>';

		inner = $("#summary tbody");
		$.each(dayperweek,function(i){			  		
			inner.append("<tr datarow='"+i+"'>"+allColumn+"</tr>");			
		});

		$("table#summary thead tr:nth-child(2) td").each(function(index){
			outer = $(this);
			classStack = outer.attr("class");		
			classArray = classStack.split(" ");		
			$.each(classArray,function(j){			
				$("tr:nth-child("+(j+1)+") td:nth-child("+(index+1)+")",inner).text(myNumber[classArray[j]]);	
			});				
		 });
	});
  </script>
 </body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 14:59:45 By : sakuraei
 


 

No. 25



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


ขอถาม คุณ Unidentifier อีกซักคำถามนะค่ะ

จากเดิม สัปดาห์ที่ 1 ,2 ,3 ,4 ,5 จะเป็น วันจันทร์ - เสาร์

แต่เงื่อนไขนี้ อยากให้ สัปดาห์ที่ 1 ,2 ให้เป็น วันจันทร์ - เสาร์ และ 3 ,4 ,5 เป็น วันจันทร์ - อาทิตย์

พอจะแก้ไขตรงจุดไหนได้บ้างค่ะ


ประวัติการแก้ไข
2013-08-16 17:12:05
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 17:10:46 By : phoenix10
 


 

No. 26



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


แบบนี้เหรอครับ
thaicreate-099140-2
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 17:21:09 By : sakuraei
 


 

No. 27



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


แก้ไขโค้ด 2 ที่ครับ
1.)เพิ่ม 7 ที่ตัวแปร dayperweek ครับ
var classStack,classArray,outer,inner,stackDays = [],dayperweek = [1,2,3,4,5,6,7];



เพิ่มเงื่อนไขตรวจสอบสัปดาห์ครับ
	$markSunday=0;
	for($i = 1; $i <= $lastdayofmonth; ++$i){
			$strdate = $newdate->format("Y-m-d");
			
			if(	isSunday($strdate) && $i > 1){
				$markSunday++;
				if($markSunday <= 2)
					$dateGrouping[$n] .= $newdate->format("/m/Y");
				else
					$dateGrouping[$n] .= ",$i".$newdate->format("/m/Y");
				$n++;			
			}elseif($i>1 && strlen($dateGrouping[$n])>0){
				$dateGrouping[$n] .= ",$i";
			}else{
				$dateGrouping[$n] .= "$i";
			}
			$newdate->modify("+1 day");
	}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 17:26:00 By : sakuraei
 


 

No. 28



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


ฉบับเต็มครับ
<?php
function genTable($date)
	{
		$lastdayofmonth = date('t',strtotime($date));
		$lastweek = ceil($lastdayofmonth/7);
		$weeks = range(1,$lastweek);		

		$newdate = new Datetime();
		$dateGrouping = array();

		list($Y,$m,$d) = explode("-",$date);
		$newdate->setDate($Y,$m,'01');

		echo "<table id='summary' border='1'><thead><tr><td colspan='$lastweek' style='text-align:center'>",$newdate->format("F Y"),"</td></tr>\n";
		echo "<tr>";
		$n=1;
		$markSunday=0;
		for($i = 1; $i <= $lastdayofmonth; ++$i){
			$strdate = $newdate->format("Y-m-d");
			
			if(	isSunday($strdate) && $i > 1){
				$markSunday++;
				if($markSunday <= 2)
					$dateGrouping[$n] .= $newdate->format("/m/Y");
				else
					$dateGrouping[$n] .= ",$i".$newdate->format("/m/Y");
				$n++;			
			}elseif($i>1 && strlen($dateGrouping[$n])>0){
				$dateGrouping[$n] .= ",$i";
			}else{
				$dateGrouping[$n] .= "$i";
			}
			$newdate->modify("+1 day");
		}
		$dateGrouping[$n] .= $newdate->format("/m/Y");
		
		foreach($weeks as $numweek){
			$c = explode("," , $dateGrouping[$numweek]);
			$lastc = count($c);
			list($c[$lastc-1],,) = explode("/",$c[$lastc-1]);
			$c = implode(" ",$c);
			$s = explode("," , $dateGrouping[$numweek]);
			$last = count($s);
			echo "<td width='100px' week='$numweek' class='$c'>",$s[0],"-",$s[$last-1],"</td>";
		}
		echo "</tr></thead>\n<tbody></tbody></table>";
		
	}
	$date = "2013-10-10";
	genTable($date);
	$lastdayofmonth = date('t',strtotime($date));
	$lastweek = ceil($lastdayofmonth/7);
	?>
<script>
	$(document).ready(function(){
		var myNumber = [];
		var lastNumOfWeek = <?php echo $lastweek; ?>;
		<?php
		$link = mysql_connect("localhost","root","@poh5992") or die(mysql_error());
		mysql_select_db("test",$link) or die(mysql_error());
		$sql = "select day(docdate) as day,docdate,number from number order by docdate";
		$res = mysql_query($sql) or die(mysql_error());
		while($data = mysql_fetch_assoc($res)){
			echo "myNumber[{$data["day"]}] = '{$data["number"]}';\n";
		}
  		?>

		var classStack,classArray,outer,inner,stackDays = [],dayperweek = [1,2,3,4,5,6,7];
		var allColumn='';
		for(i=1; i <= lastNumOfWeek; ++i)
			allColumn += '<td>&nbsp;</td>';
		
		inner = $("#summary tbody");
		$.each(dayperweek,function(i){			  		
			inner.append("<tr datarow='"+i+"'>"+allColumn+"</tr>");			
		});

		$("table#summary thead tr:nth-child(2) td").each(function(index){
			outer = $(this);
			classStack = outer.attr("class");		
			classArray = classStack.split(" ");		
			$.each(classArray,function(j){			
				$("tr:nth-child("+(j+1)+") td:nth-child("+(index+1)+")",inner).text(myNumber[classArray[j]]);	
			});				
		 });

	});
  </script>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-16 17:37:35 By : sakuraei
 


 

No. 29



โพสกระทู้ ( 41 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 25 เขียนโดย : phoenix10 เมื่อวันที่ 2013-08-16 17:10:46
รายละเอียดของการตอบ ::
ขอบคุณมากค่ะ คุณ Unidentifier

จากที่ลอง เงื่อนไขนี้ อยากให้ สัปดาห์ที่ 1 ,2 ให้เป็น วันจันทร์ - เสาร์ และ 3 ,4 ,5 เป็น วันจันทร์ - อาทิตย์

เราลองนำมาแก้ไข ตามเงื่อนไขใหม่ดู ที่ตรงจุดนี้ดู

***ตัวอย่างคือ เดือน ตุลาคม***

สัปดาห์ที่ 1 ,2
วันจันทร์ - เสาร์
วันที่ 1-5 , 7-12

สัปดาห์ที่ 3 ,4 ,5
วันอาทิตย์ - เสาร์
วันที่ 13-19 , 20-26 , 27-31

แต่ลองแก้ไขตามเงื่อนไขใหม่ของ ***ตัวอย่างคือ เดือน ตุลาคม*** ไม่ได้เลย

Code (PHP)
$markSunday=0;
for($i = 1; $i <= $lastdayofmonth; ++$i){
		$strdate = $newdate->format("Y-m-d");
		
		if(	isSunday($strdate) && $i > 1){
			$markSunday++;
			if($markSunday <= 2)
				$dateGrouping[$n] .= $newdate->format("/m/Y");
			else
				$dateGrouping[$n] .= ",$i".$newdate->format("/m/Y");
			$n++;			
		}elseif($i>1 && strlen($dateGrouping[$n])>0){
			$dateGrouping[$n] .= ",$i";
		}else{
			$dateGrouping[$n] .= "$i";
		}
		$newdate->modify("+1 day");
}



และก็สงสัยในการเรียงลำดับค่ะ เราลองเปลี่ยนมาเป็น number (ASC,DESC) ก็ไม่เรียงให้ เลยสงสัยช่วยอธิบายหน่อยนะค่ะ
โค๊ดที่คุณ Unidentifier ทำให้มันสุดยอดเลยค่ะ ขอบคุณจริงๆที่ช่วยเหลือกัน

Code (PHP)
$sql = "select day(docdate) as day,docdate,number from number order by docdate";



พอจะแก้ไขตรงจุดไหนได้บ้างค่ะ



ประวัติการแก้ไข
2013-08-20 10:48:47
2013-08-20 10:50:33
2013-08-20 10:51:41
2013-08-20 10:52:30
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 10:47:52 By : phoenix10
 


 

No. 30



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


Quote:
สัปดาห์ที่ 3 ,4 ,5
วันอาทิตย์ - เสาร์
วันที่ 13-19 , 20-26 , 27-31
วันอาทิตย์ - เสาร์ เนี่ย 7 วันเลยนี่ครับ งั้นก็ต้องเปลี่ยนเงื่อนไขครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 12:05:27 By : sakuraei
 


 

No. 31



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

ไม่ยากคับ ดึงข้อมูลเรียงตามวันที่ มาลูป แยกชุดเอา
function date() มันสามารถดูข้อมูลได้หลายอย่าง

บางทีอ่านโค้ดคนอื่นยิ่งงง ถ้าเขาอุตส่าเขียนมาให้แล้ว ถ้าอ่านไม่เข้าใจ ก็ลองทำเองจะเก่งขึ้นด้วย


ประวัติการแก้ไข
2013-08-20 12:38:22
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 12:29:53 By : pjgunner.com
 


 

No. 32



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


โค้ดที่เขียนให้จะรันเป็นสัปดาห์ คือ จันทร์ ถึง อาทิตย์
แต่ที่ต้องการจะเป็นการคาบเกี่ยวระหว่างสัปดาห์ คือ อาทิตย์ ถึง เสาร์ แบบนี้คงยากหน่อยล่ะครับ
ถ้ามีเวลาจะกลับมาแก้ไขให้ครับ หรือรอคำแนะนำจากสมาชิกท่านอื่น
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 12:40:26 By : sakuraei
 


 

No. 33



โพสกระทู้ ( 3,155 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


0000009
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 13:25:44 By : sakuraei
 


 

No. 34



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

ผมคิดได้สองวิธีตอนนี้
1. ดึงข้อมูลแบบเรียง แล้วลูป ตรวจสอบด้วย date() etc แล้วจัดเข้ากลุ่มอาเรย์
2. (วิธีลัด) สังเกตุได้จากคุณแสดงข้อมูลเป็นรายเดือน ให้สร้าง ชุดขึ้นมาก่อนโดยลูป 1-31 ของเดือนนั้นๆ ตรวจสอบด้วยว่าเดือนนั้นมีกี่วันเช่น date('t', strtotime('2013-10-1')) คราวนี้ เราจะรู้ว่าวันไหนอยู่ชุดไหนแล้ว คิดว่าอาจงง เดี๋ยวเขียนตัวอย่างให้

Example
<?php
$sql = "select * from number where month(docdate)=8 order by docdate";
$query = mysql_query($sql) or die($sql);

// สร้าง รายการวันที่
$month_days = (int)date('t', strtotime('2013-08-01')); // จำนวนวันในเดือนนี้
$group_of_days = array();
$h = 1; // หมายเลขกลุ่ม

for($i=1; $i <= $month_days; $i++){
	$date_str = '2013-08-'.substr('0'.$i, -2);
	$group_of_days[$date_str] = $h;
	
	// ตรวจสอบว่าเป็นวันสุดท้ายของสัปดาห์ ให้เลื่อนกลุ่ม w มีค่า 0(อาทิตย์)-6(เสาร์) ถ้าจะเลื่อนวัน ให้ปรับตัวนี้ได้
	if (date('w', strtotime($date_str)) == 6)
		++$h;
}
// เราได้หมายเลขกลุ่มของแต่ละวันแล้วคือ $group_of_days

// ถึงเวลาแยกกลุ่มจากข้อมูล
$week1 = $week2 = $week3 = $week4 = $week5 = array(); // คงไม่มีกลุ่ม 6 หละมั้ง 555
while($row = mysql_fetch_array($query))
	${'week'.$group_of_days[$row['docdate']]}[] = $row['number'];
?>
<h1>week1</h1><?php print_r($week1);?>
<h1>week2</h1><?php print_r($week2);?>
<h1>week3</h1><?php print_r($week3);?>
<h1>week4</h1><?php print_r($week4);?>
<h1>week5</h1><?php print_r($week5);?>
ปล.เลี้ยงไอติมผมด้วยนะ :p

ผลลัพท์
week1
Array ( [0] => 1.79 [1] => 7.35 [2] => 2.91 )
week2
Array ( [0] => 8.77 [1] => 0.84 [2] => 2.63 [3] => 7.39 [4] => 1.11 [5] => 1.85 [6] => 5.78 )
week3
Array ( [0] => 0.22 [1] => 3.56 [2] => 2.24 [3] => 11.71 [4] => 9.88 [5] => 2.62 [6] => 0.42 )
week4
Array ( [0] => 8.04 [1] => 1.54 [2] => 3.85 [3] => 9.16 [4] => 13.19 [5] => 11.71 [6] => 10.39 )
week5
Array ( [0] => 9.39 [1] => 1.60 [2] => 2.36 [3] => 4.62 [4] => 4.10 [5] => 1.90 [6] => 5.40 ) 


http://www.pjgunner.com


ประวัติการแก้ไข
2013-08-20 13:37:08
2013-08-20 13:49:46
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-20 13:35:52 By : pjgunner.com
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : อยากจะแยกข้อมูลในเรคอร์เป็นรายอาทิตย์ ทำอย่างไงค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่