Pages

Tuesday, March 25, 2014

Create a Random Photo Gallery-part 4 (Creating the Photo Gallery)

This video series about How to display a gallery with specified  number of photos randomly, each time  you load the webpage

PHP + HTML , Wampserver


This show how to

fetch and display photos in a Gallery

Final Code for Index.php







<?php
 $db_con=mysqli_connect('localhost','root','','photos') or die("Connection error");

  $form = '<form id="myfrom" enctype="multipart/form-data"method="POST" action="photoDB.php">';
 $form .= 'Chose photos to Upload :';
 $form .= '<input type="file" name="photoUp" accept="image/*" required/>';
 $form .= '<p><button type="submit" value="upload ">Upload </button>';
 $form .= '</form>';

  $gallery='';
 $max=8;
 $galleryPic=array();
 $sql="SELECT photoname from photo ORDER BY RAND() LIMIT $max";
 $query=mysqli_query($db_con,$sql);
 while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
  array_push($galleryPic,$row['photoname']);
 }
 $galleryCount=count($galleryPic);
 if($galleryCount>$max){
  array_splice($galleryPic, $max);
 }
 foreach ($galleryPic as $key => $value) {

   $gallery .= '<img src="Photos/'.$value.'"/>';
 }
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8"/>
 <style>
  #base{height: 255px;width: 800px;background: #27ae60;display: block;float: left;margin: 0px 0px 0px 50px;}
  #base>#pictr{height: 255px;}
  #base>#pictr>img{width: 200px;height: 125px;}
 </style>
</head>
<body style="background:#303030;">
 <div id = "photoform" style="color:#fff;margin:20px 50px;" ><?php echo $form ; ?></div>
 <div id="base">
  <div id="pictr"><?php  echo $gallery; ?></div>
 </div>
</body>
</html>

No comments:

Post a Comment