SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternative to foreach loop

    I hope I can explain my problem well enough! I would normally use a foreach loop but I need an alternative as each entry will be displayed on a separate web page. To add to my confusion it is not an array but a DOMNodeList that I am working with. What I would have if I were not displaying each entry on a separate page is:
    PHP Code:
    $items $doc->getElementsByTagName('item');
    foreach ( 
    $items as $item ) {
      
    $questions $item->getElementsByTagName('question');
      ...

    So my query is what do I use in place of
    PHP Code:
    foreach ( $items as $item ) { 
    Thanks G
    blessed are the cracked for they let in the light

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,028
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Is it like a list of items sold and each link leads to that item's info page?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, actually each item contains a question. After answering each question the user goes to the next one.
    blessed are the cracked for they let in the light

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,028
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Will the user be answering the same questions in the same order or will the questions they get asked depend on what their answers were to earlier questions?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In sequential order, just like would happen with foreach.
    blessed are the cracked for they let in the light

  6. #6
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,265
    Mentioned
    115 Post(s)
    Tagged
    1 Thread(s)
    While I'm not sure I understand why you're accessing the DOM on the server side, if you know where you are in the array traversal (and you're going to have to keep track of it somehow), you can just access the array directly.

    In other words, use $items[x] instead of the foreach loop. Or a range of numbers if you're looking for more than one...
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried
    PHP Code:
    $item $items[0]; 
    but got an error:
    Fatal error: Cannot use object of type DOMNodeList as array in ...\page1.php on line 16
    blessed are the cracked for they let in the light

  8. #8
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,265
    Mentioned
    115 Post(s)
    Tagged
    1 Thread(s)
    Try $item = $items->item(0);

    Source: http://www.php.net/manual/en/domnodelist.item.php
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  9. #9
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks!
    blessed are the cracked for they let in the light

  10. #10
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dave, I didn't understand your comment about accessing the DOM on the server side at the time. My script is ready an XML file and accessing its DOM...
    blessed are the cracked for they let in the light

  11. #11
    SitePoint Member
    Join Date
    May 2012
    Posts
    6
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The DOMNodeList object implements the Traversable interface and can be used in a foreach loop like a plain array.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •