|
|
|
ขอถามเกี่ยวกับการทำ nested comment หน่อยครับ..............? |
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2012-05-17 17:59:48 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมพอคิดลักษณะง่ายๆ คล้ายๆ ด้านบนคับ แต่ไอ้ด้านบนที่บอกมาคิดว่าจะยากสำหรับผม นะ
ลองดูละกัน ว่าพอจะปรับได้ไหม
Code (PHP)
$posts = array();
$posts[] = array('thread' =>'01', 'body'=> 'test1');
$posts[] = array('thread' =>'01.00', 'body'=> 'test2');
$posts[] = array('thread' =>'01.00.00', 'body'=> 'test3');
$posts[] = array('thread' =>'01.00.01', 'body'=> 'test4');
$posts[] = array('thread' =>'01.00.00.00', 'body'=> 'test5');
$posts[] = array('thread' =>'01.01', 'body'=> 'test6');
$posts[] = array('thread' =>'02', 'body'=> 'test7');
$last_stack = -1;
$stack = 0;
foreach ($posts as $post)
{
$stack = count(explode('.', $post['thread'])) - 1;
if ($last_stack != $stack)
echo '<div style="color:red; margin-left:'.(20 * $stack).'px">'.$post['thread'].'</div>';
$last_stack = $stack;
echo '<div style="margin-left:'.(20 * $stack).'px">';
echo $post['body'];
echo '</div>';
}
ลองเทสต์ดูคับ
|
ประวัติการแก้ไข 2012-05-17 20:31:38 2012-05-17 20:33:04
|
|
|
|
Date :
2012-05-17 20:26:16 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เทสต์แล้วไม่ได้อะครับ ผมทำส่วน insert เสร็จหมดแล้ว ทีนี้ตรงแสดง comment แบบลำดับชั้น (nested) มันยากจริง!
คุณเอี่ยวลองโหลดไปทดลองดูครับ
http://code.google.com/p/agni-cms/downloads/list อันที่เขียนว่า 1.0.6.1 นะครับ
จากข้างบน มันไม่ได้แสดงมาเป็นลำดับๆอะครับ คือมันลำดับ indent ก็จริง แต่มันไม่ได้คลุมพวก child ด้วย
มันคล้ายๆแบบนี้
Code (PHP)
<ul>
<li>1
<ul>
<li>1.1</li>
</ul>
</li>
<li>2</li>
</ul>
แต่อยากให้มันเป็นแบบนี้.
Code (PHP)
<article>1</article>
<div class="indent">
<article>1.1</article>
<div class="indent">
<article>1.1.1</article>
<div class="indent">
<article>1.1.1.1</article>
</div>
</div>
<article>1.2</article>
</div>
<article>2</article>
|
|
|
|
|
Date :
2012-05-17 22:20:16 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แล้วจะต้องแบ่งหน้าด้วยอีกนี่ล่ะครับ -__________-"
|
|
|
|
|
Date :
2012-05-17 22:31:35 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คิดว่าโค้ดนี้น่าจะโอเค ผมทดสอบใช้ด้วย ul
Code (PHP)
$posts = array();
$posts[] = array('thread' =>'01', 'body'=> 'test1');
$posts[] = array('thread' =>'01.00', 'body'=> 'test2');
$posts[] = array('thread' =>'01.00.00', 'body'=> 'test3');
$posts[] = array('thread' =>'01.00.01', 'body'=> 'test4');
$posts[] = array('thread' =>'01.00.00.00', 'body'=> 'test5');
$posts[] = array('thread' =>'01.01', 'body'=> 'test6');
$posts[] = array('thread' =>'02', 'body'=> 'test7');
$posts[] = array('thread' =>'02.00', 'body'=> 'test8');
$posts[] = array('thread' =>'02.01', 'body'=> 'test9');
$posts[] = array('thread' =>'02.02', 'body'=> 'test10');
$posts[] = array('thread' =>'02.02.00', 'body'=> 'test11');
$last_stack = -1;
$stack = 0;
echo '<ul>';
foreach ($posts as $post)
{
$stack = count(explode('.', $post['thread'])) -1;
if ($last_stack == -1 || $last_stack == $stack)
{
// if ($last_stack == -1) // stack title
// echo '<li style="color:green">'.$post['thread'].'</li>'; // stack title
echo '<li>'.$post['body'].'</li>';
}
elseif ($stack > $last_stack)
{
echo '<ul>';
// echo '<li style="color:green">'.$post['thread'].'</li>';// stack title
echo '<li>'.$post['body'].'</li>';
}
elseif ($stack < $last_stack)
{
$back_stack = $last_stack - $stack;
for ($i=0; $i < $back_stack; $i++)
echo '</ul>';
echo '<li>'.$post['body'].'</li>';
}
$last_stack = $stack;
}
echo '</ul>';
ไม่เคยใช้ <article>
เอา คอมเมนต์ บรรทัดที่ มี stack title ออก เพื่อแสดงหัว
|
ประวัติการแก้ไข 2012-05-17 23:56:48 2012-05-18 00:05:21 2012-05-18 00:10:00
|
|
|
|
Date :
2012-05-17 23:55:11 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ul li มันไม่ซ้อนกันอย่างถูกต้องอะครับคุณเอี่ยว
Code
-- phpMyAdmin SQL Dump
-- version 3.3.10
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 18, 2012 at 12:30 AM
-- Server version: 5.5.23
-- PHP Version: 5.3.11
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `v_agni`
--
-- --------------------------------------------------------
--
-- Table structure for table `an_comments`
--
CREATE TABLE IF NOT EXISTS `an_comments` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`post_id` int(11) DEFAULT NULL,
`account_id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL COMMENT 'comment author''s name',
`subject` varchar(255) DEFAULT NULL,
`comment_body_value` longtext,
`email` varchar(255) DEFAULT NULL COMMENT 'comment author''s email',
`homepage` varchar(255) DEFAULT NULL COMMENT 'comment author''s homepage',
`comment_status` int(1) NOT NULL DEFAULT '0' COMMENT '0=not publish, 1=published',
`comment_spam_status` varchar(100) NOT NULL DEFAULT 'normal' COMMENT 'comment spam status (normal, spam, ham, what ever)',
`ip_address` varchar(50) DEFAULT NULL,
`user_agent` varchar(255) DEFAULT NULL,
`comment_add` bigint(20) DEFAULT NULL,
`comment_add_gmt` bigint(20) DEFAULT NULL,
`comment_update` bigint(20) DEFAULT NULL,
`comment_update_gmt` bigint(20) DEFAULT NULL,
`thread` varchar(255) DEFAULT NULL,
PRIMARY KEY (`comment_id`),
KEY `account_id` (`account_id`),
KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;
--
-- Dumping data for table `an_comments`
--
INSERT INTO `an_comments` (`comment_id`, `parent_id`, `post_id`, `account_id`, `name`, `subject`, `comment_body_value`, `email`, `homepage`, `comment_status`, `comment_spam_status`, `ip_address`, `user_agent`, `comment_add`, `comment_add_gmt`, `comment_update`, `comment_update_gmt`, `thread`) VALUES
(1, 0, 37, 1, 'admin', '1', '1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200900, 1337175700, 1337200900, 1337175700, '1/'),
(2, 0, 37, 1, 'admin', '2', '2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200905, 1337175705, 1337200905, 1337175705, '2/'),
(3, 0, 37, 1, 'admin', '3', '3', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200910, 1337175710, 1337200910, 1337175710, '3/'),
(4, 1, 37, 1, 'admin', '1.1', '1.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200973, 1337175773, 1337200973, 1337175773, '1.1/'),
(5, 1, 37, 1, 'admin', '1.2', '1.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200982, 1337175782, 1337200982, 1337175782, '1.2/'),
(6, 4, 37, 1, 'admin', '1.1.1', '1.1.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337200995, 1337175795, 1337200995, 1337175795, '1.1.1/'),
(7, 4, 37, 1, 'admin', '1.1.2', '1.1.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337201001, 1337175801, 1337201001, 1337175801, '1.1.2/'),
(8, 5, 37, 1, 'admin', '1.2.1', '1.2.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337201023, 1337175823, 1337201023, 1337175823, '1.2.1/'),
(9, 5, 37, 1, 'admin', '1.2.2', '1.2.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337201029, 1337175829, 1337201029, 1337175829, '1.2.2/'),
(10, 6, 37, 1, 'admin', '1.1.1.1', '1.1.1.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337201156, 1337175956, 1337201156, 1337175956, '1.1.1.1/'),
(14, 0, 37, 1, 'admin', '4', '4', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337252839, 1337227639, 1337252839, 1337227639, '4/'),
(15, 14, 37, 1, 'admin', '4.1', '4.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337252851, 1337227651, 1337252851, 1337227651, '4.1/'),
(16, 15, 37, 1, 'admin', '4.1.1', '4.1.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337252860, 1337227660, 1337252860, 1337227660, '4.1.1/'),
(17, 15, 37, 1, 'admin', '4.1.2', '4.1.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337255387, 1337230187, 1337255387, 1337230187, '4.1.2/'),
(18, 15, 37, 0, 'chrome', '4.1.3', '4.1.3', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337255539, 1337230339, 1337255539, 1337230339, '4.1.3/'),
(19, 15, 37, 0, 'chrome', '4.1.4', '4.1.4', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337255656, 1337230456, 1337255656, 1337230456, '4.1.4/'),
(20, 15, 37, 1, 'admin', '4.1.5', '4.1.5', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337255813, 1337230613, 1337255813, 1337230613, '4.1.5/'),
(21, 15, 37, 0, 'chrome', '4.1.6', '4.1.6', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337255850, 1337230650, 1337255850, 1337230650, '4.1.6/'),
(22, 15, 37, 0, 'chrome', '4.1.7', '4.1.7', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337256370, 1337231170, 1337256370, 1337231170, '4.1.7/'),
(23, 15, 37, 0, 'chrome', '4.1.8', '4.1.8', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337256512, 1337231312, 1337256512, 1337231312, '4.1.8/'),
(24, 15, 37, 0, 'chrome', '4.1.9', '4.1.9', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337256626, 1337231426, 1337256626, 1337231426, '4.1.9/'),
(25, 15, 37, 0, 'chrome', '4.1.10', '4.1.10', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337256709, 1337231509, 1337256709, 1337231509, '4.1.10/'),
(26, 14, 37, 1, 'admin', '4.2', '4.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 1337256909, 1337231709, 1337256909, 1337231709, '4.2/'),
(27, 2, 37, 0, 'chrome', '2.1', '2.1', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337266122, 1337240922, 1337266122, 1337240922, '2.1/'),
(28, 2, 37, 0, 'chrome', '2.2', '2.2', NULL, NULL, 1, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337266197, 1337240997, 1337266197, 1337240997, '2.2/'),
(29, 0, 37, 0, 'chrome', '5', '5', NULL, NULL, 0, 'normal', '::1', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 1337266529, 1337241329, 1337266529, 1337241329, '5/');
ลอง sql ไปดู
Code (PHP)
select * from an_comments where 1 and comment_status = 1 and post_id = '37' order by abs(thread) asc
มันออกมาเป็น Code
<ul><li>1</li><ul><li>1.1</li><ul><li>1.1.1</li><li>1.1.2</li><ul><li>1.1.1.1</li></ul></ul><li>1.2</li><ul><li>1.2.1</li><li>1.2.2</li></ul></ul><li>2</li><ul><li>2.1</li><li>2.2</li></ul><li>3</li><li>4</li><ul><li>4.1</li><ul><li>4.1.1</li><li>4.1.2</li><li>4.1.3</li><li>4.1.4</li><li>4.1.5</li><li>4.1.6</li><li>4.1.7</li><li>4.1.8</li><li>4.1.9</li><li>4.1.10</li></ul><li>4.2</li></ul>
|
|
|
|
|
Date :
2012-05-18 00:33:53 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผืดอะไรตรงไหนเหรอคับ
|
|
|
|
|
Date :
2012-05-18 00:46:41 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันไม่ได้ลำดับซ้อนกันอะครับ
<ul><li>1</li><ul><li>1.1</li><ul><li>1.1.1</li><li>1.1.2</li><ul><li>1.1.1.1</li></ul></ul><li>1.2</li><ul><li>1.2.1</li><li>1.2.2</li></ul></ul><li>2</li><ul><li>2.1</li><li>2.2</li></ul><li>3</li><li>4</li><ul><li>4.1</li><ul><li>4.1.1</li><li>4.1.2</li><li>4.1.3</li><li>4.1.4</li><li>4.1.5</li><li>4.1.6</li><li>4.1.7</li><li>4.1.8</li><li>4.1.9</li><li>4.1.10</li></ul><li>4.2</li></ul>
มันเรียงติดทางซ้ายเป็นพรืดเลยครับ
|
|
|
|
|
Date :
2012-05-18 00:52:54 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul><li>1</li><ul><li>1.1</li><ul><li>1.1.1</li><li>1.1.2</li><ul><li>1.1.1.1</li></ul></ul><li>1.2</li><ul><li>1.2.1</li><li>1.2.2</li></ul></ul><li>2</li><ul><li>2.1</li><li>2.2</li></ul><li>3</li><li>4</li><ul><li>4.1</li><ul><li>4.1.1</li><li>4.1.2</li><li>4.1.3</li><li>4.1.4</li><li>4.1.5</li><li>4.1.6</li><li>4.1.7</li><li>4.1.8</li><li>4.1.9</li><li>4.1.10</li></ul><li>4.2</li></ul>
หมายความว่า มันเรียงลำดับ html ไม่ถูกต้อง หรือมัน ไม่ขึ้นบรรทัดใหม่ ไม่ indent ?
หรือว่ารูปแบบที่ต้องการไม่ใช่ตามในรูป ?
|
ประวัติการแก้ไข 2012-05-18 00:59:49
|
|
|
|
Date :
2012-05-18 00:57:52 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พอเข้าใจละ คับ เด๋วนอนก่อน เป็นไข้ ไว้จะดูทีหลังคับ
|
|
|
|
|
Date :
2012-05-18 01:02:30 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ครับ
ทั้ง html ไม่ถูก ทั้งไม่ indent อะครับ
|
|
|
|
|
Date :
2012-05-18 01:05:06 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คิดว่าโค้ดนี่จะได้ html ที่ถูกต้องที่สุดนะคับ เพียงแต่การทำ indent มันลำบากยิ่งกว่าอีก
Code (PHP)
$posts = array();
$posts[] = array('thread' =>'01', 'body'=> 'test1');
$posts[] = array('thread' =>'01.00', 'body'=> 'test2');
$posts[] = array('thread' =>'01.00.00', 'body'=> 'test3');
$posts[] = array('thread' =>'01.00.01', 'body'=> 'test4');
$posts[] = array('thread' =>'01.00.00.00', 'body'=> 'test5');
$posts[] = array('thread' =>'01.01', 'body'=> 'test6');
$posts[] = array('thread' =>'02', 'body'=> 'test7');
$posts[] = array('thread' =>'02.00', 'body'=> 'test8');
$posts[] = array('thread' =>'02.01', 'body'=> 'test9');
$posts[] = array('thread' =>'02.02', 'body'=> 'test10');
$posts[] = array('thread' =>'02.02.00', 'body'=> 'test11');
$post_count = count($posts);
$last_stack = -1;
$counter = $stack = 0;
echo '<ul>';
foreach ($posts as $post)
{
$counter++;
$stack = count(explode('.', $post['thread'])) - 1;
if ($last_stack === $stack || $last_stack === -1)
{
echo '<li>'.$post['body'].'</li>';
}
elseif ($stack > $last_stack)
{
echo '<li>';
echo '<ul>';
echo '<li>'.$post['body'].'</li>';
}
elseif ($stack < $last_stack)
{
$back_stack = $last_stack - $stack;
for ($i=0; $i < $back_stack; $i++)
echo '</ul></li>';
echo '<li>'.$post['body'].'</li>';
}
if ($post_count === $counter)
{
for ($i=0; $i < $stack; $i++)
echo '</ul></li>';
}
$last_stack = $stack;
}
echo '</ul>';
Quote:<ul><li>test1</li><li><ul><li>test2</li><li><ul><li>test3</li><li>test4</li><li><ul><li>test5</li></ul></li></ul></li><li>test6</li></ul></li><li>test7</li><li><ul><li>test8</li><li>test9</li><li>test10</li><li><ul><li>test11</li></ul></li></ul></li></ul>
หลังจาก เอามาทำ indent
Code (PHP)
<ul>
<li>test1</li>
<li>
<ul>
<li>test2</li>
<li>
<ul>
<li>test3</li>
<li>test4</li>
<li>
<ul>
<li>test5</li>
</ul>
</li>
</ul>
</li>
<li>test6</li>
</ul>
</li>
<li>test7</li>
<li>
<ul>
<li>test8</li>
<li>test9</li>
<li>test10</li>
<li>
<ul>
<li>test11</li>
</ul>
</li>
</ul>
</li>
</ul>
|
ประวัติการแก้ไข 2012-05-18 08:38:25 2012-05-18 08:45:46 2012-05-18 08:59:59 2012-05-18 09:01:37
|
|
|
|
Date :
2012-05-18 08:34:23 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอ @logic by PJGUNNER www.pjgunner.com
จริงๆ li แทกปิด เป็น optional ไม่ใส่ก็ได้
|
ประวัติการแก้ไข 2012-05-18 08:54:15
|
|
|
|
Date :
2012-05-18 08:49:38 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|