Chapter 41. Making Your Own Dynamic Block


Making blocks, as you learned in the previous topic, is easy. Crazy-style easy. Making a dynamic block isn't much harder than making a simple HTML block, in fact. For example, you may already know that PHP-Nuke allows you to assign points to users who participate in your site by posting content, comments, and so forth; I wanted a block that would show the current top five point earners, to encourage other users to participate. Here's the block I wrote:

 <?php if (eregi("block-Top10_Downloads.php",$_SERVER['PHP_SELF'])) {  Header("Location: index.php");  die(); } global $prefix, $db; $content = "Currently in the lead:<br><br>"; $a = 1; $sql = "SELECT username FROM ".$prefix."_users WHERE "  ."user_id > 2 AND user_id <> 150 ORDER BY points "  ."DESC LIMIT 0,5"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) {  $title2 = ereg_replace("_", " ", $row[username]);  $content .= "&nbsp;&nbsp;$a: $title2<br>";  $a++; } $content .= "<br>Prize awarded monthly to the top user! "  ."Earn points by participating in the "  ."<a class=\"blockcontent\" href=\"modules.php?name="  ."Forums\">Forums</a>, "; $content .= "<a class=\"blockcontent\" "  ."href=\"modules.php?name=Submit_News\">submitting scripts"  ."</a>, adding to your <a class=\"blockcontent\" "  ."href=\"modules.php?name=Journal\">Journal</a>, "; $content .= "visiting our sponsors, and more!"; $content .= " <a class=\"blockcontent\" "  ."href=\"modules.php?name=Your_Account\">Check "  ."your points</a> anytime."; ?> 

This is a pretty short section of code. The first few lines just check to make sure that the page isn't being accessed directly; if it is, the user is redirected to the site's home page. This is really just a precaution to help prevent users from individually accessing pages of the site, since not all pages are accessible to all users. By ensuring that this block is accessed only by another page on the site (usually modules.php, which loads only those blocks that the current user is allowed to see), I'm better protected against blocks being shown inappropriately.

 if (eregi("block-Top10_Downloads.php",$_SERVER['PHP_SELF'])) {  Header("Location: index.php");  die(); } 

Next, I define a couple of variables, and I start building the content for the block. Blocks don't ever use PHP's echo statement to output text; they always build a variable named $content. Other pages that load blocks expect $content to be populated with whatever the block wants to show. Here, I start by simply putting some static HTML text into the $content variable:

 global $prefix, $db; $content = "Currently in the lead:<br><br>"; 

Next comes the dynamic part of the block. I execute a SQL query against my site's database, querying the usernames in order of their point totals. I limit the query to five users, and I specifically exclude user IDs 1, 2, and 150, which are my user IDs on the site. No need for me to compete with my users!

Next, a while loop steps through each of the five users who are returned by the query and adds their names to the block's $content variable. If you're not at all familiar with PHP programming, this may look like gibberish; pick up a good book on PHP if you're interested in learning more about the language. From a PHP-Nuke standpoint, the only special thing here is the $db variable. This is an object variable (fellow PHP geeks will know what that means), and it represents the database functionality built into PHP-Nuke. $db->sql_query executes a SQL query; $db->sql_fetchrow retrieves the next row from that query. The practical upshot of this code is to produce a numbered list of five users, in descending order of points total.

 $a = 1; $sql = "SELECT username FROM ".$prefix."_users WHERE "  ."user_id > 2 AND user_id <> 150 ORDER BY points "  ."DESC LIMIT 0,5"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) {  $title2 = ereg_replace("_", " ", $row[username]);  $content .= "&nbsp;&nbsp;$a: $title2<br>";  $a++; } 

The rest of the block is fairly boring, simply adding more static content to the $content variable:

 $content .= "<br>Prize awarded monthly to the top user! "  ."Earn points by participating in the "  ."<a class=\"blockcontent\" href=\"modules.php?name="  ."Forums\">Forums</a>, "; $content .= "<a class=\"blockcontent\" "  ."href=\"modules.php?name=Submit_News\">submitting scripts"  ."</a>, adding to your <a class=\"blockcontent\" "  ."href=\"modules.php?name=Journal\">Journal</a>, "; $content .= "visiting our sponsors, and more!"; $content .= " <a class=\"blockcontent\" "  ."href=\"modules.php?name=Your_Account\">Check "  ."your points</a> anytime."; 

That finishes the block. You'll notice that the block doesn't attempt to do anything with the $content variable; it's sufficient simply to stuff it full of whatever you want shown, and PHP-Nuke takes care of the rest.

Blocks are among the simplest components of PHP-Nuke. Installing a block is simplicity itself: Copy it into the site's Blocks folder, and you're done. You then can select the block in the Blocks administration module, adding the block to your site.

So, here's a quick rundown of how to build a dynamic block:

  1. Start with an empty PHP page.

  2. Add code that populates a variable named $content with whatever you want shown.

  3. Save the file in the site's Blocks folder.

  4. Add the block to the site.

Obviously, you need to know some PHP programming to produce dynamic blocks, and this book isn't here to help you become a PHP programmer. There are tons of other books that can help you do that; browse through them online or at your favorite bookstore, and find one you like. PHP-Nuke provides that special $db variable that you can use to access database functionality, which is about the only special thing a block usually needs to do.



    PHP-Nuke Garage
    PHP-Nuke Garage
    ISBN: 0131855166
    EAN: 2147483647
    Year: 2006
    Pages: 235
    Authors: Don Jones

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net