Aug
21
2007

How Websites Show Portions Of Content For Preview Subscribers

Share
Email

In my recent post: Google AdSense Now Logs Into Your Sites I mentioned that displaying a quick preview of your content might be useful in some scenarios. Particularly if you want to charge subscription fees for full content. I’ll illustrated with an example so you can get an idea of where to start in accomplishing this with your own sites. The key component in making this work is the substr($content,0,300) function. You specify the length of content by declaring a start (0) and the end (300). You can then output a shortened version of $content and store it in a separate variable. Great to use as a “preview” for your content. Note: the number refers to the numbers of characters in the provided string, not words.

What you need to do is figure out the existing variables you’re outputting on each content page (likely found within your template). This part goes beyond the scope of this article. All sites are set up with slightly differently variables and even grab content from different sources…this is not an exact science.

I will assume your data is coming from a mySQL database and $id is a number that refers to a row of content in your database. This first snippet shows a row being queried in preparation for it to be cut down into a preview.

  1. $result = mysql_query("SELECT id,content FROM table WHERE id={$id}",$db);
  2. $row = mysql_fetch_row($result);
  3.  
  4. echo $row[0]; //The value of id
  5. echo $row[1]; //The value of content
  6. $content = $row[1]; //Store it into something we can identify easily

This is even easier when you already have your content in a string. In that case you wouldn’t have to run a query on it again. There are also applications in which you would want to have this preview show before you content. In that case you would only need to integrate something like this into your templates (and yes there is more then one way to do this):

  1. $previewcontent = substr($content,0,300);
  2.  
  3. if(isset($go)) { //If set then they have seen the preview already
  4. echo $content; //Display your usual content
  5. } else { //First time viewing this page
  6. echo "Article preview:<br><br>" . $previewcontent;
  7. echo "See this entire post<a href=\"showcontent.php?id=" . $id . "&go=yes\">here</a>";
  8.  
  9. //Or you could add something like this (instead of lines 6 & 7) for paid/subscription content
  10.  
  11. if(isset($user)) { //If a user is logged in...
  12. echo "Go ahead and <a href=\"showcontent.php?id=" . $id . "&go=yes\">view</a>";
  13. } else { //$user is not set - insert login/signup link/form below
  14. echo "To view this solution please click <a href=\"yoursignupscript.php\">here</a> to purchase access.";
  15. }

In my alternate configuration where visitors need to purchase access you should also change your first if statement to check if a user is logged in by doing:
if(isset($go) && isset($user))

There’s some security and usability you need to consider when implementing this which I didn’t get into. As shown above isset() is another very handy function in php as it allows you to set triggers in conjunction with if statements. This can make a webpage very dynamic.

If you’re not that far yet with a coding language, I hope this at least gives you some ideas of what you can do. For additional help check out php.net: http://us3.php.net/substr. Just getting started with php/mySQL?: Increase Earning Potential With These PHP/mySQL Tutorials

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.


Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois