<?php
/**  latest_post_by_category()
 ** Extract the latest post by category. Output is the date, the post title
 **   as a link to the full story and optionally the full or partial contents.
 ** Parameters:
 **     $cat (default 1) -- the category of story to extract
 **     $story (default 0) -- 0 = don't output the story, 1 = output the full
 **                           story, 2 = output the first 80 chars and an
 **                           ellipsis.
 ** You can change the date format in the function itself
 **/
function latest_post_by_category($cat=1$story=0) {
    global 
$tableposts;
    if (
$story == 1) {
        
$post_amount ', post_content AS content';
    } else if (
$story == 2) {
        
$post_amount ', SUBSTRING(post_content, 1, 80) AS content';
    } else {
        
$post_amount '';
    }
    
$date_format '%d/%m/%y %h:%i'// See http://www.mysql.com/doc/en/Date_and_time_functions.html for full spec
    
    
$sql" SELECT ID, post_title, DATE_FORMAT(post_date, '$date_format') AS formatted_date " .
          
" $post_amount  " .
          
" FROM $tableposts " .
          
" WHERE post_category = $cat " .
          
" ORDER BY post_date DESC LIMIT 1";
    
$result=mysql_query($sql);
    if (
$row mysql_fetch_array($result)) {
        echo(
$row['formatted_date'].'<br /><a href="'.$siteurl.'/'.$blogfilename.'?p='.$row['ID'].'">'.
             
stripslashes($row['post_title']).'</a><br />');
        if (
$story == 1) {
            echo(
stripslashes($row['content']).'<br />');
        } else if (
$story == 2) {
            echo(
stripslashes($row['content']).'...<br />');
        }
    }
}

/** random_post_by_category()
 ** as above but random rather than latest
 **/
function random_post_by_category($cat=1$story=0) {
    global 
$tableposts;
    if (
$story == 1) {
        
$post_amount ', post_content AS content';
    } else if (
$story == 2) {
        
$post_amount ', SUBSTRING(post_content, 1, 80) AS content';
    } else {
        
$post_amount '';
    }
    
$date_format '%d/%m/%y %h:%i'// See http://www.mysql.com/doc/en/Date_and_time_functions.html for full spec

    
$sql" SELECT ID, post_title, DATE_FORMAT(post_date, '%d/%m/%y %h:%i') AS formatted_date " .
          
" $post_amount  " .
          
" FROM $tableposts " .
          
" WHERE post_category = $cat " .
          
" ORDER BY RAND() LIMIT 1";
    
$result=mysql_query($sql);
    if (
$row mysql_fetch_array($result)) {
        echo(
$row['formatted_date'].'<br /><a href="'.$siteurl.'/'.$blogfilename.'?p='.$row['ID'].'">'.
             
stripslashes($row['post_title']).'</a><br />');
        if (
$story == 1) {
            echo(
stripslashes($row['content']).'<br />');
        } else if (
$story == 2) {
            echo(
stripslashes($row['content']).'...<br />');
        }
    }
}

/*
Example use:

This line can go anywhere after php $blog=1 include("blog.header.php"); in your 
main blog page (index.php) which is on the first line.  

    include_once('latest_post.php'); 

Then put one of these where you want the link/story to appear:

    latest_post_by_category(3);  // category 3 no 'content'
    latest_post_by_category(3, 2);  // category 3 first 80 chars
    latest_post_by_category(2, 1);  // category 2 full 'content'

as above but random
    random_post_by_category(2); 
    random_post_by_category(2, 2); 
    random_post_by_category(2, 1);

each of these needs to be inside a < ? php ? > block
*/

?>