过去几天我一直在互联网上寻找解决问题的方法,但到目前为止还没有。我有一组页面需要作为分页处理,即显示我过去的项目和下一个项目。数组内容可以根据您查看的部分而改变,但设置始终相同。
我的数组:
Array
(
[19] => Array
(
[titill] => Kafli 1
[slug] => kafli1
[undir] => Array
(
[21] => Array
(
[titill] => 1.1 Serhljodar
[slug] => serhljodar
[kaflar] => Array
(
[0] => Array
(
[id] => 24
[titill] => Æfing 1
[slug] => aefing1
)
[1] => Array
(
[id] => 25
[titill] => Æfing 2
[slug] => æfing2
)
[2] => Array
(
[id] => 26
[titill] => Próf 1
[slug] => prof1
)
)
)
[22] => Array
(
[titill] => 1.2. Samhljóðar
[slug] => samhljodar
)
[23] => Array
(
[titill] => 1.3 Málnotkun
[slug] => malnotkun
)
)
)
[20] => Array
(
[titill] => Kafli 2
[slug] => kafli2
)
)
数组键是 post_id,slug 是 url。为了解释我想用这个数组实现什么,我写了一些我想做的例子:
- 如果我正在查看
localhost/kafli1
,我会看到下一个项目是localhost/kafli1/serhljodar
(键 #21)并且没有上一个项目。 - 如果我正在查看
localhost/kafli1/serhljodar
,我应该看到下一个项目是localhost/kafli1/serhljodar/aefing1
,上一个项目是localhost/kafli
。 - 如果 8 点正在查看
localhost/kafli/serhljodar/prof1
(ID #26),我应该看到下一个项目是localhost/kafli1/samhljodar
,上一个项目是localhost/kafli1/serhljodar/aefing2
我被告知要使用 array_slice,但到目前为止它并没有太大帮助。各位好心人能帮我解决这个问题吗?
编辑: 有人说数组本身就是问题所在。下面是这个数组填充的 sql:
INSERT INTO `posts` (`id`, `parent_id`, `category`, `position`, `post_slug`, `titill`) VALUES
(9, 0, 3, 0, 'islenska-stig1', 'Íslenska stig 1'),
(19, 9, 3, 0, 'kafli1', 'Kafli 1'),
(20, 9, 3, 0, 'kafli2', 'Kafli 2',),
(21, 19, 3, 0, 'serhljodar', '1.1 Serhljodar'),
(22, 19, 3, 0, 'samhljodar', '1.2. Samhljóðar'),
(23, 19, 3, 0, 'malnotkun', '1.3 Málnotkun'),
(24, 21, 3, 1, 'aefing1', 'Æfing 1'),
(26, 21, 3, 3, 'prof1', 'Próf 1');
如果数组是问题,我怎样才能成功地从这些帖子中创建一个分页数组,以便 parent_id 对齐本身在正确的 id 下,所以我可以得到:
Array
(
[0] => islenska-stig1
[1] => kafli1
[2] => serhljodar
[3] => aefing1
[4] => prof1
[5] => samhljodar
[6] => malnotkun
[6] => kafli2
)