|
 |
|
ตัวอย่าง tree menu จาก ฐานข้อมูล คือพอจะเข้าใจหลักการ แต่ไม่รู้จะวางอะไรยังไงดี |
|
 |
|
|
 |
 |
|
ถ้าสามารถ ซ้อน ซับเมนู ได้ด้วยยิ่งดี คือถ้าคลิกที่ เมนูหลัก ก็ค่อยให้ ซับเมนู โชว์ขึ้นมา เพราะถ้าเรามีซับเมนูเยอะๆ จะได้ไม่ยาวไป ตอนนี้ กำลังหาอ่านจาก อินเตอร์เน็ท แต่ยังไม่เจอที่เข้าใจ
|
 |
 |
 |
 |
Date :
2011-12-28 13:09:04 |
By :
มือใหม่หัดขับ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ผมใช้อยู่ครับ มีตัวอย่างการใช้เมนูจากฐานข้อมูลด้วย จะใช้ sub menu เท่าไหร่ก็ได้
http://www.gosu.pl/MyGosuMenu/
|
ประวัติการแก้ไข 2011-12-28 13:31:19
 |
 |
 |
 |
Date :
2011-12-28 13:30:34 |
By :
Songkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือตอนนี้ทำ แบบ gosuMenu ได้แล้วค่ะ แต่ปัญหาคือ เราดึงทั้ง เมนู กับ ซับเมนู มาจากฐานข้อมูล ตอนนี้เราดึง เมนูจากตาราง catagory มาได้แล้ว แต่ไม่เข้าใจตรง ที่จะดึง ซับเมนูจากตาราง product โดยใช้ cat_id ซึ่งเป็น FK ในตาราง product ยังไง คือ ตอนนี้เราดึง ซับเมนูไม่ได้ เพราะเราไม่เข้าใจอะ รบกวนผู้รู้ช่วยอธิบายหน่อยนะค่ะ
|
 |
 |
 |
 |
Date :
2011-12-28 14:32:23 |
By :
มือใหม่หัดขับ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็นสคริปจากจาก Gosu จะมีหลักการก็คือ เราต้องคิวรี่ข้อมูลออกมาให้อยู่ในรูปของตาราง โดยเน้น 3 ฟิลดิ์ คือ id, name, id_par เพราะฉะนั้นจะมีกี่ตารางก็แล้วแต่ จะต้องคิวรี่ออกมาให้ได้ 3 ฟิลดิ์แล้วก็ตั้งชื่อตามนี้ เพราะฉะนั้นถ้าในกรณีเจ้าของกระทู้ต้องใช้ UNION JOIN เข้ามาช่วยเชื่อมต่อตาราง ให้สังเกตุดูตรงฟิลดิ์ id_par เพราะมันคือเลขเมนูหลักและ Sub menu ทั้งหมดนั่นเอง
|
 |
 |
 |
 |
Date :
2011-12-28 16:40:56 |
By :
Songkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดูโค๊ดหน่อยครับ กำลังติดปัญหานี้อยู่พอดี
งง ตรงที่จะเชื่อมต่อ ข้อมูล กับ ด้าต้าเบส
|
ประวัติการแก้ไข 2011-12-28 16:59:18
 |
 |
 |
 |
Date :
2011-12-28 16:52:10 |
By :
okboou |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขออภัยนะครับ พอดีตอนนี้อยู่บ้านแล้ว ถ้าอย่างไร พรุ่งนี้จะเอาตัวอย่างมาให้ดูนะครับ (อาจจะแก้ปัญหากันได้แล้วก็ได้ ^^ )
|
 |
 |
 |
 |
