GIDNetwork > Multipart Articles on GIDNetwork, Part 2
Register
« Multipart Articles on GIDNetwork Introducing GIDZipTest... again »

Multipart Articles on GIDNetwork, Part 2

by: admin - Jan 06, 2006

After creating the database table for the multipart articles, I had to create one custom PHP function that queries the table every time an article page is viewed.

Here at GIDNetwork.com, the article pages have filenames like b-84.html. Don't let that confuse you, it's not really a HTML document, it's just Apache's mod_rewrite module in action. The actual document is simply a PHP script, e.g. article.php and Apache translates b-84.html into article.php?id=84.

Anyway, back to the custom PHP function...

Custom PHP Function: loadArticleParts

The custom PHP function to load the multipart article information is shown below.

PHP Code Example:


<?php

/**
 * Loads all multipart article <b>id</b>s (key) and <b>title</b>s (value)
 * for GIDNetwork.com articles.  Returns an EMPTY array
 * if none exists.
 * 
 * @param resource $conn
 * @param integer $article_id The article ID.
 * @return array
 */
function loadArticleParts( &$conn$article_id )
{
  $array = array();
  $sql "SELECT `AP2`.`partid` AS `id`, `A`.`title`
          FROM `article_parts` AS `AP`
          LEFT JOIN `article_parts` AS `AP2` ON ( `AP`.`articleid`=`AP2`.`articleid` AND `AP2`.`partid`!=$article_id )
          LEFT JOIN `articles` AS `A` ON `AP2`.`partid`=`A`.`id`
          WHERE `AP`.`partid`=$article_id";
  if( $result=@mysql_unbuffered_query($sql$conn) )
  {
    while( $row=@mysql_fetch_assoc($result) )
      $array$row['id'] ] = $row['title'];
  }
  return( $array );
}

\?>

Using a self-join, the SQL in this function finds the articleid off AP (an alias for the article_parts table) and then searches a copy of itself, i.e. AP2 (the alias for the copy of article_parts) for the ALL the partids matching the (found) articleid.

The final (left) join is simply to get the titles off the articles table.

I hope that made sense, if not, please leave a comment with your question and I will be happy to try again.

Modify article.php

Now comes the part where I had to modify the article.php file. Assuming I put the loadArticleParts() function into an (existing) include file - say, functions.articles.inc.php - all I need to do is insert a few lines to show the multipart links (on the web page), if any exists for an article...

PHP Code Example:



<?php
// Filename: article.php
// ---------------------
include_once( '/home/username/shared/functions.articles.inc.php' );

if( !isset($_GET['id']) )
  trigger_error"Error Message..."E_USER_ERROR );
else
  $_GET['id'] = intval$_GET['id'] );

// ... existing, regular code to generate web page content.
// ...

// NEW (02.01.2006): show multipart links, if any.
if( $multipart_articles=loadArticleParts($conn$_GET['id']) )
{
  echo "<ul>";
  foreach( $multipart_articles as $multipart_id=>$multipart_title )
  {
    echo "<li><a href="/b-$multipart_id.html">",
         htmlspecialchars$multipart_title ),
         "</a></li>";
  }
  echo "</ul>";
}

// ... the rest of the web page generating code.
?>


This is a 2 part article, so the results from this modification is present on this page. :P

Would you like to comment? This story has been viewed 5,264 times.
« Multipart Articles on GIDNetwork Introducing GIDZipTest... again »

__top__

Copyright © GIDNetwork™ 2001 - 2017

Another website by J de Silva

Page generated in : 0.01183 sec.