Help - Search - Members - Calendar
Full Version: News CMS Script
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
d7x
Well in this tutorial i will show you how to create a news content managment system!
Lets get started


First for the SQL Code!

CODE
CREATE TABLE `newscms` (
`id` int(10) unsigned NOT NULL auto_increment,
`date` varchar(50) default NULL,
`title` varchar(50) NOT NULL default '',
`message` text NOT NULL,
`icon` varchar(100) NOT NULL default '',
`user` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `date` (`date`)
);


This will set your tables up, hit the SQL button on your mysql program.

This is dbconnect.php


CODE
<?
$username = "username"; //put your mysql username
$password = "password"; //put your mysql password
$host = "localhost"; //put your mysql host usually localhost
$database = "database"; //put your mysql
database name

//Do not change these lines below
mysql_connect($host,$username,$password) or die("Error connecting to Database! " . mysql_error());
mysql_select_db($database) or die("Cannot select database! " . mysql_error());
?>


Now time for the display code, it is comment heavy so you can understand what it does.

CODE
<?
include('dbconnect.php'); //connects to database
//select the table
$result = mysql_query("select * from newscms order by id desc limit 5");
//grab all the content from the table
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$title=$r["title"];
$date=$r["date"];
$user=$r["user"];
$icon=$r["icon"];
$message=$r["message"];
//displays the row's
echo "<img src='
$icon' align='left'>
<b>$title</b> Posted on $date
<br>Posted by: <b>
$user</b>
<br>$message
<br>";
}
?>


The display only shows the 5 newest news posts. Also save that as display.php

Time for the addnews.php

CODE
<? include('dbconnect.php'); ?>
<form action="addnews.php" method="post">
<br>Title:
<br><input name="title" type="text" value="Title">
<br>Author:
<br><input name="user" type="text" value="Name">
<br>Date:
<br><input name="date" type="text" value="<?php print date("F j Y"); ?>">
<br>Icon:
<br><input name="icon" type="text" value="Icon URL">
<br>Message:
<br><textarea name="message" cols="40" rows="6" value="Message"> </textarea>
<br>Password:
<br><input name="password" type="password">
<br><input name="submit" type="submit" value="Submit">
<?php $password="yourpassword"; //change this to the password you want if ($_POST['password']==$password){ //DO NOT CHANGE THIS LINE
if (isset($_POST['submit'])) {
include("dbconnect.php");
$title = addslashes(strip_tags($_POST['title']));
$user = addslashes(strip_tags($_POST['user']));
$icon = addslashes(strip_tags($_POST['icon']));
$message = $_POST['message'];
$date = addslashes(strip_tags($_POST['date']));
$sql = "INSERT INTO newscms SET title='$title', user='$user', icon='$icon', message='$message', date='$date'";
if (mysql_query($sql)) {
echo("Your news has been added.");
} else {
echo("Error adding entry: " . mysql_error() . "");
}
}
?>


There is one thing you must change in this, its the password, change it to whatever you want.

Thats it, your done! Have fun! Modify it however you want! I have tested it and it works like a charm! Happy coding!

Thought everyone would like it smile.gif
K.Schmidt
Nice Script, works great, i like it w00t.gif
Zenom
This is pretty good, nice job.
bluestriker
The script is good...but you need to explain it more.
austen
QUOTE(bluestriker @ Oct 13 2005, 01:26 AM)
The script is good...but you need to explain it more.

I agree here, if it is to be a tutorial, explain for the noobs how it works victory.gif Great job on the script though, it's great for integration with just about anything if people are too lazy to code something from scratch or dont' know how yet.
d7x
yeh, i will soon, been busy glad ppl like it tho
Raremandan
Awesome script, I was having trouble with the SQL bit but after reading many tutorials and this one is get it.
blinek
like austen said this seems more of a script than a tutuorial than a script, but its still nice.

Maybe you could also add a way to edit and delete the news aswell smile.gif
Indigo
Agree with Aeiko, edit and delete password, pluss a way to login with sessions. Then itīd be almost perfect
bsf
I am learning ALOT from this tutorial/script but i have a few questions smile.gif

