Help with looping?

Discussion in 'PHP' started by Kass77, Apr 16, 2009.

  1. #1
    I am using a master page (rss_master.php) with variables in it to make weekly changes to parts of several other pages.
    On the rss_master.php page some of the variables are named:
    $newitems1
    $newitems2
    $newitems3.... and so on. The number of newitems will vary from week to week.

    On the multiple pages that pull information from the rss_master.php page, there are references to the newitems like this:
    $rss_writer_object->additem($newitems1);
    $rss_writer_object->additem($newitems2);
    $rss_writer_object->additem($newitems3);

    My problem:
    I'd like to make some sort of reference or loop or something that no matter how many $newitems1, newitems2, newitems3, newitems4, etc. that I have listed in the rss_master.php page, the multiple feed pages will pick up all the $newitems_ without direct referencing each line "$rss_writer_object->additem($newitems1);" like I have above.

    Any help would be appreciated.
     
    Kass77, Apr 16, 2009 IP
  2. guruwebmaster

    guruwebmaster Active Member

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    Well,

    you can do this way on the rss_master.php

    $newitems[$week] = "value"; better that a lot of variables

    and use

    foreach ($newitems as $item)
    {
    $rss_writer_object->additem($item);
    }

    had you understand?
     
    guruwebmaster, Apr 16, 2009 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    can you remodify your variables and put it into array? i mean array_push each variable so you dont have that much problem in referencing.
     
    bartolay13, Apr 16, 2009 IP
  4. Kass77

    Kass77 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh, thank you! I was pretty sure I needed a loop, and had tried the foreach loop, but didn't have the clause set correctly.

    I ended up with the following:

    I put this in the rss_master.php

    $newitemsall= array($newitems1,$newitems2,$newitems3,$newitems4)
    so I can just add new array items as needed in my master.

    In the multiple pages I put the loop as suggested, with minor adjustments:
    foreach ($newitemsall as $newitem)
    {
    $rss_writer_object->additem($newitem);
    }

    I'm relatively new to PHP and didn't have the value set correctly before. I think I'm getting this now!

    Thanks so much for the help! :p

    Maybe someday I'll be skilled enough to help someone else!
     
    Kass77, Apr 16, 2009 IP
  5. guruwebmaster

    guruwebmaster Active Member

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    That´s the spirit.
     
    guruwebmaster, Apr 17, 2009 IP
  6. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    No doubt about it! You just have to be persistent and keep programming! If you stop for a period of time, you'll become rusty, so you must continue to program. At least a different project every couple of days (depending on complexity) to keep your skills sharp and learn new things! ;)

    Good luck! Don't be shy if you have any more questions, just create some posts!

    Regards,
    Dennis M.
     
    Dennis M., Apr 17, 2009 IP