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 > จะทำ array ต่อไปนี้ให้อยู่ในรูปแบบ ul li แบบเรียงลำดับถูกต้องได้ยังไงครับ (ช่วยผมทีได้โปรด)



 

จะทำ array ต่อไปนี้ให้อยู่ในรูปแบบ ul li แบบเรียงลำดับถูกต้องได้ยังไงครับ (ช่วยผมทีได้โปรด)

 



Topic : 057240



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



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




อ้างอิงหลักการจาก
http://www.phpriot.com/articles/nested-trees-1
และโค้ดจาก
http://www.phpriot.com/articles/nested-trees-2
มันคือ category nested tree ที่เรียงโดยใช้ nleft nright nlevel(depth) มาช่วย ผมเอามาปรับแต่งใหม่ส่วนหนึ่งแล้ว ติดตรงการ list ออกมา

ทีนี้ตอนแสดงผลจะเรียกแบบนี้
Code (PHP)
$list = $this->getDescendants($category_id, true);



จะได้
Code
Array
(
[1] => stdClass Object
(
[category_id] => 1
[parent_id] => 0
[category_name] => General Resources
[category_description] =>
[category_uri] => general-resources
[nleft] => 1
[nright] => 22
[nlevel] => 1
)

[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 1
[category_name] => Books & Publications
[category_description] =>
[category_uri] => books-publications
[nleft] => 2
[nright] => 9
[nlevel] => 2
)

[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[category_name] => Apache
[category_description] =>
[category_uri] => apache
[nleft] => 3
[nright] => 4
[nlevel] => 3
)

[7] => stdClass Object
(
[category_id] => 7
[parent_id] => 4
[category_name] => MySQL
[category_description] =>
[category_uri] => mysql
[nleft] => 5
[nright] => 6
[nlevel] => 3
)

[6] => stdClass Object
(
[category_id] => 6
[parent_id] => 4
[category_name] => PostgreSQL
[category_description] =>
[category_uri] => postgresql
[nleft] => 7
[nright] => 8
[nlevel] => 3
)

[2] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[category_name] => Code Paste
[category_description] =>
[category_uri] => code-paste
[nleft] => 10
[nright] => 11
[nlevel] => 2
)

[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 1
[category_name] => Documentation
[category_description] =>
[category_uri] => documentation
[nleft] => 12
[nright] => 13
[nlevel] => 2
)

[8] => stdClass Object
(
[category_id] => 8
[parent_id] => 1
[category_name] => Links
[category_description] =>
[category_uri] => links
[nleft] => 14
[nright] => 21
[nlevel] => 2
)

[9] => stdClass Object
(
[category_id] => 9
[parent_id] => 8
[category_name] => Databases
[category_description] =>
[category_uri] => databases
[nleft] => 15
[nright] => 16
[nlevel] => 3
)

[10] => stdClass Object
(
[category_id] => 10
[parent_id] => 8
[category_name] => Generators
[category_description] =>
[category_uri] => generators
[nleft] => 17
[nright] => 18
[nlevel] => 3
)

[11] => stdClass Object
(
[category_id] => 11
[parent_id] => 8
[category_name] => Portals
[category_description] =>
[category_uri] => portals
[nleft] => 19
[nright] => 20
[nlevel] => 3
)

[12] => stdClass Object
(
[category_id] => 12
[parent_id] => 0
[category_name] => root category2
[category_description] => root of category
[category_uri] => root-category2
[nleft] => 23
[nright] => 32
[nlevel] => 1
)

[13] => stdClass Object
(
[category_id] => 13
[parent_id] => 12
[category_name] => sub category 2.1
[category_description] =>
[category_uri] => sub-category-21
[nleft] => 24
[nright] => 29
[nlevel] => 2
)

[14] => stdClass Object
(
[category_id] => 14
[parent_id] => 13
[category_name] => category 2.1.1
[category_description] =>
[category_uri] => sub-category-211
[nleft] => 25
[nright] => 26
[nlevel] => 3
)

[15] => stdClass Object
(
[category_id] => 15
[parent_id] => 13
[category_name] => category 2.1.2
[category_description] =>
[category_uri] => sub-category-212
[nleft] => 27
[nright] => 28
[nlevel] => 3
)

[16] => stdClass Object
(
[category_id] => 16
[parent_id] => 12
[category_name] => sub category 2.2
[category_description] =>
[category_uri] => sub-category-22
[nleft] => 30
[nright] => 31
[nlevel] => 2
)

)



