Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts

    PHP & ajax (foreach loop)

    Say I have a rss reader like this:

    PHP Code:
    <?php
    $rss 
    simplexml_load_file('http://myfeedurl.com/feeds');
    ?>
    <h1><?php echo $rss->title?></h1>
    <ul>
    <?php
    foreach($rss->entry as $e) {
        echo 
    "<li><a href=\"".$e->link['href']."\">";
        echo 
    $e->title;     
        echo 
    "</a></li>\n";
    }    
    ?>
    </ul>
    I now want to change the hardcoded url into a custom one a user can submit via form. So the form to add the link should be on top, and below the content of the rss feed should parse once submitted.

    What is the best approach for that?
    I have issues solving this with ajax because of the foreach loop, as the script doesn't return the data one by one but fully parsed. I don't know if there is an easy approach to solve this. Only thing I can think of is returning the parsed rss feed result as an array to javascript in which I then parse the array content with another loop?

    Another (non ajax) idea I could think of is to have two seperate pages, one for submitting the url, the other for parsing the content while using a $SESSION variable to save the url name. But that's not the best either =/

    Anyways any input on how to best solve this issue is appreciated, thx in advance =)

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,287
    Thanks
    23
    Thanked 611 Times in 610 Posts
    You need to use html to collect the users url in a form and submit it to this php file. Change the url that is there now for the one received from the form submit.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I want to submit to the very same page though, that is the issue for me

    On the top of the page I have my text form with the submit button.

    Below I want to parse the rss feeds on the fly.

    And if possible I'd like to prevent refreshing the page and go for ajax solution instead. And that's where I'm stuck as I dunno how to process the foreach loop

    Edit: I guess I will be going for ajax and just pass a finished array
    Last edited by coffeecup; 04-22-2013 at 01:24 PM.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,287
    Thanks
    23
    Thanked 611 Times in 610 Posts
    I have thought about this and think it's far more complicated than you think.
    Your trying to read rss feeds using "echo $rss->title;" What if it doesn't have a title or if the title is buried.
    echo "<li><a href=\"".$e->link['href'] Means that the element link has an attribute. What if it doesn't. What if it's <link>http://www.wikipedia.org/</link>?

    I looked up http://myfeedurl.com/feeds to see what you were reading for starters and that does not exist.

    There is no generic form for an rss feed so trying to read every single one submitted with a single method will just lead to failure.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    coffeecup (04-22-2013)

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts
    It's a valid concern but this is just a school project so I can be picky with used tags and go for rss 2.0 standards, which are supposed to have the tags title, link and description included inside a channel tag. So no worries about rss feeds which go with different tag names.

    http://myfeedurl.com/feeds is just a placeholder, currently I'm working with http://news.yahoo.com/rss/sports for testing purpose.


    my new php looks like this:
    PHP Code:
    <?php
    $rss 
    simplexml_load_file('http://news.yahoo.com/rss/sports'); 

    $rssarray=array();

    foreach(
    $rss->channel->item as $item) {
        
    array_push($rssarrayjson_encode($item->title));
    }    

    echo 
    json_encode($rssarray);  
    ?>
    I run the script via $.ajax post and receive the data the following way:
    {"0":"AP Source: J.R. Smith wins NBA sixth man"}
    {"0":"Jets trade Darrelle Revis to Buccaneers"}
    {"0":"Ginobili, Parker lead Spurs past Lakers in Game 1"}
    etc...

    not perfect yet as this isn't quite what I want as I only need the title and not the attributes... ^^

  • #6
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts
    "Sunfighter" has suggested the right way. Just collect the URLs from HTML form and pass those values within the code.


  •  

    Posting Permissions

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