Counting in a loop.

Discussion in 'PHP' started by Miquexia, Jun 11, 2010.

  1. #1
    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.
     
    Miquexia, Jun 11, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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:
     
    lukeg32, Jun 11, 2010 IP
  3. Miquexia

    Miquexia Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Amazing. Thanks alot!
     
    Miquexia, Jun 11, 2010 IP
  4. Miquexia

    Miquexia Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ** 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.
     
    Miquexia, Jun 21, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Sorry, just seen the structure - will they ALWAYS have 3 services?
     
    Last edited: Jun 21, 2010
    lukeg32, Jun 21, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    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:
     
    lukeg32, Jun 21, 2010 IP
  7. Miquexia

    Miquexia Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    Miquexia, Jun 21, 2010 IP
  8. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #8
    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:
     
    lukeg32, Jun 21, 2010 IP
  9. Miquexia

    Miquexia Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ahhh ofcourse!

    Thanks again!

    You're really helpful.
     
    Miquexia, Jun 21, 2010 IP