I am currently working on a new site that offers various files for download. The file information is stored in a MySQL database and the page is produced in PHP depending on how the user got to the page:
[php]
<?php
session_start() ;
$_SESSION['pageStyle'] = $_SESSION['pageStyle']; // set to whatever is, as you can only access this page from within a top level section
?>
<!--
File: download.php
Author: Nathan Davies
Created: 09/08/2007
Purpose: The downloads from the CLF site - for each topic type
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<?php
include("includ e/pageHeader.php" );
if($loUser->hasAccess(1) ||$loUser->hasAccess(2) || $loUser->hasAccess(3) )
{
// establish where this page was called from
if(isset($_GET['topicID']) && isset($_GET['topicType'])) // use isset as 0 is valid for this page
{
$lnTopicID = $_GET['topicID'] ;
$lnTopicType = $_GET['topicType'] ;
switch($lnTopic Type)
{
case 0: // ad hoc entries that appear under general resources
$lnSubSection = 4 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID,
b.description
FROM download a
LEFT OUTER JOIN downloadtype b on b.ID = a.typeID
WHERE a.topicType = $lnTopicType
ORDER BY a.typeID ASC" ; // this set is only ever the full set
break ;
case 1: // event downloads
$lnSubSection = 2 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID, a.topicID,
b.title,
c.description
FROM download a
LEFT OUTER JOIN event b on b.ID = a.topicID
LEFT OUTER JOIN downloadtype c on c.ID = a.typeID
WHERE a.topicType = $lnTopicType" ;
// if only one specific event then add that condition to the SQL statement
if($lnTopicID > 0)
{
$lcDownloadSele ct .= " AND a.topicID = $lnTopicID" ;
}
$lcDownloadSele ct .= " ORDER BY a.topicID ASC" ;
break ;
case 2: // module downloads
$lnSubSection = 3 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID, a.topicID,
b.title,
c.description
FROM download a
LEFT OUTER JOIN module b on b.ID = a.topicID
LEFT OUTER JOIN downloadtype c on c.ID = a.typeID
WHERE a.topicType = $lnTopicType" ;
// if only one specific module then add that condition to the SQL statement
if($lnTopicID > 0)
{
$lcDownloadSele ct .= " AND a.topicID = $lnTopicID" ;
}
$lcDownloadSele ct .= " ORDER BY a.topicID ASC" ;
break ;
}
include("includ e/subSectionTitle AndNavigation.p hp") ;
// execute the query
$laDownload = $loDB->queryGetData($ lcDownloadSelec t) ;
if($laDownload)
{
// there is information to display
switch($lnTopic Type)
{
case 0: // general
echo '<h1 class="pageTitl e">General Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by file type - e.g. PDF, MP3 etc' ;
echo '</p>' ;
break ;
case 1: // event
echo '<h1 class="pageTitl e">Event Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by event title' ;
echo '</p>' ;
break ;
case 2: // module
echo '<h1 class="pageTitl e">Training Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by module title.';
echo '</p>' ;
break ;
}
$lnCurrentCateg oryID = -1 ;
foreach($laDown load as $laDownloadItem )
{
switch($lnTopic Type)
{
case 0: // general
$lnNextCategory ID = $laDownloadItem['typeID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading $laDownloadItem['description']
echo '<h4 class="eventLis tItem">Download Category: ' . substr_replace( $laDownloadItem['description'], strtoupper(subs tr($laDownloadI tem['description'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}
break ;
case 1: // event
$lnNextCategory ID = $laDownloadItem['topicID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading
echo '<h4 class="eventLis tItem">Event: ' . substr_replace( $laDownloadItem['title'], strtoupper(subs tr($laDownloadI tem['title'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}
break ;
case 2: // module
$lnNextCategory ID = $laDownloadItem['topicID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading
echo '<h4 class="eventLis tItem">Module: ' . substr_replace( $laDownloadItem['title'], strtoupper(subs tr($laDownloadI tem['title'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}
break ;
}
// write the stuff that's the same for each type - the link - url, name and the onclick code using the ID to update the association between leader and download
// note the update is only for leaders - so another hasAccess check is required.
echo '<p class="linkList ">' ;
if ($loUser->hasAccess(1) )
{
echo '<a id="' . $laDownloadItem['ID'] . '" "class="inlineL ink" href="' . $laDownloadItem['url'] . '" title="' . $laDownloadItem['name'] . '" onclick="dataUp date(1, ' . $laDownloadItem['ID'] . ')">' . $laDownloadItem['name'] . '</a><br />';
}
else
{
echo '<a class="inlineLi nk" href="' . $laDownloadItem['url'] . '" title="' . $laDownloadItem['name'] . '">' . $laDownloadItem['name'] . '</a><br />';
}
echo '</p>';
}
echo '<br />' ; // just to give it some room to breathe
}
else
{
echo '<p>' ;
echo 'There are currently no downloads available. ' ;
echo 'New information is loaded regularly so please check back soon.' ;
echo '</p>' ;
echo '<p>' ;
echo 'In the mean time please feel free to browse around the rest of the site.' ;
echo '</p>' ;
}
}
else
{
echo '<p>' ;
echo 'Unfortunately this page was accessed incorrectly.' ;
echo '</p>' ;
echo '<p>' ;
echo 'There are 3 valid ways into this page - via Resources, Leaders Forum and Training. ';
echo 'It is essential that you are a registered with the site and that you are currently logged in.' ;
echo '</p>' ;
}
}
else
{
echo '<p>' ;
echo 'Access to this area of the site is for members only. If you are a member already please log in.' ;
echo '</p>' ;
echo '<p>' ;
echo 'If you would like to join the Christian Leadership Foundation please take a moment to complete the <a class="inlineLi nk" href="applicati on.php?step=1" title="Applicat ion Form">form</a>.' ;
echo '</p>' ;
}
include("includ e/pageFooter.php" );
?>
[/php]
On downloading a file the idea is that a record is either updated in or added to the database. The purpose of this is so that at a later date I can make recommendations within the members area based on the category of downloads they have been interested in previously. A simplistic recommendation system.
In order to do this the link to the download file has an onClick that calls a JavaSacript function
Expand|Select|Wrap|Line Numbers
- function dataUpdate(pnType, pnItem)
- {
- GetXmlHttpObject() ;
- gcUrl = "../lib/dataupdateajax.php?type=" + pnType + "&item=" + pnItem
- gcItemID = pnItem
- goXMLHTTP.onreadystatechange = function()
- {
- if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
- {
- alert("Enjoy");
- }
- } ;
- goXMLHTTP.open("GET",gcUrl,true) ;
- goXMLHTTP.send(null) ;
- }
I know it's not getting into the PHP code because I have echo lines in there before any conditions are tested and they don't execute.
The basic problem is that I can't get the php file dataupdateajax. php to load.
Any suggestions on this or alternative ways to build a recommendation system will be greatly appreciated.
Cheers
nathj