 |
(Datepicker) คำถามจะลดค่า Previousyear ของ $DateStart AND $DateEnd ลงได้อย่างไร โดยส่งมาจากฟอร์มเดียวกัน |
|
 |
|
|
 |
 |
|
1.(Datepicker) คำถามจะลดค่า Previousyear ของ '$DateStart' AND '$DateEnd' ลงได้อย่างไร โดยส่งมาจากฟอร์มเดียวกัน
สมมุติเลือกข้อมูลปี BETWEEN 2018-01-01 AND 2018-01-31 ข้อมูลของ Previousyear ต้องลดลง 1 ปี เป็น BETWEEN 2017-01-01 AND 2017-01-31
ส่วน Currentyear ก็รับค่ามาแสดง list รายการตามปกติ
ค่าปีที่ลดจะเอามาแสดงส่วนที่เป็น กราฟ เพราะต้องการเปรียบเทียบ กราฟคนละปี เดือนเดียวกัน
Code (PHP)
public function show(Request $request) {
$DateStart = $request->DateStart;
$DateEnd = $request->DateEnd;
$nowyear = date('Y');
$startyear = 2017;
$bargraph = DB::select("
SELECT * FROM
(SELECT count(tbmaster.id) as Previousyear
FROM TB_INFO tbinfo
LEFT JOIN TB_MASTER tbmaster
ON tbinfo.id = tbmaster.id
LEFT JOIN TB_NAME tbname
ON tbinfo.id = tbname.id
WHERE tbinfo.datetime is not null
AND tbinfo.datetime BETWEEN '$DateStart' AND '$DateEnd' // ลดค่าลง 1 ปี BETWEEN 2017-01-01 AND 2017-01-31
AND tbmaster.CancleCode is not null
AND tbmaster.CancleCode in ('10','20')
AND tbname.suff ='1'
) as Y1,
(SELECT Count(tbmaster.id) as Currentyear
FROM TB_INFO tbinfo
LEFT JOIN TB_MASTER tbmaster
ON tbinfo.id = tbmaster.id
LEFT JOIN TB_NAME tbname
ON tbinfo.id = tbname.id
WHERE tbinfo.datetime is not null
AND tbinfo.datetime BETWEEN '$DateStart' AND '$DateEnd' //รับค่าจากฟอร์มปีปัจจุบัน BETWEEN 2018-01-01 AND 2018-01-31
AND tbmaster.CancleCode is not null
AND tbmaster.CancleCode in ('10','20')
AND tbname.suff ='1'
) as Y2 ");
//dd($bargraph);
return view('myview')->with(['bargraph' => $bargraph, 'nowyear' => $nowyear, 'startyear' => $startyear]);
}
เอามาแสดงผลที่ View ส่วนที่จะทำเป็นกราฟแท่ง
Code (PHP)
series: [
@if (isset($bargraph))
@for ($year = $nowyear; $year >= $startyear; $year--)
{ name: {{ $year }},
data: [
@for ($month = 1; $month <= 12; $month++)
@foreach ($bargraph as $graph)
@if ($year == $nowyear)
{{ $graph->Currentyear }}
@elseif($year >= $startyear)
{{ $graph->Previousyear }},
@endif
@endforeach
@endfor
]
},
@endfor
@endif
]
view source code มันดึงข้อมูลของปี 2018 มาแสดง ทำให้กราฟแท่งออกมาเท่ากัน
ข้อมูลมันออกมาเท่ากันทั้ง 2 ปี
Code (PHP)
series: [
{
name: 2018,
data: [
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
]
},
{
name: 2017,
data: [
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
24,
]
},
]
Tag : PHP, Ms SQL Server 2016, Laravel Framework
|
|
 |
 |
 |
 |
Date :
2018-06-15 11:15:52 |
By :
nottpoo |
View :
799 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
http://carbon.nesbot.com/
ผมเคยแปะให้แล้วนะครับ บอกแล้วชีวิตจะดีขึ้น อ่านเถอะ เขียนบรรทัดเดียวจบ laravel มีให้แล้ว
|
 |
 |
 |
 |
Date :
2018-06-15 14:33:39 |
By :
DK |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
dd(Carbon:: createFromFormat("Y-m-d", "2018-01-01")->subYears(1));
|
 |
 |
 |
 |
Date :
2018-06-15 14:36:36 |
By :
DK |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามต่ออีกนิดนึงสิ
ตอนนี้ข้อมูลกราฟมันออกมาข้อมูลได้ตามที่ต้องการแล้ว แต่มีเงื่อนไขเพิ่มว่า ให้แสดงเฉพาะ วัน-เดือน-ปี ที่เลือก วัน-เดือน-ปี ที่เลือกไม่ต้องแสดงออกมา
เราเขียนไว้แบบนี้
Code (PHP)
$DateStart = $request->DateStart;
$DateEnd = $request->DateEnd;
$nowyear = date('Y', strtotime("$DateStart"));
$startyear = date('Y', strtotime("$DateStart- 1 year"));
$months = date('m', strtotime("$DateStart"));
return view('myview')->with(['bargraph' => $bargraph, 'nowyear' => $nowyear, 'startyear' => $startyear, 'months' => $months]);
Code (PHP)
series: [
@if (isset($bargraph))
@for ($year = $nowyear; $year >= $startyear; $year--)
{ name:
{{ $year +543 }},
data: [
@for ($month = 1; $month <= 12; $month++)
@foreach ($bargraph as $graph)
@if ($year == $nowyear)
{{ $graph->Currentyear }},
@elseif($year >= $startyear)
{{ $graph->Previousyear }},
@else
0
@endif
@endforeach
@endfor
]
},
@endfor
@endif
]
|
ประวัติการแก้ไข 2018-06-15 16:13:34
 |
 |
 |
 |
Date :
2018-06-15 16:11:37 |
By :
nottpoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|