Jump to content


Photo
- - - - -

News CMS Script


  • This topic is locked This topic is locked
26 replies to this topic

#1 d7x

d7x

    P2L Jedi

  • Twodded Staff
  • PipPipPip
  • 586 posts
  • Gender:Male
  • Location:Virginia
  • Interests:Life

Posted 20 September 2005 - 05:49 PM

Well in this tutorial i will show you how to create a news content managment system!
Lets get started


First for the SQL 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


<? 
$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.

<? 
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

<? 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 :D

Edited by Donna, 28 September 2005 - 02:39 PM.

  • AaMuckedGr likes this

#2 K.Schmidt

K.Schmidt

    Young Padawan

  • Members
  • Pip
  • 7 posts

Posted 28 September 2005 - 02:21 PM

Nice Script, works great, i like it :P

#3 _*Speed_*

_*Speed_*
  • Guests

Posted 29 September 2005 - 12:52 AM

This is pretty good, nice job.

#4 bluestriker

bluestriker

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Location:Illinois

Posted 12 October 2005 - 08:26 PM

The script is good...but you need to explain it more.

#5 austen

austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 12 October 2005 - 10:29 PM

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 :whistle: 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.

#6 d7x

d7x

    P2L Jedi

  • Twodded Staff
  • PipPipPip
  • 586 posts
  • Gender:Male
  • Location:Virginia
  • Interests:Life

Posted 13 October 2005 - 06:37 PM

yeh, i will soon, been busy glad ppl like it tho

#7 _*Raremandan_*

_*Raremandan_*
  • Guests

Posted 17 October 2005 - 04:22 AM

Awesome script, I was having trouble with the SQL bit but after reading many tutorials and this one is get it.

#8 blinek

blinek

    Jedi In Training

  • Members
  • PipPip
  • 377 posts
  • Location:Earth

Posted 23 November 2005 - 07:01 AM

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 :P

#9 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 02 December 2005 - 03:12 AM

Agree with Aeiko, edit and delete password, pluss a way to login with sessions. Then itīd be almost perfect

#10 bsf

bsf

    Young Padawan

  • Members
  • Pip
  • 7 posts

Posted 27 December 2005 - 04:23 PM

I am learning ALOT from this tutorial/script but i have a few questions :blush:

$title = [b]stripslashes(strip_tags(htmlspecialchars($_POST['title']));[/b]
What does the end of that code actually do? (the bold part :))

and,
$result = mysql_query("select * from newscms order by id desc limit 5");
What does this whole code stand for? :wacko:

#11 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 28 December 2005 - 07:16 AM

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:)

#12 chatmasta

chatmasta

    Young Padawan

  • Members
  • Pip
  • 2 posts
  • Location:&lt;a href="http://www.wickedfire.com"&gt;wickedfire&lt;/a&gt;

Posted 05 January 2006 - 01:42 PM

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. :)) 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.

<?
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 />";
} 

?>

<? 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();
	}
}
?>


#13 BackEnThaWomb

BackEnThaWomb

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 16 February 2006 - 11:11 AM

lookin good! thanks

#14 pihl

pihl

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 17 February 2006 - 01:07 PM

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)

#15 Morrigan

Morrigan

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 27 March 2006 - 05:23 PM

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.

#16 spleen

spleen

    Young Padawan

  • Members
  • Pip
  • 14 posts
  • Location:UK

Posted 19 February 2007 - 02:40 PM

Wow. Nice tut mate.

#17 ogrekey

ogrekey

    Young Padawan

  • Members
  • Pip
  • 31 posts

Posted 12 March 2007 - 08:21 PM

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.

Edited by ogrekey, 12 March 2007 - 08:22 PM.


#18 SiteReboot

SiteReboot

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:www.sitereboot.com

Posted 13 April 2007 - 02:18 AM

How could one modify this script to allow for catagories?

#19 CyrusWu

CyrusWu

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Niagara Falls, CA

Posted 03 May 2007 - 05:55 PM

Nice one! Too bad i have an better copy of it.

#20 abosaleh

abosaleh

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 12 May 2007 - 03:36 AM

here is the delete page for all and save as delete.php


<?
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>

Edited by abosaleh, 12 May 2007 - 03:37 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users