CODE
$title = [b]stripslashes(strip_tags(htmlspecialchars($_POST['title']));[/b]

What does the end of that code actually do? (the bold part tongue.gif)

and,
CODE
$result = mysql_query("select * from newscms order by id desc limit 5");

What does this whole code stand for? wacko.gif
Indigo
I can at least explain the last example there:
* = All
from newscms = From the database table "newscms"
Order by id = Simple enough, it orders all in the database by id
desc limit 5 = Shows the newest news on top, and with a limit of five, so only five news will show.

So the code does the folllowing: Selects all entries in the table "newscms" and orders them by id so that only 5 news shows, with the newest news on top.

Hope that helps:)
chatmasta
Here, I improved upon your code. It should be much easier to read, faster, and more secure now.

What I did:
- Changed double quotes to single quotes - this is a speed issue. Only use double quotes when echoing out a few variables. If you only have one variable to echo out, it is usually more efficient to break out of your echo and display the variable, then come back in.
- Put a space on both sides of operators. This is just a readability issue. It's much easier to read this way.
- Made your HTML standards compliant, mainly with <br /> and <strong></strong> instead of <b></b>. Always validate your HTML code here.
- Split your code up more - try to logically place breaks in between different parts of your code. This makes it a lot easier to read.
- I put tabs (I should use spaces, but I'm lazy. tongue.gif) in it to give it format. This is really important for readability.
- I changed addslashes() on your MySQL query variables to mysql_real_escape_string(). This is a lot more secure, and you should always use it before putting any variables into your database. It prevents MySQL injection better than addslashes().
- I changed isset() to !empty(). This is faster.

Here's the code.

CODE
<?
include('dbconnect.php'); //connects to database

//select the table
$result = mysql_query("select * from newscms order by id desc limit 5");

//grab all the content from the table
while($r = mysql_fetch_array($result))
{
    $id = $r['id'];
    $title = $r['title'];
    $date = $r['date'];
    $user = $r['user'];
    $icon = $r['icon'];
    $message = $r['message'];
    
    //displays the rows
    echo "<img src='$icon' align='left' /><strong>$title</strong> Posted on $date <br />
    Posted by: <strong>$user</strong><br />
    $message <br />";
}

?>


CODE
<? include('dbconnect.php'); ?>
<form action="addnews.php" method="post"><br />
Title: <br />
<input name="title" type="text" value="Title"><br />
Author:<br />
<input name="user" type="text" value="Name"><br />
Date:<br />
<input name="date" type="text" value="<?php print date('F j Y'); ?>"><br />
Icon:<br />
<input name="icon" type="text" value="Icon URL"><br />
Message:<br />
<textarea name="message" cols="40" rows="6" value="Message"></textarea><br />
Password:<br />
<input name="password" type="password"><br />
<input name="submit" type="submit" value="Submit"><br />

<?php

$password = 'yourpassword'; //change this to the password you want if ($_POST['password']==$password){ //DO NOT CHANGE THIS LINE
if (!empty($_POST['submit']))
{
    include("dbconnect.php");
    $title = mysql_real_escape_string(strip_tags($_POST['title']));
    $user = mysql_real_escape_string(strip_tags($_POST['user']));
    $icon = mysql_real_escape_string(strip_tags($_POST['icon']));
    $message = $_POST['message'];
    $date = mysql_real_escape_string(strip_tags($_POST['date']));
    $sql = "INSERT INTO newscms SET title='$title', user='$user', icon='$icon', message='$message', date='$date'";
    
    if (mysql_query($sql)) {
        echo 'Your news has been added.';
    }
    else
    {
        echo 'Error adding entry: ' . mysql_error();
    }
}
?>
BackEnThaWomb
lookin good! thanks
pihl
one thing some people don't understand is that a CMS stands for Content Management System, and its general idea is to add, edit and delete content.
This script is only adding (and displaying) the news, there's no edit nor delete function there.

Nice news-adding-script anyways (to be the nosy kind)
Morrigan
It's pretty cool, but as said above, it doesn't edit nor delete content. It's easily able to be worked upon to add those functions.
spleen
Wow. Nice tut mate.
ogrekey
Very helpful tutorial, I've been trying many to create an updateable news section and this is the first to work for me. But I have a question for anyone who'd like to help me out a bit - what code would I use to integrate the display.php file into a flash movie? I want to be able to update the news with the addnews.php file and have the updates show up in a textbox or something of the like in my flash-based website.
SiteReboot
How could one modify this script to allow for catagories?
CyrusWu
Nice one! Too bad i have an better copy of it.
abosaleh
here is the delete page for all and save as delete.php


CODE
<?
include('dbconnect.php'); //connects to database


// select record from mysql
$sql="SELECT * FROM newscms";
$result=mysql_query($sql);


?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<div align="center">
<table width="69%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF" colspan="5" align="center"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF" width="4%"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF" width="13%"><strong>title</strong></td>
<td align="center" bgcolor="#FFFFFF" width="16%"><strong>date</strong></td>
<td align="center" bgcolor="#FFFFFF" width="30%"><strong>message</strong></td>
<td align="center" bgcolor="#FFFFFF" width="5%">&nbsp;</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF" align="center" width="4%"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF" align="center" width="13%"><? echo $rows['title']; ?></td>
<td bgcolor="#FFFFFF" align="center" width="16%"><? echo $rows['date']; ?></td>
<td bgcolor="#FFFFFF" align="center" width="30%"><? echo $rows['message']; ?></td>
<td bgcolor="#FFFFFF" align="center" width="5%"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td>
</tr>
<?

// close while loop
}

