#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    PHP with RSS/RDF - link as a variable


    Hi!
    I'm trying to create an RSS-feed with php, using information from a database. Basically I load link,title description,creator and feeddate from the database as $link, $title, $description etc.. The $link is Primary Key in the database and contains a link to a pdf or doc.
    This is together creating a bunch of news articles, which I want to create an RSS-feed with.

    I want to use RDF to do this and my problem is that I'm trying to use the $link variable, and I can't get it to work.
    I call a function with: news($link)
    and the function looks like this:


    function news(<?php $newslink ?>){
    <channel>
    <items>
    <rdf:Seq>
    <rdf:li rdf:resource="<?php $newslink ?>"/>
    </rdf:Seq>
    </items>
    </channel>

    <item rdf:about="<?php $newslink ?>">
    <title><?php $title ?></title>
    <link><?php $newslink ?></link>
    <description><?php $description ?></description>
    <dc:creator><?php $creator ?></dc:creator>
    <dc:date><?php $feeddate ?></dc:date>
    </item>
    }


    My problem is the links (I think) and getting the $link-variable to work properly. As for now, it doesn't work at all and I can not understand why. The reason I'm using a function for this is because I want to do the same thing for all of the news. I'm pretty new to this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    You need to include the whole function as part of the PHP code to call it from PHP elsewhere.
    PHP Code:
    <?php
    function news($newslink){
    echo 
    "
    <channel>
    <items>
    <rdf:Seq>
    <rdf:li rdf:resource=\"
    $newslink\"/>
    </rdf:Seq>
    </items>
    </channel>

    <item rdf:about=\"
    $newslink\">
    <title>
    $title</title>
    <link>
    $newslink</link>
    <description>
    $description</description>
    <dc:creator>
    $creator</dc:creator>
    <dc:date>
    $feeddate</dc:date>
    </item>"
    ;
    }
    ?>
    Note the use of [ PHP ] tags for the code. See ManiacDan's New User Guide.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    The PHP tags I already had, just forgot to mention that.
    But the rest helped me alot, thanks!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Problem with rdf:li adding links


    I now have a new problem (probably the last one before this is working as I want it to). I have changed the way I handle the links from the database a little. The links is stored in the variable $link which passes this on to an array $links[]. In this array I store all of the links and from there I want to put each array element in to the rdf:li, as separate rdf:li elements.
    The problem is that I want to do this before I actually read it from the database.
    Here's my code:

    <channel>
    //some channel elements code that's not interesting//
    PHP Code:
    <?php
    echo "
    <items> 
        <rdf:Seq> 
            <rdf:li rdf:resource=\"
    $links[0]\"/>
            <rdf:li rdf:resource=\"
    $links[1]\"/>
            <rdf:li rdf:resource=\"
    $links[2]\"/>
            <rdf:li rdf:resource=\"
    $links[3]\"/>
        </rdf:Seq> 
    </items> 
    </channel>"
    ;
    ?>
    And a bit further down:

    $result = mysql_query($query)
    or die("Query failed");

    $i = 0;

    PHP Code:
    <?php
    while ($line mysql_fetch_object($result)) {
            
    $link $line->link
            
    $link1 ereg_replace (' ''%20'$link);
            
    $title $line->title;
            
    $description $line->description;
            
    $description1 ereg_replace ('&''&amp;'$description);
            
    $creator $line->creator;
            
    $feeddate $line->feeddate;
            
    $feeddate1 strftime(strtotime($feeddate));
             
            
    $links[$i] = $link1;
            
    $i++;
            
            echo 
    "
            <item rdf:about=\"
    $link1\"> 
                <title>
    $title</title> 
                <link>
    $link1</link> 
                <description>
    $description1</description> 
                <dc:creator>
    $creator</dc:creator> 
                <dc:date>
    $feeddate1</dc:date> 
            </item>"

        }
    ?>
    In this case I have 4 links that I'm reading from the database. These links I want to put in to the rdf:li elements at the top, and then I want the rest to happen. The problem is that I don't know how to put it above "the rest", since the array is empty before I've read it from the database. I get a feeling that this is something simple I have missed, but I can't find any help by searching.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    I don't understand. Why not put the list after the query or the query before the list?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by gw1500se
    I don't understand. Why not put the list after the query or the query before the list?
    The reason I want to do it in that order is because I want it to look exactly like an xml-file I already have (in the structure), but still I want to generate these links, instead of just writing them in the code. My aim is to achieve this special order of the elements, where I need the list to be at the top, and then define each of the links with their attributes such as description and title.

    The result doesn't look good if I define the list after the separate links have been defined with their attributes. Hope you understand.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    My output structure that I'm talking about is the source code of the RSS-feed that's created with this PHP-file. This output structure is supposed to look like the xml-file I'm aiming for.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    Sorry for being dense but I still don't see how where you put the database query has anything to do with what the output looks like. The client does not know anything about the query, only the output result.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Okay, maybe my explanation was not spot on. When I look at the source code of the RSS-feed I can see the elements channel, items, item, list, and so on. Of course I can't see the php-code in the source code. The things that I "echo" in my code though, exists in the source code. Everything works just fine, except that the rdf:li-elements misses the links, since the array is empty when I "echo" this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    Now I'm really confused. Are you writing the PHP code and query or are you trying to screen scrape the data from some other site?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by gw1500se
    Now I'm really confused. Are you writing the PHP code and query or are you trying to screen scrape the data from some other site?
    I am writing the PHP code and query. From earlier I have an xml-file with the same info that is in my database, but in hardcore code. In this xml-file all info is coded, nothing is taken from my database. Now I want to do the same thing but read the info from my database instead of coding everything, to learn and see if I can get the same results with PHP and query, as I got with xml. All files are my own.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    I guess I am missing something as this brings me back to my original suggestion. If you are building the page, then what prevents you from moving the query so that it populates the link array before you use it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by gw1500se
    I guess I am missing something as this brings me back to my original suggestion. If you are building the page, then what prevents you from moving the query so that it populates the link array before you use it.
    Maybe I just misunderstood what you said at first, but now I think I got what you mean. I solved it just now by doing a special query at the top handling only the links. Very basic. Sometimes I just have trouble with the "thinking as a programmer" part, heh. Thanks alot for your help!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,459
    Rep Power
    594
    I hope you are not doing a double query. Everything you get from the database can be done with a single query, put into an array (or perhaps better yet into an array of objects - learn some OOP here) and then used elsewhere as needed.

    One more note. You should not be using the depreciated MySQL extensions. Instead switch to PDO (another OOP learning opportunity).
    Last edited by gw1500se; November 15th, 2012 at 09:27 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo