Login

Member statsTotal: 206Latest: CcardinGuests online: 5Members online: 0
EmailLogin here

Location
ForumPHP Tutorials • [NEWS ADDON] Display how many posts are in a selected category

Access: Member
Total posts: 9

Status: Offline
[NEWS ADDON] Display how many posts are in a selected category Quote 

I came up with this code for my website, I thought I would post it should anybody find it useful, its a short simple snippet really;

PHP Code:
<?php

define
('SQL_HOST','localhost'); // Connect to your mySQL database
define('SQL_USER','xxx');
define('SQL_PASS','xxx');
define('SQL_DB','xxx');

$link = mysql_connect(SQL_HOST,SQL_USER,SQL_PASS)
or die(
'Could not connect to the mysql server...; ' . mysql_error());

mysql_select_db(SQL_DB,$link)
or die(
'Could not select database...; ' . mysql_error());

$query = mysql_query("SELECT COUNT(*) AS totalposts FROM news30_story WHERE catid='6'"); //Change the catid here to the id of your category that you want to count the posts for. To find out the id for your category go to the news admin panel, go to the categories page proceed to scroll over your category then look in the status bar of your browser, the last number is your category id

$query = mysql_fetch_array($query);

$number = $query['totalposts'];

echo
$number;

?>
01:12pm 21st Aug 08
_______________
my website: www.vyb3.co.uk
Access: Admin
Total posts: 174

Status: Offline
Re: [NEWS ADDON] Display how many posts are in a selected category Quote 

This looks good, one thing I'd change though is the query your using, I've edited it slightly so the user doesn't have to enter the id of the category, instead they can just enter the category name.

PHP Code:
<?php
$catname
= 'Cat2';
$query = mysql_query("SELECT COUNT(*) FROM $newstable INNER JOIN $newscats ON $newstable.catid = $newscats.id WHERE $newscats.name = '$catname'");
?>
09:47pm 21st Aug 08
_______________
Chris - Network-13
Last edited by Chris at 04:37am 22nd Aug 08
Post a reply!