Hi All,
First up, if anyone knows a class that can do this already, please let me know.
I know of the Categories Class at phpclasses.org, but I needed to develop one that manages unlimited subCats with VARCHAR keys, which are easier on the eye, memory, and searchbots.
My site will consist of modules for tutorials, news, forum and reviews. Each module uses extensions of common subclasses for various tasks, but the categories class is standard. Therefore, the DB will contain 4 categories tables, each prefixed with the module letter.
EG: tCats, nCats, fCats and rCats.
CREATE TABLE cats (
id VARCHAR(50) UNIQUE NOT NULL PRIMARY KEY, // VARCHAR KEY for URL
title VARCHAR(50) NOT NULL,
hasSubs TINYINT NOT NULL DEFAULT'0', // boolean 1 || 0
numTopics INT NOT NULL DEFAULT'0',
parentID VARCHAR(50) NOT NULL, // Holds parentID for compiling subCats
sort TINYINT NOT NULL, // Order of appearance in list
depth TINYINT NOT NULL, // number of spacer.gif's for indentation in category tree
INDEX(parentID)
)
The following gives an overview of the class, and has been abbreviated:
class cats{
// Instantiate class parameters
function cats($module) {
sets variables
$this->siteUrl;
$this->tableName;
$this->module;
}
// Decide whether to get subCats or continue
function listCats($parentID) {
list($hasSubs,$id)=fetch('Select hasSubs,id where id = $parentID');
if (hasSubs=='1') {
$this->tempArray[]=$this->makeLink($id);
$obj=new cats($this->module,$id);
$this->tempArray[]=$obj->fetchResults();
}else{
$this->tempArray[]=$this->makeLink($id);
};//end if
}//end function
// Make link
function makeLink($id){
SELECT title WHERE id = '$id';
return <ahref="$this->Url.'/'.$this->module.'/'.$id">$title</a>;
} // end function
// Results
function fetchResults(){
foreach ($this->tempArray as $key=>$value){
$string.="
".$value;
};//end foreach
return $string;
} // end function
// Other methods:
function addCat($id,$title,$parentCat){}
function deleteCat($id){}
function changeParent($id,$newParentID){}
function reSort($id,$newSort){}
function renameCat($oldID,$newID,$newTitle){}
[bold]And here's the problem with complete syntax:[/bold]
recalculateNumTopics($parentID){
$query="SELECT id, hasSubs FROM ".$this->tableName." WHERE parentID = '".$parentID."' ORDER BY sort ASC";
$rtn=mysql_query($query);
########### Line 30 ############
while($rtrn=mysql_fetch_assoc($rtn)){
if($rtrn['hasSubs']=='1'){
$newID=$rtrn['id'];
$obj=new cats($this->module);
$this->tempArray[]=$obj->recalculateNumTopics($newID);
}else{
$query="SELECT numTopics AS numTopics FROM ".$this->tableName." WHERE id = '".$parentID."'";
$rtn=mysql_query($query);$rtn=mysql_fetch_array($rtn);
$this->tempArray[]=$rtn['numTopics'];
};//end if
};//end while
foreach($this->tempArray as $key=>$value){
$this->total += $value;
};
$query="UPDATE ".$this->tableName." SET numTopics = '".$total."' WHERE id = '".$parentID."'";
return $this->total;
} //end function
};//end class
[hr]
The idea is to recalculate numTopics for all parentCats in the table like so:
$cat=new cats('tutorials');
$tableTotal=$cat->recalculateNumTopics('0');
Now, that all looks right to me, but for some reason, I keep getting the mysql error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\PJAppSrv_v2.2\webapps\testmethod_debug_tmp.php on line 30
I've tried echoing the queries and variables to screen at various points in the script, but I still can't find the cause for this error. I have used a similar strategy in my listCats() method, where creating new objects in the 2-way switch works fine.
This has me stumped! The queries work fine by cmd querying MySQL by hand.
I really do need this method as a management tool. Perhaps someone else can see what I need to do, and offer a method that has worked for them.
Help, anyone?
Cheers.
Anthony Gallon
Music Matters Most
www.studiojunkies.com