ช่วย ดูโค้ค นี้ ให้หน่อยครับทำไมแก้ไข กับลบข้อมูลไม่ได้ ท่านเทพ ช่วยที ครับ
Code (PHP)
<?php
$s = new Smarty();
$action = $_GET["action"];
switch ($action) {
/*
* TODO: index
*/
case "index":
$db = new db("not_work");
$not_works = $db
->where("user_id", get_user_id())
->order_by("not_work_created_date")
->find_all();
$s->assign("not_works", $not_works);
$s->assign("n", 1);
$s->assign("not_work_types", enum::getNotWorkType(true));
$s->display("not_work.tpl");
break;
/*
* TODO: form
*/
case "form":
$s->assign("not_work_types", enum::getNotWorkType());
$s->display("not_work_form.tpl");
break;
/*
* TODO: delete
*/
case "delete":
$sql = "DELETE FROM not_work WHERE not_work_id = $_GET[not_work_id]";
if (db::execute($sql)) {
redirect("index.php?url=not_work.php&action=index");
}
break;
/*
* TODO: save
*/
case "save":
// variable
$user_id = get_user_id();
$not_work_type = $_POST["not_work_type"];
$not_work_reason = $_POST["not_work_reason"];
$not_work_date_from = input_from_date("not_work_date_from");
$not_work_date_to = input_from_date("not_work_date_to");
$not_work_id = $_POST["not_work_id"];
// validate
if ($not_work_reason == "") {
$s->config_load("app.conf");
$s->assign("message", $s->get_config_vars("required"));
} else {
// sql command
if (empty($not_work_id)) {
$sql = "
INSERT INTO not_work (
user_id,
not_work_type,
not_work_reason,
not_work_date_from,
not_work_date_to,
not_work_created_date
) VALUES (
'$user_id',
'$not_work_type',
'$not_work_reason',
'$not_work_date_from',
'$not_work_date_to',
NOW()
)
";
// query
if (db::execute($sql)) {
// find last query id
$db = new db("not_work");
$not_work = $db
->where("user_id", $user_id)
->where_and("not_work_type", $not_work_type)
->where_and("not_work_reason", $not_work_reason)
->where_and("not_work_date_from", $not_work_date_from)
->where_and("not_work_date_to", $not_work_date_to)
->find();
// insert to not_work_result
$day = db::datediff($not_work_date_to, $not_work_date_from);
$not_work_id = $not_work["not_work_id"];
// insert first record
$sql = "
INSERT INTO not_work_result(not_work_id, not_work_date)
VALUES('$not_work_id', '$not_work_date_from')";
db::execute($sql);
// insert next record
for ($i = 1; $i <= $day; $i++) {
$not_work_date = db::day_add($not_work_date_from, $i);
$sql = "
INSERT INTO not_work_result(not_work_id, not_work_date)
VALUES('$not_work_id', '$not_work_date')";
db::execute($sql);
}
}
} else {
$sql = "
UPDATE not_work SET
not_work_type = '{$not_work_type}',
not_work_reason = '{$not_work_reason}',
not_work_date_from = '{$not_work_date_from}',
not_work_date_to = '{$not_work_date_to}'
WHERE not_work_id = $not_work_id
";
}
// redirect
redirect("index.php?url=not_work.php&action=index");
}
$s->assign($_POST);
$s->assign("not_work_types", enum::getNotWorkType());
$s->display("not_work_form.tpl");
break;
/*
* TODO: search
*/
case "search":
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$not_work_type = $_POST["not_work_type"];
$db = new db("not_work");
$not_works = $db
->where("not_work_date_from >=", $date_from)
->where_and("not_work_date_to <=", $date_to);
if (!empty($not_work_type)) {
$not_works = $not_works->where_and("not_work_type", $not_work_type);
}
$not_works = $not_works->find_all();
// assign value
$s->assign($_POST);
$s->assign("n", 1);
$s->assign("not_works", $not_works);
$s->assign("not_work_types", enum::getNotWorkType(true));
$s->assign("not_work_type", $not_work_type);
$s->display("not_work.tpl");
break;
/*
* TODO: edit
*/
case "edit":
$not_work_id = $_GET["not_work_id"];
$db = new db("not_work");
$not_work = $db->where("not_work_id", $not_work_id)->find();
$s->assign($not_work);
$s->display("not_work_form.tpl");
break;
/*
* TODO: report data
*/
case "report_data":
$db = new db();
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$depart_id = $_POST["depart_id"];
$section_id = $_POST["section_id"];
$sql = "
SELECT
* DATE_FORMAT(not_work_date, '%Y%m%d%h%i%s') AS day
FROM not_work_result
INNER JOIN not_work ON not_work.not_work_id = not_work_result.not_work_id
INNER JOIN user ON user.user_id = not_work.user_id
INNER JOIN section ON section.section_id = user.section_id
INNER JOIN depart ON depart.depart_id = section.depart_id
WHERE 1 > 0";
if ($date_from != "") {
$sql .= " AND not_work_date BETWEEN '{$date_from}' AND '{$date_to}' ";
}
if ($depart_id != "") {
$sql .= " AND section.depart_id = $depart_id";
}
if ($section_id != "") {
$sql .= " AND user.section_id = $section_id";
}
$sql .= " ORDER BY day";
$not_works = $db->query($sql);
$arr_not_works = array();
$size = count($not_works);
for ($i = 0; $i < $size; $i++) {
$not_work = $not_works[$i];
if ($i > 0) {
// before not_work_date
$before_not_work_date = $not_works[$i - 1]["not_work_date"];
if ($before_not_work_date == $not_work["not_work_date"]) {
$not_work["not_work_date"] = "";
}
}
$arr_not_works[] = $not_work;
}
$s->assign("not_works", $arr_not_works);
$s->assign("depart_ids", depart_find_select(true));
$s->assign("depart_id", $depart_id);
$s->assign("section_id", $section_id);
$s->display("not_work_report_data.tpl");
break;
}
?>
not_work.tpl
Code (PHP)
{literal}
<script type="text/javascript">
$(function() {
$("#cmdSearch").click(function() {
$("#formNoteSearch").submit();
});
});
</script>
{/literal}
<div class="panel_header">บันทึกประจำวัน</div>
<div class="panel">
<div class="top_button">
{button_new url='index.php?url=note.php&action=form'}
{button_search}
{button_print}
{button_backup}
{button_restore}
</div>
<div class="panel_content">
<!-- filter -->
<form method="post" id="formNoteSearch" action="index.php?url=note.php&action=search">
<table>
<tr>
<td>จากวันที่</td>
<td>{ui_dropdown_date name=date_from value=$date_from}</td>
<td>ถึงวันที่</td>
<td>{ui_dropdown_date name=date_to value=$date_to}</td>
</tr>
</table>
</form>
<!-- grid -->
<table class="grid">
<thead>
<tr>
<td width="35px">ลำดับ</td>
<td width="500px">รายละเอียด</td>
<td>วันที่บันทึก</td>
<td>แก้ไข</td>
<td>ลบ</td>
</tr>
</thead>
<tbody>
{foreach item=note from=$notes}
<tr>
<td align="center">{$n++}</td>
<td>{$note.note_detail}</td>
<td>{$note.note_created_date|to_thai_date}</td>
<td>{button_edit url='note.php&action=edit' param=note_id value=$note.note_id}</td>
<td>{button_delete url='note.php&action=delete' param=note_id value=$note.note_id}</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
</div>
not_work_form.tpl
Code (PHP)
{literal}
<script type="text/javascript">
$(function() {
$("#cmdSave").click(function() {
if (confirm(confirm_save)) {
$("#formNotWork").submit();
}
});
});
</script>
{/literal}
<div class="panel_header">บันทึกวันที่ไม่เข้ามาทำงาน</div>
<div class="panel">
<div class="top_button">
{button_cancel url='index.php?url=not_work.php&action=index'}
{button_save}
</div>
<div class="panel_content">
{include file=message.tpl}
<form method="post" id="formNotWork" action="index.php?url=not_work.php&action=save">
<table>
<tr>
<td>ตั้งแต่วันที่</td>
<td>{ui_dropdown_date name=not_work_date_from value=$not_work_date_from}</td>
</tr>
<tr>
<td>ถึงวันที่</td>
<td>{ui_dropdown_date name=not_work_date_to value=$not_work_date_to}</td>
</tr>
<tr>
<td>ประเภท</td>
<td>{html_options name=not_work_type options=$not_work_types selected=$not_work_type}</td>
</tr>
<tr valign="top">
<td>เหตุผล</td>
<td><textarea name="not_work_reason" cols="80" rows="8">{$not_work_reason}</textarea></td>
</tr>
</table>
<input type="hidden" name="not_work_id" value="{$not_work_id}" />
</form>
</div>
</div>
Tag : PHP, MySQL
Date :
2011-08-30 02:20:52
By :
มือใหม่
View :
923
Reply :
6
notwork.php
Code (PHP)
<?php
$s = new Smarty();
$action = $_GET["action"];
switch ($action) {
/*
* TODO: index
*/
case "index":
$db = new db("not_work");
$not_works = $db
->where("user_id", get_user_id())
->order_by("not_work_created_date")
->find_all();
$s->assign("not_works", $not_works);
$s->assign("n", 1);
$s->assign("not_work_types", enum::getNotWorkType(true));
$s->display("not_work.tpl");
break;
/*
* TODO: form
*/
case "form":
$s->assign("not_work_types", enum::getNotWorkType());
$s->display("not_work_form.tpl");
break;
/*
* TODO: delete
*/
case "delete":
$sql = "DELETE FROM not_work WHERE not_work_id = $_GET[not_work_id]";
if (db::execute($sql)) {
redirect("index.php?url=not_work.php&action=index");
}
break;
/*
* TODO: save
*/
case "save":
// variable
$user_id = get_user_id();
$not_work_type = $_POST["not_work_type"];
$not_work_reason = $_POST["not_work_reason"];
$not_work_date_from = input_from_date("not_work_date_from");
$not_work_date_to = input_from_date("not_work_date_to");
$not_work_id = $_POST["not_work_id"];
// validate
if ($not_work_reason == "") {
$s->config_load("app.conf");
$s->assign("message", $s->get_config_vars("required"));
} else {
// sql command
if (empty($not_work_id)) {
$sql = "
INSERT INTO not_work (
user_id,
not_work_type,
not_work_reason,
not_work_date_from,
not_work_date_to,
not_work_created_date
) VALUES (
'$user_id',
'$not_work_type',
'$not_work_reason',
'$not_work_date_from',
'$not_work_date_to',
NOW()
)
";
// query
if (db::execute($sql)) {
// find last query id
$db = new db("not_work");
$not_work = $db
->where("user_id", $user_id)
->where_and("not_work_type", $not_work_type)
->where_and("not_work_reason", $not_work_reason)
->where_and("not_work_date_from", $not_work_date_from)
->where_and("not_work_date_to", $not_work_date_to)
->find();
// insert to not_work_result
$day = db::datediff($not_work_date_to, $not_work_date_from);
$not_work_id = $not_work["not_work_id"];
// insert first record
$sql = "
INSERT INTO not_work_result(not_work_id, not_work_date)
VALUES('$not_work_id', '$not_work_date_from')";
db::execute($sql);
// insert next record
for ($i = 1; $i <= $day; $i++) {
$not_work_date = db::day_add($not_work_date_from, $i);
$sql = "
INSERT INTO not_work_result(not_work_id, not_work_date)
VALUES('$not_work_id', '$not_work_date')";
db::execute($sql);
}
}
} else {
$sql = "
UPDATE not_work SET
not_work_type = '{$not_work_type}',
not_work_reason = '{$not_work_reason}',
not_work_date_from = '{$not_work_date_from}',
not_work_date_to = '{$not_work_date_to}'
WHERE not_work_id = $not_work_id
";
}
// redirect
redirect("index.php?url=not_work.php&action=index");
}
$s->assign($_POST);
$s->assign("not_work_types", enum::getNotWorkType());
$s->display("not_work_form.tpl");
break;
/*
* TODO: search
*/
case "search":
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$not_work_type = $_POST["not_work_type"];
$db = new db("not_work");
$not_works = $db
->where("not_work_date_from >=", $date_from)
->where_and("not_work_date_to <=", $date_to);
if (!empty($not_work_type)) {
$not_works = $not_works->where_and("not_work_type", $not_work_type);
}
$not_works = $not_works->find_all();
// assign value
$s->assign($_POST);
$s->assign("n", 1);
$s->assign("not_works", $not_works);
$s->assign("not_work_types", enum::getNotWorkType(true));
$s->assign("not_work_type", $not_work_type);
$s->display("not_work.tpl");
break;
/*
* TODO: edit
*/
case "edit":
$not_work_id = $_GET["not_work_id"];
$db = new db("not_work");
$not_work = $db->where("not_work_id", $not_work_id)->find();
$s->assign($not_work);
$s->display("not_work_form.tpl");
break;
/*
* TODO: report data
*/
case "report_data":
$db = new db();
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$depart_id = $_POST["depart_id"];
$section_id = $_POST["section_id"];
$sql = "
SELECT
* DATE_FORMAT(not_work_date, '%Y%m%d%h%i%s') AS day
FROM not_work_result
INNER JOIN not_work ON not_work.not_work_id = not_work_result.not_work_id
INNER JOIN user ON user.user_id = not_work.user_id
INNER JOIN section ON section.section_id = user.section_id
INNER JOIN depart ON depart.depart_id = section.depart_id
WHERE 1 > 0";
if ($date_from != "") {
$sql .= " AND not_work_date BETWEEN '{$date_from}' AND '{$date_to}' ";
}
if ($depart_id != "") {
$sql .= " AND section.depart_id = $depart_id";
}
if ($section_id != "") {
$sql .= " AND user.section_id = $section_id";
}
$sql .= " ORDER BY day";
$not_works = $db->query($sql);
$arr_not_works = array();
$size = count($not_works);
for ($i = 0; $i < $size; $i++) {
$not_work = $not_works[$i];
if ($i > 0) {
// before not_work_date
$before_not_work_date = $not_works[$i - 1]["not_work_date"];
if ($before_not_work_date == $not_work["not_work_date"]) {
$not_work["not_work_date"] = "";
}
}
$arr_not_works[] = $not_work;
}
$s->assign("not_works", $arr_not_works);
$s->assign("depart_ids", depart_find_select(true));
$s->assign("depart_id", $depart_id);
$s->assign("section_id", $section_id);
$s->display("not_work_report_data.tpl");
break;
}
?>
Date :
2011-08-30 02:59:48
By :
มือใหม่
สงสัยอาจจะได้ debug เอาค่า SQL ออกมาดูครับ
Date :
2011-08-30 06:31:11
By :
webmaster
สงสัยอาจจะได้ debug เอาค่า SQL ออกมาดูครับ .....?
Date :
2011-08-30 10:55:49
By :
มือใหม่
ปุ่มที่กด Delelte เป็น note_id
{button_delete url='note.php&action=delete' param=note_id value=$note.note_id}
เงื่อนไขในการลบ ใน Where เป็น not_work_id
case "delete":
$sql = "DELETE FROM not_work WHERE not_work_id = $_GET[not_work_id]";
if (db::execute($sql)) {
redirect("index.php?url=not_work.php&action=index");
}
break;
มันคนละตัวกันหรือเปล่าครับ
อันนี้ไม่แน่ใจหน่ะครับ เพราะไม่เคยใช้ Smarty
Date :
2011-08-30 11:05:01
By :
ไวยวิทย์
อัน เด๋วกันครับ เวลากด {button_delete url='note.php&action=delete' param=note_id value=$note.note_id}จะ ลบ ข้อมูลที่ยุ ใน row น่ะ ครับ
Date :
2011-08-30 11:30:05
By :
มือใหม่
มี ตัวอย่าง ที่ลบแก้ไขได้ เป็นปกติ ครับ เพราะ อันนี้ มันดึง ข้อมูล มาไม่เยอะ
note.php
Code (PHP)
<?php
$action = $_GET["action"];
$s = new Smarty();
switch ($action) {
/*
* TODO: index
*/
case "index":
$db = new db("note");
// data
$notes = $db
->where("user_id", get_user_id())
->where_and("YEAR(note_created_date)", "YEAR(NOW())", false)
->where_and("MONTH(note_created_date)", "MONTH(NOW())", false)
->where_and("DAY(note_created_date)", "DAY(NOW())", false)
->order_by("note_created_date", "desc")
->find_all();
// assign
$s->assign("n", 1);
$s->assign("notes", $notes);
$s->display("note.tpl");
break;
/*
* TODO: form
*/
case "form":
$s->display("note_form.tpl");
break;
/*
* TODO: delete
*/
case "delete":
$sql = "DELETE FROM note WHERE note_id = $_GET[note_id]";
if (db::execute($sql)) {
redirect("index.php?url=note.php&action=index");
}
break;
/*
* TODO: save
*/
case "save":
// variable
$note_id = $_POST["note_id"];
$note_detail = $_POST["note_detail"];
$user_id = get_user_id();
// validate
if ($note_detail == "") {
$s->config_load("app.conf");
$s->assign("message", $s->get_config_vars("required"));
} else {
// sql command
if (empty($note_id)) {
$sql = "
INSERT INTO note(user_id, note_detail, note_created_date)
VALUES($user_id, '$note_detail', NOW())
";
} else {
$sql = "
UPDATE note SET
note_detail = '$note_detail'
WHERE note_id = $note_id
";
}
// query
if (db::execute($sql)) {
redirect("index.php?url=note.php&action=index");
}
}
// display
$s->assign($_POST);
$s->display("note_form.tpl");
break;
/*
* TODO: edit
*/
case "edit":
$note_id = $_GET["note_id"];
$db = new db("note");
$note = $db->where("note_id", $note_id)->find();
$s->assign($note);
$s->display("note_form.tpl");
break;
/*
* TODO: search
*/
case "search":
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$db = new db("note");
// data
$notes = $db
->where("user_id", get_user_id())
->between("note_created_date", $date_from, $date_to)
->order_by("note_created_date", "desc")
->find_all();
// assign
$s->assign("n", 1);
$s->assign("notes", $notes);
$s->display("note.tpl");
break;
}
?>
=======================================
note.tpl
Code (PHP)
<?php
$action = $_GET["action"];
$s = new Smarty();
switch ($action) {
/*
* TODO: index
*/
case "index":
$db = new db("note");
// data
$notes = $db
->where("user_id", get_user_id())
->where_and("YEAR(note_created_date)", "YEAR(NOW())", false)
->where_and("MONTH(note_created_date)", "MONTH(NOW())", false)
->where_and("DAY(note_created_date)", "DAY(NOW())", false)
->order_by("note_created_date", "desc")
->find_all();
// assign
$s->assign("n", 1);
$s->assign("notes", $notes);
$s->display("note.tpl");
break;
/*
* TODO: form
*/
case "form":
$s->display("note_form.tpl");
break;
/*
* TODO: delete
*/
case "delete":
$sql = "DELETE FROM note WHERE note_id = $_GET[note_id]";
if (db::execute($sql)) {
redirect("index.php?url=note.php&action=index");
}
break;
/*
* TODO: save
*/
case "save":
// variable
$note_id = $_POST["note_id"];
$note_detail = $_POST["note_detail"];
$user_id = get_user_id();
// validate
if ($note_detail == "") {
$s->config_load("app.conf");
$s->assign("message", $s->get_config_vars("required"));
} else {
// sql command
if (empty($note_id)) {
$sql = "
INSERT INTO note(user_id, note_detail, note_created_date)
VALUES($user_id, '$note_detail', NOW())
";
} else {
$sql = "
UPDATE note SET
note_detail = '$note_detail'
WHERE note_id = $note_id
";
}
// query
if (db::execute($sql)) {
redirect("index.php?url=note.php&action=index");
}
}
// display
$s->assign($_POST);
$s->display("note_form.tpl");
break;
/*
* TODO: edit
*/
case "edit":
$note_id = $_GET["note_id"];
$db = new db("note");
$note = $db->where("note_id", $note_id)->find();
$s->assign($note);
$s->display("note_form.tpl");
break;
/*
* TODO: search
*/
case "search":
$date_from = input_from_date("date_from");
$date_to = input_from_date("date_to");
$db = new db("note");
// data
$notes = $db
->where("user_id", get_user_id())
->between("note_created_date", $date_from, $date_to)
->order_by("note_created_date", "desc")
->find_all();
// assign
$s->assign("n", 1);
$s->assign("notes", $notes);
$s->display("note.tpl");
break;
}
?>
======================================
note_form.tpl
Code (PHP)
{literal}
<script type="text/javascript">
$(function() {
$("#cmdSave").click(function() {
if (confirm(confirm_save)) {
$("#formNote").submit();
}
});
});
</script>
{/literal}
<div class="panel_header">บันทึกประจำวัน</div>
<div class="panel">
<div class="top_button">
{button_cancel url='index.php?url=note.php&action=index'}
{button_save}
</div>
<div class="panel_content">
{include file=message.tpl}
<form method="post" id="formNote" action="index.php?url=note.php&action=save">
<textarea name="note_detail" cols="100" rows="10">{$note_detail}</textarea>
<input type="hidden" name="note_id" value="{$note_id}" />
</form>
</div>
</div>
Date :
2011-08-30 11:35:00
By :
มือใหม่
Load balance : Server 02