View Single Post
Old 22-11-2009, 00:45   #12
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 30
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: Any php coders out there?

Quote:
Originally Posted by Mr Angry View Post
Certainly looks "dinky" but I'm looking something which lists headlines as links which open news story pages when clicked.

A simple admin interface where the admin types in the date, headline and content. submits it and hey presto! a link appears.

PM (not Gordon Brown) en route.
Ahh I see what you mean.

I was taking the "news" bit too literally. I thought you wanted to stream headlines from BBC News or something.

Easiest way is to do this:

Create a database table (with phpMySql is easiest) . You need the following fields (as a minimum) with the data type in brackets.

Quote:
id (int) & auto increment
headline (varchar)
content (longtext)
date (datetime)
Or import this SQL:

Code:
CREATE TABLE IF NOT EXISTS `headlines` (
  `id` int(11) NOT NULL auto_increment,
  `headline` varchar(100) NOT NULL default '',
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  `content` longtext NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Then you need to create 3 files:

connect.php - This stores the database username/password and makes the connection

PHP Code:
<?php

$username 
"your_username";
$password "your_password";
$database "your_database";

mysql_connect("localhost",$username,$password)
    OR die(
mysql_error());

mysql_select_db($database);
?>
admin.php - This allows you to add new news articles.

PHP Code:
<html>
    
<h1>Admin page...</h1>

<?php

// Connect to database
include("connect.php");

// Check if you are submitting a new headline
if (isset($_POST['submit']))
{
    
    
// Build the INSERT query
    
$sql "INSERT INTO headlines (`date`, `headline`, `content`) VALUES('" date("Y-m-d H:i",time()) . "','" mysql_real_escape_string($_POST['headline']) . "','" mysql_real_escape_string($_POST['content']) . "')";
    
    
$result mysql_query($sql);
    
    if (
mysql_affected_rows() > 0)
    {
        echo (
"<h2>Headline submitted.</h2><br /><br />");
    }
    else
    {
        echo (
"Headline NOT submitted because: " mysql_error());
    }
    
        
}

// Otherwise create a new headline

// Create form to submit to back to this page
?>
<form action="<?php echo($SERVER['PHP_SELF']); ?>" method="post">
<table>
    <tr><td>Headline</td><td><input type="text" name="headline" id="headline" size="100" /></td></tr>
    <tr><td>Content</td><td><textarea name="content" id="content" cols="100" rows="20"></textarea></td></tr>
    <tr><td colspan="2" style="text-align:center;"><input type="submit" name="submit" id="submit" value="Add headline" /></td></tr>
</table>
</form>
</html>
headlines.php - This shows all the headlines if no parameter is passed or a specified headline if one is.

PHP Code:
<html>
    <head>
        <!-- You need this bit to wrap the headline content properly -->
        <style>
            pre {
                font-family:Times New Roman;
                white-space: pre-wrap; /* css-3 */
                white-space: -moz-pre-wrap /* Mozilla, since 1999 */
                white-space: -pre-wrap; /* Opera 4-6 */
                white-space: -o-pre-wrap; /* Opera 7 */
                word-wrap: break-word; /* Internet Explorer 5.5+ */
            }
        </style>
    </head>
    
    <h1>List of headlines</h1>
<?php

// Connect to database
include("connect.php");

// Check whether to view a single news story or a list of headlines
if (isset($_GET['h']))
{
    
// Show headline
    
$sql "SELECT * FROM headlines WHERE id = " $_GET['h'];
    
    
$query mysql_query($sql);
    
    
$row mysql_fetch_array($query);
    
    echo (
"<div style=\"width:500px;\"><h2>" $row['headline'] . "</h2></a>Published: " date("d/m/Y G:i"strtotime($row['date'])) . "<br /><pre>" $row['content'] . "</pre></div>");
        
    
}
else
{
    
// Show headlines
    
$sql "SELECT * FROM headlines ORDER BY date DESC;";
    
    
$query mysql_query($sql);
    
    while(
$row mysql_fetch_array($query))
    {
        echo (
"<a href=\"headlines.php?h=" $row['id'] . "\"><h2>" $row['headline'] . "</h2></a>Published: " date("d/m/Y G:i"strtotime($row['date'])) . "<br />");
        
        
// Handle preview (first 100 characters)
        
if (strlen($row['content']) > 100)
        {
            echo (
substr($row['content'],0,100) . "...");
        }
        else
        {
            echo (
$row['content']);
        }
        
        echo 
"<hr />";
        
    }
    
}


?>

</html>
Bear in mind this is the bear minimuim you would need with no style formatting. You'll probably want to add in author and paging but this will get you going.

Anything else you need or anything explaining, lemme know.

Edit: As Callumpy said you can use Wordpress or similar. They can be hard work to merge into an existing site so might not gain any time savings.

Last edited by punky; 22-11-2009 at 00:54.
punky is offline   Reply With Quote