PHP - foreach loop as WHERE clause
I'm trying to figure this out but just can't wrap it around myself.
I have these xml objects that I am working with:
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
)
)
What would be the correct loop for the final markup to look like this:
<ul>
<li>A Athletic Wear</li>
<ul>
<li>Crewneck Sweatshirt</li>
</ul>
<li>A Accessories</li>
<ul>
<li>Aprons</li>
</ul>
</ul>
Here's my function:
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;
}
If it was SQL I would just use the WERE clause and everything else. But I had never met this before and needed help. Thank.
source to share
The very first thing you need to understand is that you are dealing with an array, not a database. SQL just returns the result array as an array, and then you just parse it the way you want.
As for you, you have several problems: you are tightly par> loading, parsing and rendering data (it seems like that), which just makes it harder to read the body of your function.
To solve such a problem, you need to break the problem down into small ones. Here's what to do:
- Loading data from an external resource (be it XML, JSON or whatetver)
- Data analysis
- Analysis of the analyzed data
So in your case it would look like this:
$url = 'http://stores.inksoft.com/GetProductCategoryList/1210/';
$data = parse_data(simplexml_load_file($url));
echo render_as_dropdown($data);
And the functions will look like this:
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 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;
}
source to share