Mathias
August 18th, 2007, 08:42 PM
Note, all of my functions belong to classes, but I am posting only snippets of my functions to avoid boredom and giving away all my secrets. Please post corrections, suggestions, and your own related functions.
Mathias
August 18th, 2007, 08:52 PM
Note: I changed my table name to ~Table~ and renamed the variable a couple variables that may have given too much away. Hopefully I didn't hose the code. My regular expressions were updated regularly in this function for a while. It may not be current. User beware.
//-------------------------------------------------------------------------
// Function: GetSubNavigationList
// Author: Andrew Hinkle
// Modified: 07/25/2007
// Description: Return an array of all SubNavigation links.
//-------------------------------------------------------------------------
function GetSubNavigationList()
{
// Get the directory from the current page. Add an ending '/' if necessary.
$sLink = $this->_pSettings['sLink'];
// Remove the "filename" from the link. There is no extension, so dirname($sLink) won't work.
$sDirectoryWithoutSlash = $this->GetDirname($sLink);
// Add the ending slash of the directory if necessary.
$sDirectory = $this->FormatDirectory($sDirectoryWithoutSlash);
// Regular Expression explanation.
// ^ (Start of line), (Directory), [^/]+ (Any character that is not a /, 1 to unlimited times), $ (End of line)
// Get the index of the first and last records.
// $sSQL = "select max(nSort) as nLast from ~Table~ where sLink regexp '^" . $sDirectory . "[^/]+$'";
$sSQL = "select min(nSort) as nFirst, max(nSort) as nLast from ~Table~ where sLink regexp '^" . $sDirectory . "[^/]+$'";
$pRow = $this->_pDatabase->mysqlSelect($sSQL);
$nFirst = !empty($pRow[0]['nFirst']) ? $pRow[0]['nFirst'] : 0;
$nLast = !empty($pRow[0]['nLast']) ? $pRow[0]['nLast'] : 0;
// Get the index of the current record.
if ($this->IsDirectory($sLink))
{
$nLimit = $this->_pSettings['nLimit'];
if ($nLimit)
{
// Round the current record to the limit numbers.
$nCurrent = $this->_nCurrent;
$nRemainder = $nCurrent % $nLimit;
$nCurrent -= $nRemainder;
// Round the last record to the limit numbers.
$nRemainder = $nLast % $nLimit;
$nLast -= $nRemainder;
}
$bIndex = 1;
}
else
{
$nCurrent = $this->_pSettings['nSort'];
$nLimit = 1;
$bIndex = 0;
}
// Get the index of the first, previous, and next records.
$nPrevious = $nCurrent - $nLimit > $nFirst ? ($nCurrent - $nLimit) : $nFirst;
$nNext = $nCurrent + $nLimit < $nLast ? ($nCurrent + $nLimit) : $nLast;
// Regular Expression explanation.
// ^ (Start of line), (Directory), [^/]+ (Any character that is not a /, 1 to unlimited times), $ (End of line)
// This select does not return duplicate rows, so if $nFirst and $nPrevious are the same, then only one record is returned instead of two.
// Only directories with links will display the navigation. A directory with nothing, but subdirectories is ignored.
// $sSQL = "select * from ~Table~ where sLink regexp '^" . $sDirectory . "[^/]+$' and nSort in ($nFirst, $nPrevious, $nNext, $nLast) order by nSort";
// All directories with subdirectories or links will display in the subnavigation.
$sSQL = "select * from ~Table~ where sLink regexp '^" . $sDirectory . "[^/]+/?$' and nSort in ($nFirst, $nPrevious, $nNext, $nLast) order by nSort";
$aRows = $this->_pDatabase->mysqlSelect($sSQL);
// Return 0 if no rows were returned.
if (empty($aRows)) return (0);
// Get the link for the first record.
$i = 0;
$aSubNavigationList[0]['sName'] = "First";
$aSubNavigationList[0]['sLink'] = $bIndex ? "$sDirectoryWithoutSlash.$nFirst/" : $aRows[$i]['sLink'];
// Get the link for the previous record.
$i += $nFirst == $nPrevious ? 0 : 1;
$aSubNavigationList[1]['sName'] = "Previous";
$aSubNavigationList[1]['sLink'] = $bIndex ? "$sDirectoryWithoutSlash.$nPrevious/" : $aRows[$i]['sLink'];
// Get the link for the directories index.
$aSubNavigationList[2]['sName'] = "Index";
$aSubNavigationList[2]['sLink'] = $sDirectory;
// Get the link for the next record.
$i += $nPrevious == $nNext ? 0 : 1;
$aSubNavigationList[3]['sName'] = "Next";
$aSubNavigationList[3]['sLink'] = $bIndex ? "$sDirectoryWithoutSlash.$nNext/" : $aRows[$i]['sLink'];
// Get the link for the last record.
$i += $nNext == $nLast ? 0 : 1;
$aSubNavigationList[4]['sName'] = "Last";
$aSubNavigationList[4]['sLink'] = $bIndex ? "$sDirectoryWithoutSlash.$nLast/" : $aRows[$i]['sLink'];
return $aSubNavigationList;
}
vBulletin® v3.8.0 Beta 1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.