Date :
2011-12-28 19:05:32 |
By :
Songkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามสัญญาครับ ..... ผมอาจจะอธิบายได้ไม่ดีเท่าไหร่ มันเป็น code ที่ผมทำไว้นานแล้ว ผ่านการแก้ไขมาพอสมควร ที่ผมจะเอามาเป็นตัวอย่างนี้เป็น code ที่ผมดัดแปลงนิดหน่อยจาก gosuMenu เพราะต้องการแสดงเมนูจาก database mysql เพื่อให้สามารถตวจสอบการเข้าถึงข้อมูลของแต่ละ user ได้สะดวกยิ่งขึ้น ความจริงผมแก้ไฟล์อื่นด้วยที่เกี่ยวข้อง ข้างล่างนี้จะนำเสนอเฉพาะไฟล์ที่เป็นไฟล์แสดงผล tree นะครับ ลองเอาไปเทียบดูกับตัวอย่างที่เขาให้มาได้เลย
Code (PHP)
session_start();
include('inc/dbclass/connect.php'); //ส่วนของการ connection ขึ้นอยู่กับของแต่ละคน (ยังไงก็ได้)
function tree_html($id = null)
{
$ret = '';
if (null == $id) $q_id = 'id_par IS NULL';
else $q_id = 'id_par = '.(int)$id;
if ($_SESSION['depid'] <> '4') { // บันทัดนี้ผมเพิ่มเข้ามาเพื่อใช้ในการตรวจสอบในกรณีพิเศษครับ อันนี้แค่ตัวอย่างนะครับ (ถ้าไม่ใช้ก็ตัดทิ้งได้)
$q_id .= ' AND adm = "N"';
}
$result = mysql_query('SELECT * FROM menu WHERE status = "Y" AND '.$q_id.' ORDER BY level'); // บันทัดนี้คือการคิวรี่ ปกติในตัวอย่างจะมี 3 ฟิลดิ์คือ id, name, id_par เท่านั้น แต่ผมเพิ่มการตรวจสอบสถานะและการจัดเรียงตาม level เอาไว้ด้วยเลยเป็นประมาณนี้ครับ เพราะฉะนั้นคิวรี่ของเดิมจะแตกต่างจากผมนะครับ
$childs = array(); //ตั้งค่าตัวแปรเพื่อรับค่า โดยกำหนดให้เป็น array
while ($row = mysql_fetch_assoc($result)) { // ต่อเนื่องจากบันทัดบนครับ loop เอาค่าที่คิวรี่ใส่ไว้ใน array
$childs[] = $row;
}
//ข้างล่างนี้เป็นส่วนสำคัญครับ คือการ loop เพื่อเอาค่าทั้งหมดมาจัดเรียงใส่ไว้ในตัวแปร $ret ก่อนที่จะนำไปใช้กับจาวา
//สังเกตุตรง tag a นะครับ เพราะมันคือ event ที่เราจะให้แต่ละเมนูคลิกแล้วทำอะไรบ้าง ซึ่งผมใช้ฟิลดิ์เก็บข้อมูลการคลิกของแต่ละเมนูแยกกัน
//เพราะฉะนั้น แต่ละเมนูจะมีการเรียกใช้ฟังก์ชั่นแตกต่างกันอย่างอิสระ ข้อดีก็คือถ้าเราทำระบบหลังบ้านก็สามารถ insert ข้อมูลฟังก์ชั่นลงฐานข้อมูลไปเลย
//จะเพิ่ม ลบ แก้ไข อย่างไรก็สะดวกครับ
foreach ($childs as $row)
{
$row['name'] = str_replace(array("'", '"'), '', $row[name]);
$row['name'] = htmlspecialchars($row['name']);
$nest = tree_html($row[id]);
if ($nest) {
$ret .= '<div class="folder '.trim($row[iconclass]).'" id="tree-'.$row[id].'">';
$ret .= '<a href="Javascript:'.$row[script].'">'.$row[name].'</a>';
$ret .= $nest;
$ret .= '</div>';
} else {
$ret .= '<div class="doc '.trim($row[iconclass]).'" id="tree-'.$row[id].'">';
$ret .= '<a href="Javascript:'.$row[script].'" >'.$row[name].'</a>';
$ret .= '</div>';
}
}
return $ret;
}
?>
//ข้างล่างนี้คือที่จะเอาข้อมูลมาแสดงให้เป็นเมนู tree
<div class="DynamicTree">
<div class="top">Index</div>
<div class="wrap" id="tree">
<?php echo tree_html();?>
</div>
</div>
//ข้างล่างนี้ก็เป็นสคริปจาวาตามตัวอย่างครับ ตั้งค่าได้ตามชอบเลย
<script type="text/javascript">
var tree = new DynamicTree("tree");
tree.foldersAsLinks = true;
tree.foldersAsLinks2 = true;
tree.enableSetActive = true;
tree.path = 'img/icons/';
tree.icons = {
"help" : "img/Xiao Icon/51.png", // ส่วนนี้ก็เป็นการกำหนดให้เมนูบางเมนูมีไอคอนตามที่เรากำหนด (free style)
"home" : "img/Xiao Icon/61.png",
"homeOpen" : "img/Xiao Icon/63.png",
"exit" : "img/lock.gif",
"log" : "img/log.png",
"write" : "img/Xiao Icon/60.png",
"announce" : "img/002_26.png",
"adm" : "img/Xiao Icon/37.png",
"admOpen" : "img/Xiao Icon/38.png",
"admusr" : "img/user_edit.png",
"fload" : "img/Xiao Icon/22.png",
"stat" : "img/Xiao Icon/53.png"
};
tree.init();
tree.openTo("tree-19"); // ส่วนนี้คือการกำหนดให้เมนูหลักบางเมนู expand ออกมาตั้งแต่การเปิดครั้งแรกเลย
tree.openTo("tree-1");
tree.openTo("tree-2");
tree.openTo("tree-21");
</script>
|
 |
 |
 |
 |
Date :
2011-12-29 08:52:58 |
By :
Songkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|