1

Bazaar – php example code – part 13 – RSS syndication script

Article focus on a way how to generate RSS XML code for syndication news about currently published articles. User can subscribe for obtaining news about currently posted articles into a RSS readers.

RSS short introduction

RSS (RDF Site Summary or Really Simple Syndication)[2] is a web feed[3] that allows users and applications to access updates to websites in a standardized, computer-readable format.  (wikipedia, 26.12.2020)

By creating RSS feed, we enable to other people gain info about all of our new post. Our goal is to populate newsfeed from the bazaar_item database table to inform about new items available for sell.

To view an RSS feed, users need a RSS newsreader. Some of them are integrated in most of today browsers.

Closer look at RSS

RSS uses as a HTML markup language that uses tags and attributes for content description. Base of RSS is then XML used to describe web content for syndication.

Next picture show output of generated RSS code for advertising new items on our Bazaar app.

Required tags for the channel

  • title. The title of the channel. Should contains the name.
  • link. URL of the website that provides this channel.
  • description. Summary of what the provider is.
  • one item tag at least, for the content.
1
2
3
4
5
6
7
8
9
10
<rss version="2.0">
<channel>
    <title>XUL</title>
    <link>https://www.link.domain</link>
    <description></description>
    <item>
    ...
    </item>
</channel>
</rss>

Script rss.php for RSS markup generation

Next code provide all functionality for obtaining data about items for sell and generate RSS XML syndication feed for subscribed users.

<?php header(‚Content-Type: text/xml‘); ?>
<?php echo ‚<?xml version=“1.0″ encoding=“utf-8″?>‚; ?>
<rss version=“2.0″>
<channel>
<title>Bazaar – items for sell feed </title>
<link>http://localhost/bazaar/ </link>
<description>Latest items for sell on example Bazaar app created for educational purposes. </description>
<language>en-gb</language>
<?php // main part read info about items for sell and generate feed posts
session_start(); // start the session – must be added on all pages for session variable accessing
// solution using SESSIONS with COOKIES for longer (30days) login persistency
if(!isset($_SESSION[‚users_id‘])) { // if session is no more active
    if(isset($_COOKIE[‚users_id‘]) && isset($_COOKIE[‚username‘])) { // but cookie is set then renew session variables along them
        $_SESSION[‚users_id‘] = $_COOKIE[‚users_id‘];
        $_SESSION[‚username‘] = $_COOKIE[‚username‘];
        $_SESSION[‚user_role‘] = $_COOKIE[‚user_role‘]; // added for role
    }
 }
 /**
  *  Main part of generator newsfeed
  */
  // connect to the database
  require_once(‚appvars.php‘); // including variables for database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
    // Check connection
        if($dbc === false){
            die(„ERROR: Could not connect to database. “ . mysqli_connect_error());
        }
  // Obtain all listed items for sell
  $sql = „SELECT * FROM bazaar_item“  ;
  //go through the array of items for sell and format it as RSS
  
            if($output = mysqli_query($dbc, $sql)){
                if(mysqli_num_rows($output) > 0){  // if any record obtained from SELECT query
                    
                    while($row = mysqli_fetch_array($output)){ //next rows outputed in while loop
                            //$first_name_buyer = $row[‚first_name‘];
                        echo  ‚<item>‚; 
                        echo  ‚<title>‘.$row[‚name_of_item‘].‘</title>‚; 
                        echo  ‚<link> http://localhost/bazaar/item.php?item_id=‚.$row[‚item_id‚].‘</link>‚;
                        echo  ‚<pubDate>‚.$row[‚item_add_date‘].‘ ‚.date(‚T‘).‘</pubDate>‚; 
                        echo  ‚<description>‚.substr($row[‚item_description‘], 0, 64).‘</description>‚; 
                        
                        
                        echo  ‚</item>‚; 
                                                                            
                    }
                    
                    // Free result set
                    mysqli_free_result($output);
                } else {
                    echo „No info about buyer obtained.“; // if no records in table
                    $cart_was_submitted = false; // items cann not be bought by technical issue
                }
            } else {
                echo „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc); // if database query problem
                $cart_was_submitted = false; // items cann not be bought by technical issue
            };
?>
</channel>
</rss>

Integration RSS generator in to a page

Our RSS generator is available for syndication from bottom part (footer) of the pahe via link with syndication image as it depicts next screen.

RSS subscription available from page footer

Full code of footer.php scrict included in our pages is:

<!– ***************************************************************** –>
<!–                              PHP footer of bazaar  for including                                 –>
<!– ***************************************************************** –>
<!– Vrsion: 1.0        Date: 22. – 22.11.2020 by CDesigner.eu                              –>
<!– ***************************************************************** –>
<?php
 // for further rework of the code
 function generate_footer($width) {
  if($width==580) {
    echo ‚<div class=“footer“ >‘;     
    echo ‚<div class=“footer“ id=“footer_container_580″>‘; 
    echo ‚<a class=“navbar-brand“ href=“https://cdesigner.eu“> Visit us on CDesigner.eu </a>‘; 
    echo ‚<a class=“navbar-brand“ href=“rss.php„> Subscribe to newsfeed <img src=“./images/rss.png“ width=“25″> </a>‚; 
    echo ‚</div>‘; 
    echo ‚</div>‘;    
  }
  if($width==1060) {
    echo ‚<div class=“footer“ >‘;     
    echo ‚<div class=“footer“ id=“footer_container_1060″>‘; 
    echo ‚<a class=“navbar-brand“ href=“https://cdesigner.eu“> Visit us on CDesigner.eu </a>‘; 
    echo ‚<a class=“navbar-brand“ href=“rss.php“> Subscribe to newsfeed <img src=“./images/rss.png“ width=“25″> </a>‘; 
    echo ‚</div>‘; 
    echo ‚</div>‘;    
  }
      }
?>
<!– older solution without daptive width
    <div class=“footer“ >     
        <div class=“footer“ id=“footer_container_1060″> 
            <a class=“navbar-brand“ href=“https://cdesigner.eu“> Visit us on CDesigner.eu </a>
            <a class=“navbar-brand“ href=“rss.php“> Subscribe to newsfeed <img src=“./images/rss.png“ width=“25″> </a>
            </div>
    </div>
–>

In our pages is this footer script invoked way as can be visible on next code snippet. After including with requireonce() is invoked function for generating footer with expected with.

… code omitted from index.php …

   
        <?php  // footer include code
            require_once(‚footer.php‘); // including footer
            generate_footer(1060); // function from footer.php for seting width, you can use 580 and 1060px width
        ?>         
      
      
</body>
</html>

Conclusion

With RSS syndication is opened new way for our users to subscribe for getting all new info about products for sell. Short example provide shor introduction to RSS generation and how to implement RSS generator into a code of our page.

Full Bazaar app code for further reference can be obtained from github here.