Hello there, I want the following: There's an <UL>, I want 5 <LI>'s, after those 5, it should close the </UL> and continue another one. I just can't get it working. So frustrating. First row: Next row: I get the data from an XML file which I loaded with the SimpleXMLElement function. The XML looks like this: So, I want 5 "items" per row. How can I do this? Sorry for my english. Thanks.
There are many ways you can achieve it. What should happen if there are less than five items though? process them anyway or ignore them? Something like the following should do - its untested, but you get the general idea.... # load your xml file first.... $xml = simplexml_load_string($xmlfile); $count = 0; $element = 0; foreach($xml->item as $item) { if($count == 0) print "<ul>"; print "<li>".$item->name."</li>"; $count++; $element++; if($count == 5 || ($count < 5 && $element == sizeof($xml->item))) { print "</ul>"; $count = 0; } } PHP:
** Hope it's ok to make a doublepost, otherwise I'll have to make another topic which is pretty useless. ** I have another little question. I can't get it to work. My xml has the following structure: Now, what I wanna do is make a loop for all services and getting the name of them. So profile id 100 has 3 services, I want it to display all 3 of them including their names ("Name of service 01", "Name of service 02", "Name of service 03") How can I do this? Thanks.
If this is your XML feed, you might want to look into changing the tree structure, then you can just iterate through each service easily; <service id="service01"> (details) </service> Code (markup): Otherwise, if you are stuck dealing with it, you are going to have to use something like the following (assuming the number can change); $xml = simplexml_load_string($tree); foreach($xml->all->profile as $p) { print "<div>".$p->id."</div>"; preg_match_all("#<service[0-9][0-9]>(.*)</service[0-9][0-9]#", $tree, $s); foreach($s[1] as $service) { print "<div>".$service."</div>"; } } PHP:
Hello Luke, Thank you for your reply. The thingy is, they don't really have those service names (service01, service02, etc.). Each of them are named differently. So the best way is changing the structure? How should I display the services so I can loop them and give them their own piece of HTML for example? Thanks
Yeah - if you can change the structure then do so, you are trying to collect like data together which is exactly what you have here (a list of services). If you use something like the following its just a simple for loop; so using your original XML; [..otherXML...] <information> <services> <service>Name of service 01 or details</service> <service>Name of service 02 or details</service> <service>Name of service 03 or details</service> </services> </information> [..otherXML...] Code (markup): Then, when you come to process it; $xml = simplexml_load_string($tree); foreach($xml->all->profile as $p) { print "<div>".$p->id."</div>"; foreach($p->information->services->service as $s) { print "<div>".$s."</div>"; } } PHP: