【发布时间】:2016-03-09 12:30:19
【问题描述】:
我知道我可能问了一个愚蠢的问题,但我真的很渴望优秀的 PHP 开发人员,所以无论如何,关于我的问题:
我需要以下结构
Cat1 item1 item2
Cat2 item21 item22
等等……
我的 XML 结构如下:
<?xml version="1.0" encoding="utf-8"?>
<List>
<category name="cat1" dispName="First Category" catCode="FC1">
<item itmCode="item1">
<name>item 1</name>
<img>path to image 1</img>
</item>
<item itmCode="item2">
<name>item 2</name>
<img>path to image 2</img>
</item>
</category>
<category name="cat2" dispName="Second Category" catCode="SC2">
<item itmCode="item21">
<name>item 21</name>
<img>path to image 21</img>
</item>
<item itmCode="item22">
<name>item 22</name>
<img>path to image 22</img>
</item>
</category>
</List>
我的PHP代码如下:
<?php
$xml=simplexml_load_file('items.xml') or die('Error: Cannot create Grouped Items');
foreach($xml->category as $cat){
$currentCat=$cat['dispName'];
$catName=$cat['name'];
echo $catName.'<br/>';
echo $currentCat.'<br/>';
$itemsCount=3;
$random = array_rand($xml->xpath('category'), 3);
if(is_array($random) || is_object($random)){
foreach ($random as $key){
//here is the issue as am trying to get the child nodes of each of the category nodes from the above XML list
}
}else{echo '<br/>error<br/>';}
}
?>
写一个好的高性能列表页面的最佳方法是什么,我以后会有太多的类别,每个类别都有超过 30 个项目。
我非常感谢任何帮助和建议,因为我独自在这个项目(一个家庭项目)上工作,开发人员 - 设计师 :)
【问题讨论】:
-
请指定您想要完成的任务。您的代码似乎过于复杂,无法解决您在上面描述的问题。
-
我只需要列出每个类别中的 x 个项目,并将类别名称写在其项目的顶部。
-
欲了解更多信息,我有一个正在运行的页面,其中包含我需要查看的内容,但其行为不当目前 uistarter.com/css3icons/icons.php 目前正在拥有该类别,我想要在每只猫下方列出 3 个项目