// close connection;
mysql_close();

?>
</table></div>
</td>
</tr>
</table>
abosaleh
and here is the delete_ac.php

QUOTE
<?
include('dbconnect.php'); //connects to database

[b]// get value of id that sent from address bar
$id=$_GET['id'];


// Delete data in mysql from row that has this id
$sql="DELETE FROM newscms WHERE id='$id'";
$result=mysql_query($sql);


// if successfully deleted
if($result){
echo "<center><b><br>";
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}


else {
echo "ERROR";
}


// close connection
mysql_close();


?>

[/b]
boyis92
ah i like
artlover
Thank you for the Script and your time, Appreciate your time.
α∂αмяoss
QUOTE(CyrusWu @ May 3 2007, 11:55 PM) *
Nice one! Too bad i have an better copy of it.


Was that really needed? If you dont have anything constructive to say than why post at all? Anyways. Im loving the script so far. Will do a great job for anyone. Keep it up bigwink.gif
curthard89
not a tutorial to is it really, people get mis conception and just say, add this to this page, add that to that, wheres the days of step 1, 1st we add this line which does blablabla lol
Varis
wow, it works well smile.gif thankx.
CSB
heres a random edit page I created due to bordem victory.gif untested.

SQL
<?php
include('dbconnect.php'); //connects to database

//select the table
$result = mysql_query("select * from newscms order by id desc LIMIT 5 ");

//grab all the content from the table
while($r = mysql_fetch_array($result))
{
$id = $r['id'];
$title = $r['title'];
$date = $r['date'];
$user = $r['user'];
$icon = $r['icon'];
$message = $r['message'];

}

// the input fields for editing

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h1>Edit Tutorial #<?php echo $row[0]; ?></h1>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<h2>Title</h2>
<input name="title" type="text" value="<?php echo $title ?>" size="50"><br>
<h2>Date</h2>
<input name="date" type="text" value="<?php echo $date ?>" size="50">
<h2>User</h2>
<input name="user" type="text" value="<?php echo $user ?>" size="50"><br><br>
<h2>Icon</h2>
<input name="icon" type="text" value="<?php echo $icon ?>" size="50"><br><br>
<h2>Message</h2>
<textarea name="message" cols="80" rows="30"><?php echo $message ?></textarea>

<p>
<input name="submit" type="Submit" value="Submit">
<input type="reset">
</p>
</form>

<?php

{
$id = $_POST['id'];
$title = $_POST['title'];
$date = $_POST['date'];
$user = $_POST['user'];
$icon = $_POST['icon'];
$message = $_POST['message'];

$query = "UPDATE tutorials SET name = '".$id."', keywords = '".$title."', avatar = '".$date."', category = '".$user."', skill = '".$icon."', brief = '".$message."' WHERE id = '".$id."'";
$result = mysql_query($query)or die(mysql_error());
echo 'News item successfully edited.';
}


mysql_close($connection);
?>


ps. nice script smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.