Random Picture Box

Written by @m_k_amin 30 December 2012

It is a nice idea to have a picture gallery in your site. You can put a box on your home page which randomly shows a picture to the visitors. This script do it for you, by using both MySQL Database (for storing pictures information) and File Uploading (for add new pictures) procedures. It is just a one page automatic script and all it needs is your Database server information. Enjoy!

Code Snippet:

                                                
                                                <!-- 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 Pics';
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;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
');
$sql = "CREATE TABLE `Pics`.`pic` (
`ID` INT NOT NULL ,
`Title` TEXT NOT NULL,
`Pic` 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="Pics"; 
$result=mysql_select_db($db_name);
if(!$result){
	setup();
}
$sql="SELECT * FROM pic";
$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'];
	if ($_FILES['picf']['error']==0) {
          $tmpFile= $_FILES['picf']['tmp_name'];
          $newFile=getcwd().'/'.$_FILES['picf']['name'];
          if (move_uploaded_file($tmpFile, $newFile)){
          	$path=$_SERVER['HTTP_HOST'].'/'.dirname ($_SERVER['REQUEST_URI']).'/'.$_FILES['picf']['name'];
          	echo '
            <p style="text-align: center;font-size: 17pt;color: #008000;"><strong>Your Picture is Added Successfully.</strong></p>					';
           	$sql="INSERT INTO pic(ID,Title,Pic)VALUES('$id', '$title', '$path')";
			$result=mysql_query($sql);
			if (!$result) die (mysql_error());
			$r=$id;
          }else
          	echo '
            <p style="text-align: center;font-size: 17pt;color: #FF0000;"><strong>An Error Occurred While Uploading.</strong></p>
			';
	}else
          echo 'An Error occuerd NO.: '.$_FILES['userfile']['error'].'<br>';
}

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>Pictures</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 Picture</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 pic";
$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 Picture Found</strong></p>";
}else {
	echo"<p style='text-align: center; color: #B21212;'><strong>No Picture Found</strong></p>";
}$cntr=1;
$t='No News Found';
$d='';
if($r!=0){
	$sql="SELECT * FROM pic WHERE ID='$r'";
	$result=mysql_query($sql);
	$rpic=mysql_fetch_array($result);
	$t=$rpic['Title'];
	$d=$rpic['Pic'];
}

?>
		</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>
		<br><br><span style="text-align: left; color: #B21212;"><?php if ($d!='') echo "<img alt='The picture is not available.' src='http://$d' height='200' width= '300'>" ?></span>
		</p></td>
	</tr>
</table>
<br>
<form action="" method="post" enctype= "multipart/form-data" >
<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: 295px; 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">
					Picture:</td>
					<td>
					<input name="picf" accept="image/*" style="font-size: 14pt; font-family: 'Times New Roman', Times, serif; width: 279px;" type="file"></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>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2