I want to make a function that will show a different random rss feed every time the page loads. I was trying to make an array that has the titles of the RSS feed, and the urls of each feed, and the function would make the array show the different title and url values.

    Sounds like a workable plan. You could use [man]shuffle/man to randomize the array, then [man]array_shift/man or [man]array_pop/man to pick the first or last element off of the array after you have shuffled it.

      Thats sounds good, I didn't know that was even possible,
      I still am unsure how I should go about writing the function or array in the first place. This is what I was trying to work out:

      I wanted to be able to echo the Title value and url value in two places in a javascript that I was going to echo out.

      function rssFeeds() {
      
      $rssfeed = rand(1,5);
      
      $feeds = array(
      	"Web Design from Scratch", "http://webdesignfromscratch.com/rss.xml",
      	"Best Tech Videos", "http://www.bestechvideos.com/user/jaredwilli/favorites/rss.xml",
      	"Title", "url",
      	"Title", "url",
      	"Title", "url",	
      	"Title", "url",
      );
      
      $results[0] = $feed[$rssfeed];
      $results[1] = $feed[$rssfeed + 5];
      
      return $results;	
      
      }
      
      $rssurl = rssFeeds();
      
      

      I know this doesn't work as is, nor is it the greatest way to try and do it Im sure. I just cant figure out what I need to do exactly, but would like to be able to work in the shuffle() and other ideas you mentioned too.

      Im not too good with functions or arrays so its not easy for me to just create something that works and isn't the most rediculous way to do it.

        When you make your array, you either want an array of arrays...

        $feeds = array(
           array(
              'title' => 'This is a title',
              'url' => 'http://example.com/feed.rss'
           ),
           array(
              'title' => 'This is another title',
              'url' => 'http://another.example.com/feed.rss'
           )
        );
        

        ...or you could use one one element as the key and one as the value...

        $feeds = array(
           'This is a title' => 'http://example.com/feed.rss',
           'This is another title' => 'http://another.example.com/feed.rss'
        );
        

        The first takes a little more typing initially, but I think it is a bit simpler to work with, as you could then just do:

        shuffle($feeds);
        $thisFeed = array_shift($feeds); // note that this removes the first element from the array
        echo "<h3>" . $thisFeed['title'] . "</h3>\n";
        // now use $thisFeed['url'] to acquire the RSS data and process it
        

        If you define the array using the second method, then you could use [man]each/man instead of array_shift():

        shuffle($feeds);
        list($title, $url) = each($feeds);
        echo "<h3>$title</h3>\n";
        // now use $url to acquire the RSS data and process it
        

          How do I put the variables $thisFeed['title'] and $thisFeed['url'] in a javascript code in place of where I would have the title and url for a single feed, heres what I am trying to do exactly, but what I have here doesn't work. I don't know how to embed the php in javascript or vice versa.

          // rss feeds
          $feeds = array(
             'This is a title' => 'http://webdesignfromscratch.com/rss.xml',
             'This is another title' => 'http://www.bestechvideos.com/user/jaredwilli/favorites/rss.xml',
             'Web Design from Scratch' => 'http://webdesignfromscratch.com/rss.xml',
             'Best Tech Videos' => 'http://www.bestechvideos.com/user/jaredwilli/favorites/rss.xml',
          );
          
          shuffle ($feeds);
          list ($title, $url) = each ($feeds);
          
          echo '<div style="padding-left: 10px;" id="feeddiv"></div>
          
          <script type="text/javascript">
          
          var feedcontainer=document.getElementById("feeddiv")
          var rssoutput="<h2>' . $thisFeed['title'] . '</h2><ul>"
          var feedurl="' . $thisFeed['title'] . '" 
          var feedlimit=10
          
          function rssfeedsetup(){
          var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method
          feedpointer.setNumEntries(feedlimit) //Google Feed API method
          feedpointer.load(displayfeed) //Google Feed API method
          }
          
          function displayfeed(result){
          if (!result.error){
          var thefeeds=result.feed.entries
          for (var i=0; i<thefeeds.length; i++)
          rssoutput+="<li><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + "</a></li>"
          rssoutput+="</ul><br /><hr /><br />"
          feedcontainer.innerHTML=rssoutput
          }
          else
          alert("Error fetching feeds!")
          }
          
          window.onload=function(){
          rssfeedsetup()
          }
          </script>
          ';

            Okay I got it to display the feed info ( the url part) but the Title part only shows a zero no matter what feed is being shown...

              You may have copied my code for the last part of my previous post before I edited it. If you're using that method, make sure the variable names used in the list($title, $url) expression are the variables you use in the actual output statements. (Originally I had copied-and-pasted the array variable names from the previous example and forgot to change them.)

                Ya, no I realized what I had done, and I fixed it,... and now the different RSS feeds display ok, but for the title of each feed it just shows a zero (0), not the title as I have it in the array. Heres what I have:

                // rss feeds
                $feeds = array(
                   'This is a title' => 'http://webdesignfromscratch.com/rss.xml',
                   'This is another title' => 'http://www.bestechvideos.com/user/jaredwilli/favorites/rss.xml',
                   'Web Design from Scratch' => 'http://webdesignfromscratch.com/rss.xml',
                   'Best Tech Videos' => 'http://www.bestechvideos.com/user/jaredwilli/favorites/rss.xml',
                );
                
                //foreach ($feeds as $rsstitle => $rssurl) {
                
                shuffle ($feeds);
                list ($title, $url) = each ($feeds);
                
                $title = "<h2>$title</h2><ul>";
                
                echo '<div style="padding-left: 20px;" id="feeddiv"></div>
                
                <script type="text/javascript">
                
                var feedcontainer=document.getElementById("feeddiv")
                var rssoutput="' . $title . '"
                var feedurl="' . $url . '" 
                var feedlimit=10
                ';
                
                ?>
                

                The rest of the javascript code comes after I close the php block here, and it seems to work ok doing it like that, aside from the title not displaying correctly. Im pretty sure the rest of the jscript isnt affected by having the first half of the script echoed with the php, so I can only think that something is not right with how the

                shuffle ($feeds);
                list ($title, $url) = each ($feeds);

                is written. What would be the difference in using a foreach(), something like,
                foreach($feeds as blah blah something) {

                is that not necessary here with the each(), I assume that does a similiar task as foreach()...

                  Ah, yes, each() returns the numeric index first. Try:

                  list($ignore_me, $url, $title) = each($feeds);
                  

                    I added the $ignore_me var like you suggested, but the title still doesn't show up. With the $ignore_me it doesn't display anything where the title should be, where as before with no $ignore var, a zero would show where the title should show.

                    I tried writing it using the other method you mentioned, and I could only get it to give me the same results. I tried a few different other things too, like using the foreach() and, which I had added another part to the array, so I had a "$title of feed" a "$link to site with the feed" and "$feedurl", and I managed to get all of it to output the html code I wanted, I used the "$link" for making the "$title" a link back to the site with the feed, and the source code outputted worked as I wanted it to, but since I ouputted it within a javascript code, the href=" quotes were screwing up the javascript then. I couldn't figure out how to make it output into the jscript so that it wouldn't screw that code up.

                    I am using Google's RSS ApI to show the feeds here, but I want to be able to make the page show a different feed every time it reloads, or make it possible to display more than one feed by using the google api key I have.

                    If you know of any hacks or mods to the google rss api, or where I go to maybe get some idea about how to alter it to do anything at all really, that would be great. Its been too long of a 2 day night for me now... lol. I don't know how much more of this nonsense I can take. Its one of the simpler things I have tried to do, but it's not happening, least not right now.

                    I would like to be able to use the Google
                    dynamic feed contol
                    thing which have altered before, and it came out well, but the generate code button doesn't work for me on that page.

                    Oh well...

                    Thank you for you help BTW.. I appreciate it.

                      Write a Reply...