ผมต้องการจะเอา array ชุดนี้เรียงออกมาเป็น
<ul>
<li>General Resources
<ul>
<li>Books & Publications
<ul>
<li>Apache</li>
<li>MySQL</li>
<li>PostgreSQL</li>
</ul>
</li>
<li>Code Paste</li>
... ไล่ลงไปเรื่อยๆ
</ul>
</li>
</ul>

ผมทำไม่เป็นครับ >_<



Tag : PHP, MySQL







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-03-14 17:17:23 By : mr.v View : 1342 Reply : 3
 

 

No. 1



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



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


ไม่มีใครทำได้เลยเหรอ T_T






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-14 21:11:45 By : mr.v
 


 

No. 2

Guest


หลังจากได้ array มาแล้ว ทำได้ไม่ยากครับ
ตอนแรกก็ loop รวบรวม parent_id เหมือนกันอยู่ในกลุ่มเดียวกันครับ

Code (PHP)
$rs = array();
$rs[4] = array(1,2,3); สมมุติว่า 1,2,3 คือ category_id ซึ่ง 1,2,3 นั้นมี parent_id เหมือนกันคือ 4
$rs[8] = array(5,6,7); สมมุติว่า 5,6,7 คือ category_id ซึ่ง 5,6,7 นั้นมี parent_id เหมือนกันคือ 8
$rs[10] = array(4,8); สมมุติว่า 4,8 คือ category_id ซึ่ง 4,8 นั้นมี parent_id เหมือนกันคือ 10


จากนั้นจะทำการเรียก recursive function เพื่อ
แสดงผล ul li ซ้อนกันแบบ tree

Code (PHP)
function show_tree($id){
    global $rs;
    if (!empty($rs[$id])){
    echo '<ul>';
        foreach($rs[$id] as $ch) {
            echo '<li>'; echo $ch; show_tree($ch);
            echo '</li>';
        }
    echo '</ul>';
    }
}


show_tree(10);

ข้างบนนี้จะต้องปรับปรุงโครงสร้างเพื่อให้ค้นหาข้อมูลใน array มาแสดงอีกครับ ตอนนี้แสดงแค่ category_id เท่านั้น
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-14 21:59:42 By : num
 

 

No. 3



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



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


ขอบคุณครับ ได้แล้วครับ

Code (PHP)
/**
	 * show category nested
	 * copy from http://stackoverflow.com/questions/1310649/getting-a-modified-preorder-tree-traversal-model-nested-set-into-a-ul
	 * @param int $category_id
	 * @return string
	 */
	function show_category_nested($category_id = 0) {
		$list = $this->getDescendants($category_id, true);
		$output = "";
		if ( is_array($list) ) {
			$current_depth = 1;
			$counter = 0;
			$output = "\n<ul class=\"category_tree\">\n";
			foreach ( $list as $key => $item ) {
				$node_depth = $item->nlevel;
				if($node_depth == $current_depth){
					if($counter > 0) $output .= "</li>\n";
				} elseif($node_depth > $current_depth){
					$output .= "\n".str_repeat("\t", $node_depth)."<ul>\n";
					$current_depth = $current_depth + ($node_depth - $current_depth);
				}elseif($node_depth < $current_depth){
					$output .= str_repeat("</li>\n</ul>\n",$current_depth - $node_depth);
					$output .= "</li>\n";
					$current_depth = $current_depth - ($current_depth - $node_depth);
				}
				$output .= str_repeat("\t", $node_depth) . "<li id=\"c".$key."\"";
				$output .= " class=\"lv$node_depth" . ($node_depth < 2 ? ' open' : '') . "\"";
				$output .= "><a href=\"".site_url("category/view/".urlencode($item->category_uri))."\">".$item->category_name."</a>";// แก้ไข link category ที่นี่
				++$counter;
			}
			$output .= str_repeat("</li>\n</ul>\n",$node_depth-1)."</li>\n";
			$output .= "</ul>\n\n";
		}
		return $output;
	}// show_category_nested


เอามาจาก http://stackoverflow.com/questions/1310649/getting-a-modified-preorder-tree-traversal-model-nested-set-into-a-ul
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-14 22:31:47 By : mr.v
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : จะทำ array ต่อไปนี้ให้อยู่ในรูปแบบ ul li แบบเรียงลำดับถูกต้องได้ยังไงครับ (ช่วยผมทีได้โปรด)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 00
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 อัตราราคา คลิกที่นี่