1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Urgent Help

Discussion in 'PHP' started by angilina, Mar 3, 2008.

  1. #1
    Please tell me what this expression means

    $rss->items

    I actually dont know what is the use of this "->"

    Need help
     
    angilina, Mar 3, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    It is a reference to a method or property of an object.

    In this case it is most likely a reference to the xml node items. You would probably see this when using the simpleXML library or another xml parser.
     
    jestep, Mar 3, 2008 IP
  3. angilina

    angilina Notable Member

    Messages:
    7,824
    Likes Received:
    186
    Best Answers:
    0
    Trophy Points:
    260
    #3
    Thank You for the quick reply.

    Can you please tell me one more thing.

    In case I have this loop

    foreach ($rss->items as $item ) {
    }

    Show can I replace this loop?

    I mean what can replace "$rss->items as $item"?

    What I want to do is that I want to limit the rss shown, the above loop show all the rss feeds where I want to limit it to 10.

    So, can I replace this loop?
     
    angilina, Mar 3, 2008 IP
  4. michael.aulia

    michael.aulia Active Member

    Messages:
    736
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #4
    That line is saying, for every item in RSS (you can access it as $item which will change on every loop)...

    To limit it, easy.
    Add a variable before the loop (a counter), say $counter = 0;
    then the loop for each(.......) {
    // You do something with the $item, such as print it out, etc

    // Then add the counter
    $counter++;

    // Check if already more than 10
    if($counter > 10) break;
    }

    or google on how to do a FOR loop :)
     
    michael.aulia, Mar 3, 2008 IP
    angilina likes this.
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    There are probably 50 ways to do this, (for, while, foreach). Here's quick way to limit to 10.

    
    
    $i = 1;
    foreach ($rss->items as $item ) {
    if($i <= 10){
    
    //do stuff here
    }
    $i++;
    }
    
    
    PHP:
     
    jestep, Mar 3, 2008 IP
  6. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You may be able to use a for loop instead. Something like for($i=0; $i<=10; $i++) then iterate through the loop that way. But I'd have to see the entire loop. Could you post it?
     
    Altari, Mar 3, 2008 IP
  7. angilina

    angilina Notable Member

    Messages:
    7,824
    Likes Received:
    186
    Best Answers:
    0
    Trophy Points:
    260
    #7
    Actually doing what you said or "michael.aulia michael.aulia" is a good idea but the problem is that if rss feeds are 100, then this loop will still run for 100 times, where I want to limit the loop from running 100 times + I want to show 10 feeds only

    Anyway to replace the loop?
     
    angilina, Mar 3, 2008 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    Can you show an example of the XML. A for or do while loop is probably going to be the best way to do it.
     
    jestep, Mar 3, 2008 IP
  9. angilina

    angilina Notable Member

    Messages:
    7,824
    Likes Received:
    186
    Best Answers:
    0
    Trophy Points:
    260
    #9
    Thanks, on first look I didnt see this part in your post

    "break; "

    This will do the trick, Thanks for the help.

    Anyway, I still wonder is we can replace the foreach loop:confused:
     
    angilina, Mar 3, 2008 IP
  10. angilina

    angilina Notable Member

    Messages:
    7,824
    Likes Received:
    186
    Best Answers:
    0
    Trophy Points:
    260
    #10
    Can you tell me how bad the below code is, in terms of page opening time, and SEO

    if there is a while loop that runs for 62 times and this while loop is in IF loop which will run for 30 times.

    so while loop will run for 60 * 30 times.
     
    angilina, Mar 3, 2008 IP