My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CodeSamples  
Some useful code samples
Updated Nov 5, 2010 by chriswat...@gmail.com

Show articles that are NOT assigned to specified categories

The $cat variable allows you to show articles based on which category they're assigned to. This code sample allows you to do the opposite, show articles that are NOT assigned to the specified categories

<?php
require_once('news/db.php');
require_once('news/config.php');


// Specify here which categories you DON'T want to use
$nocats = array();
$nocats[] = "Cat1";
$nocats[] = "Cat2";
$nocats[] = "Cat4";

// Grab all existing categories so we can check if the ones specified above exists or not
$allcats = DataAccess::fetch("SELECT id, name FROM " . NEWS_CATS);
$j = 0;
$nocatids = array();
foreach($allcats AS $acat){ 
	if(in_array($acat['name'],$nocats)){
		// Categories which do exist get added to the $nocatids array
		$nocatids[] = $acat['id'];
		$j++;
	}
}

// Implode the $nocatids array so that it can be used in a query to select all other categories
$nocatids = implode(",", $nocatids);
$nocats = DataAccess::fetch("SELECT name FROM " . NEWS_CATS . " WHERE id NOT IN ($nocatids)");

unset($cat);
$cat = array();
foreach($nocats AS $nocat){
	// Assign all the selected categories to the $cat array
	$cat[] = $nocat['name'];
}

include 'news/index.php';
?>

Display categories along with the article count for each

The following code grabs all the categories within the system then outputs them along with the amount of news articles assigned to each one like so.

2 - Blog
1 - Music
1 - Updates 
<?php
require_once('news/db.php');
require_once('news/config.php');
$catcounts = DataAccess::fetch("SELECT DISTINCT
	(SELECT COUNT(storyid) FROM news30_groupcats WHERE type = 'news' AND catid = news30_cats.id) AS totalposts,
	name FROM news30_cats
	LEFT JOIN news30_groupcats ON news30_cats.id = news30_groupcats.catid ORDER BY news30_cats.name ASC");
    
foreach($catcounts AS $c){
	echo $c['totalposts'] . " - " . $c['name'] . "<br />";
}
?> 

Display headlines assigned to the same category as the current news article

When viewing a single news article you can use this code to display some headlines below the article that are assigned to the same categories as the article currently being viewed.

<?php
include 'news/index.php';

if($_GET['id']){
	// viewing a single news article
	// if using friendly urls change $_GET['id'] to $url['0'] both here and further down this code
	
	require_once('news/db.php');
	require_once('news/config.php');
	
	$cat = array();
	$assignedcats = array();
	$assignedcats = DataAccess::fetch("SELECT news30_cats.name AS catname FROM news30_groupcats
		LEFT JOIN news30_cats ON news30_groupcats.catid = news30_cats.id
		WHERE storyid = ? AND type = 'news'", $_GET['id']);
	
	// grab any categories the current article is assigned to then assign them to the $cat array we normally use to specify categories before an include
	foreach($assignedcats AS $cats){
		$cat[] = $cats['catname'];
	}
	$static = true;
	$template = 'headlines';
	include 'news/index.php';
}
?> 

Dynamic page titles / meta tags

When viewing a single news article you can use this code to display the title of the article in the head section of your page like so

<?php
require_once('news/db.php');
require_once('news/config.php');

if(FRIENDLY){
	$id = $url['0'];
}else{
	$id = $_GET['id'];
}
$headerinfo = DataAccess::fetch(sprintf("SELECT title, shortstory AS summary, story AS article FROM %s WHERE id = ?", NEWS_ARTICLES), $id);
$title		= $headerinfo['0']['title'];
$summary	= str_replace("\"", "", strip_tags($headerinfo['0']['summary']));
if(!$id || count($headerinfo) < 1){
	// not viewing a full article or article doesn't exist so display your normal title
	echo '<title>example</title>';
	echo '<meta name="title" content="example.com" />';
	echo '<meta name="description" content="example.com - description" />';
}else{
	// Viewing a full article so display the title of that article + description meta tags
	echo sprintf("<title>%s</title>", $title);
	echo sprintf('<meta name="title" content="%s" />', $title);
	echo sprintf('<meta name="description" content="%s" />', $summary);
}
?>

Show news stats

To display some stats about the news system use the following code

<?php
require_once('news/db.php');
require_once('news/config.php');

// users
$totalusers = count(DataAccess::fetch(sprintf("SELECT uid FROM %s", NEWS_USERS)));
echo $totalusers . " users <br />";

// news articles
// Shows only approved articles, to show both non-approved and approved remove the WHERE condition
$totalnews = count(DataAccess::fetch(sprintf("SELECT id FROM %s WHERE approved = ?", NEWS_ARTICLES), '1'));
echo $totalnews . " articles <br />";

// comments
$totalcomments = count(DataAccess::fetch(sprintf("SELECT id FROM %s", NEWS_COMMENTS)));
echo $totalcomments . " comments <br />";

// smilies
$totalsmilies = count(DataAccess::fetch(sprintf("SELECT id FROM %s", NEWS_SMILIES)));
echo $totalsmilies . " smilies <br />";

// filters
$totalfilters = count(DataAccess::fetch(sprintf("SELECT id FROM %s", NEWS_FILTER)));
echo $totalfilters . " filters <br />";

// categories
$totalcategories = count(DataAccess::fetch(sprintf("SELECT id FROM %s", NEWS_CATS)));
echo $totalcategories . " categories <br />";

// templates
$totaltemplates = count(DataAccess::fetch(sprintf("SELECT id FROM %s", NEWS_TEMPLATES)));
echo $totaltemplates . " templates <br />";

// access levels
$totalaccess = count(DataAccess::fetch(sprintf("SELECT uid FROM %s", NEWS_ACCESS)));
echo $totalaccess . " access levels <br />";

// total images
$totalimages = count(DataAccess::fetch(sprintf("SELECT uid FROM %s", NEWS_IMAGES)));
echo $totalimages . " images <br />";

// total files
$totalfiles = count(DataAccess::fetch(sprintf("SELECT uid FROM %s", NEWS_FILES)));
echo $totalfiles . " files <br />";

// total rss feeds
$totalrss = count(DataAccess::fetch(sprintf("SELECT feedid FROM %s", NEWS_FEEDS)));
echo $totalrss . " rss feeds <br />";

?>

Display the latest 10 comments

Display the latest 10 comments for all news articles

<?php
require_once('news/db.php');
require_once('news/config.php');

// amount of comments to show
$commentstoshow = 10;

// to change the order the comments are shown change ASC to DESC
$allcomments = DataAccess::fetch(sprintf("SELECT pid, user, email, message, id FROM %s ORDER BY timestamp ASC LIMIT 0 , %s", NEWS_COMMENTS, $commentstoshow));

foreach($allcomments AS $comment){
	// outputs 
	// <a href="index.php?id=1#2">message</a> - username - email
	echo '<a href="index.php?id=' . $comment['pid'] . '#' . $comment['id'] . '">' . $comment['message'] . "</a> - " . $comment['user'] . " - " . $comment['email'] .  "<br />";
}
?>

Sign in to add a comment
Powered by Google Project Hosting