我一直在努力想出这个,但却无法绕过它。
我正在使用这些xml对象:
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13463
[name] => A Athletic Wear
[path] => A Athletic Wear
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13475
[name] => A Accessories
[path] => A Accessories
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13482
[parent_id] => 13463
[name] => Crewneck Sweatshirts
[path] => A Athletic Wear -> Crewneck Sweatshirts
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/Crewneck_Sweatshirts/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13483
[parent_id] => 13475
[name] => Aprons
[path] => A Accessories -> Aprons
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/Aprons/80.gif
)
)
使用什么是正确的循环,以便最终的标记看起来像这样:
<ul>
<li>A Athletic Wear</li>
<ul>
<li>Crewneck Sweatshirt</li>
</ul>
<li>A Accessories</li>
<ul>
<li>Aprons</li>
</ul>
</ul>
这是我的功能:
function GetCatalogyList() {
global $url, $get_product_catalog;
$xml = simplexml_load_file($url . $get_product_catalog . "");
foreach ($xml as $items) {
$pcid = $items["product_category_id"];
$pid = $items["parent_id"];
$name = $items["name"];
// Logic to iterate through the xml so that product_category_id elements
// appears under the parent_id element using unordered list.
}
return $return;
}
如果这是SQL,我只是使用WERE子句并完成其余的工作。但这是我之前没有遇到的事情,需要帮助。感谢。
答案 0 :(得分:3)
您必须意识到的第一件事是您正在处理数组,而不是数据库。 SQL只是将结果集作为数组返回,然后您只需按照需要进行解析。
至于你的功能,它有几个问题:你tightly-couple数据加载,解析和渲染(似乎是这样),只是更难以阅读它的函数体。
为了解决这样的问题,您需要将任务分解为小任务。这是需要做的事情:
所以在你的情况下,这将是这样的:
$url = 'http://stores.inksoft.com/GetProductCategoryList/1210/';
$data = parse_data(simplexml_load_file($url));
echo render_as_dropdown($data);
功能,看起来像这样,
function parse_data($array) {
// We're going to keep relationships here
$data = array(
'items' => array(),
'parents' => array()
);
foreach ($array as $node) {
// First step - Make those XML attributes easier to read
$attributes = (array) $node->attributes();
$attributes = $attributes['@attributes'];
$data['items'][$attributes['product_category_id']] = $attributes;
// When we have no parents, let's make it null to avoid conflicts
if (!isset($attributes['parent_id'])) {
$attributes['parent_id'] = null;
}
$data['parents'][$attributes['parent_id']][] = $attributes['product_category_id'];
}
return $data;
}
function render_as_dropdown($data, $parentId = null) {
$html = '';
if (isset($data['parents'][$parentId])) {
$html = '<ul>';
foreach ($data['parents'][$parentId] as $itemId) {
$html .= '<li><a href="#">' . $data['items'][$itemId]['name'] . '</a>';
// find childitems recursively
$html .= render_as_dropdown($data, $itemId);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}