It's free and you have access to premium codes!
Welcome back! Please login to your account.
Don't worry, we'll send you a message to help you to recover your acount.
Please check your email for instructions to activate your account.
Written by 21 December 2012
If your site is getting updated frequently with new posts, or if you have a site that has new articles everyday, you need to show them in different ways to the visitors. A common way is a random news box. This script let you to add news and shows one of them randomly. It also has a list of all news that get shown by click. Note that it is enough to put your MySQL connecting information in the script, it will make its database and table automatically. So it is easy to use and Enjoy!
<!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.hawkee.com, found at www.phpfreecode.com-->
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
function setup(){
echo('
<p style="color: #008000; text-align: left; font-size: 15pt;"">-Automatic setup is started...</p>
');
global $host,$username,$password,$link;
//$link=mysql_connect($host, $username, $password);
$sql= 'CREATE DATABASE news';
if (!mysql_query ($sql, $link)) die('
<p style="text-align: center; font-size: 20pt;"><span style="color: #FF0000;">Failed to
create database! </span><br><span style="font-size: 12pt;">>>Please check the parameters and database server<<</span></p>
');
$sql = "CREATE TABLE `news`.`news` (
`ID` INT NOT NULL ,
`Title` TEXT NOT NULL,
`Detail` TEXT NOT NULL
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;";
mysql_query($sql) or die('Setup Failed');
echo('
<p style="color: #008000; text-align: left; font-size: 15pt;"">-Automatic setup completed successfully. Your Database is ready!</p>
');
}
$link=mysql_connect($host, $username, $password) or die(mysql_error());
$db_name="news";
$result=mysql_select_db($db_name);
if(!$result){
setup();
}
$sql="SELECT * FROM news";
$result=mysql_query($sql);
if ($result) $cntr=mysql_num_rows($result)+1; else $cntr=0;
if ($cntr>1) $r=rand(1,$cntr-1); else $r=0;
if (isset($_POST['Add'])){
$id=$cntr;
$title=$_POST['title'];
$detail=$_POST['detail'];
$sql="INSERT INTO news(ID,Title,Detail)VALUES('$id', '$title', '$detail')";
$result=mysql_query($sql);
if (!$result) die (mysql_error());
echo "<p style='color: #008000; text-align: left;font-size: 15pt'>-Your News is Added successfully.</p>";
$r=$id;
}
if (isset($_GET['no']))$r=$_GET['no'];
?>
<table style="border: 1px solid #000000;width: 836px" align="center">
<tr>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 281px; color: #2214B9;border-style: solid;border-width: 1px;">
<strong>News Titles</strong></td>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; color: #2214B9;border-style: solid;border-width: 1px; width: 382px;">
<strong>Random News</strong></td>
</tr>
<tr>
<td style=" padding:5px; font-family: 'Times New Roman', Times, serif;font-size: 14pt;text-align:left ;width: 281px; color: #2214B9;border-style: solid;border-width: 1px;">
<?php
$sql="SELECT * FROM news";
$result=mysql_query($sql);
if ($result) {
$cntr=mysql_num_rows($result)+1;
while($rows=mysql_fetch_array($result)){
echo "<a href='".$_SERVER['PHP_SELF']."?no=".$rows['ID']."'>".$rows['Title']."</a><br>";
}
if (mysql_num_rows($result)==0) echo"<p style='text-align: center; color: #B21212;'><strong>No News Found</strong></p>";
}else {
echo"<p style='text-align: center; color: #B21212;'><strong>No News Found</strong></p>";
}$cntr=1;
$t='No News Found';
$d='';
if($r!=0){
$sql="SELECT * FROM news WHERE ID='$r'";
$result=mysql_query($sql);
$news=mysql_fetch_array($result);
$t=$news['Title'];
$d=$news['Detail'];
}
?>
</td>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt; color: #2214B9;border-style: solid;border-width: 1px; width: 382px; padding:5px">
<p style="text-align: center; color: #B21212;"><strong><?php echo $t ?></strong></p>
<span style="text-align: left; color: #B21212;"><?php echo $d ?></span>
</td>
</tr>
</table>
<br>
<form action="" method="post" >
<table style="border: 1px solid #000000;width: 600" align="center">
<tr>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 151px;color: #2214B9;border-style: solid;border-width: 1px;">
<strong>News NO.<?php echo $cntr?></strong></td>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;color: #2214B9;border-style: solid;border-width: 1px;">
<table style="width: 100%">
<tr>
<td style="width: 68px; font-size: 14pt;">Title:</td>
<td>
<strong>
<input name="title" style="width: 320px; font-size: 14pt; font-family: 'Times New Roman', Times, serif; font-weight: bold;"></strong>
</td>
</tr>
<tr>
<td style="width: 68; font-size: 14pt;" valign="top">Details:</td>
<td><strong>
<textarea name="detail" style="width: 320px; height: 101px; font-size: 14pt; font-family: 'Times New Roman', Times, serif;" cols="20" rows="1"></textarea></strong></td>
</tr>
</table>
</td>
</tr>
</table>
<p style="text-align: center">
<input name="Add" style="width: 118px; height: 63px; font-size: 30pt; font-family: 'Times New Roman', Times, serif; color: #19B024;" type="submit" value="Add"></p>
</form>
<div style="text-align: center; height: 15px">
<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>
Comments
Here you can leave us commments. Let us know what you think about this code tutorial!