PDA

View Full Version : Display Link Counts next to Categories


MrRundog
03-29-2005, 12:51 PM
This mod requires that you know how to add a new field the your mysql table 'Category'
First - Let’s add in the calls to the new function in all pages that require it....

Open up Classes folder - open file class.Category
Find - var $accepted;
Add Below - var $CatNum;

Find - $description = $this->getProperty('description') ? $this->getProperty('description') : '';
Add Below - $CatNum = $this->getProperty('CatNum') ? $this->getProperty('CatNum') : '';

Find - $tpl->set_var('SUBCAT_DESCRIPTION', $this->getProperty('description') );
Add Below - $tpl->set_var('SUBCAT_CATNUM', $this->getProperty('CatNum') );

Find - 'description' => $obj->getProperty('description'),
Add Below - 'CatNum' => $obj->getProperty('CatNum'),
Save & Close

Open up tpl Folder - Open view_categories.html file
Find - <H4>%7B$items%5Bsubcategory%5D.link%7D"]{$items[subcategory].title}</H4>
Replace with - <H4>$items%5Bsubcategory%5D.link%7D"]{$items[subcategory].title}
({$items[subcategory].CatNum})</H4>

**Note The above line can be formatted to suit your needs by altering/adding to the style info in the head of the index.html file in tmp folder**
Save & Close

Open up index.php in main directory folder (whatever you called it)
Find - $links = $tpl->fetch('view_links.html');
Add Below - $tpl->assign('num', '$CatNum');
Save & Close

Second - Let's Create a new field in the database/Category table. The new field should be called catNum set the format to be INT

Third - We need the function that will do all the necessary work to add our link counts to the database table 'Category' in our new field 'catNum'
Open up a text file or use an editor -
Start with a blank page -
Insert this code...


<?php
$conf = array();
//The required files to run this function set
require (MAIN_DIR . 'conf.php');
//init database
$dbh = new DB();
$dbh->user = $conf['DB_USER'];
$dbh->pass = $conf['DB_PASS'];
$dbh->server = $conf['DB_HOST'];
$dbh->database = $conf['DB_NAME'];
$dbh->connect();

//Select the id field which is the relationship for the category field in links table
$info = mysql_query("SELECT id FROM Category")
or die(mysql_error());
//Start a loop to run through each category until there are no more
while($row = mysql_fetch_array( $info )) {
//variable to store the current row id number
$rowID = $row['id'];
//Use the current id(now a variable $rowID) number to search all links that belong to the category matching the id number in links table
$result = mysql_query("SELECT * FROM Link WHERE `Link`.category = $rowID;")
or die(mysql_error());
//variable to store the current number of links in the current category being processed
$CatNum = 0;
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a variable
$CatNum++;
}
//add the new result of the CatNum variable(the sum of links belonging each category) to the databse field catNum on each pass through
the loop
$result = mysql_query("UPDATE Category SET CatNum='$CatNum' WHERE `Category`.id = $rowID")
or die(mysql_error());
}
//close the database connection for this pass.
mysql_close();
?>
Save As linkCount.php into the main folder where index.php resides. I have commented the code as best I can - to explain what is happening. I am
fairly certain that this could be cleaned up a little by a more experienced programmer (especially the error handling - though I have no error as yet).
Cue Igor

Finally - Upload all the altered files to their correct directories and upload the new file 'linkCount'

To Do** If there is a subcategory - Add the links from those to the parent - As is this is not the case - while it does add link totals to the subs - I have

not yet tackled adding sublink totals to the main category - A minor modification I am sure - .

This mod worked for me with no errors - Disclaimer - This is an unofficial mod and I will not be held responsible for any errors or damage caused by Applying this mod. ...ewww...scary stuff - I’m being practical ;-)

Good luck
Andy

David
03-29-2005, 04:37 PM
Thanks for the submission, Andy. I am hopeful that more people will join us and help us make the code even better as we go along. I'm working on this now. In the meantime, I really appreciate what you have done.
Thanks,
David

MrRundog
03-29-2005, 07:11 PM
Your welcome David Thanks

Andy

Anonymous
04-14-2005, 03:35 AM
Look at this topic. I did this same thing.

mdarriba
09-22-2005, 06:41 AM
Hi David, I have a question, where is class.Category.php I cant find this file in the directories.

Thank you for your help and time

Mariano

David
09-22-2005, 06:46 AM
I believe this is a thread concerning the 1.0 version, so it is a little outdated.