<?php require_once('../Connections/hilltop.php'); ?>
<?php $image = $_GET['images[]'];?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "mutiple_file_upload_form")) {
  $insertSQL = sprintf("INSERT INTO photos (gallery, imagename, `description`, title) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['selectgallery'], "text"),
                       GetSQLValueString($_POST['images2'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['title'], "text"));

  mysql_select_db($database_hilltop, $hilltop);
  $Result1 = mysql_query($insertSQL, $hilltop) or die(mysql_error());
}

mysql_select_db($database_hilltop, $hilltop);
$query_galleryselect = "SELECT * FROM gallerys";
$galleryselect = mysql_query($query_galleryselect, $hilltop) or die(mysql_error());
$row_galleryselect = mysql_fetch_assoc($galleryselect);
$totalRows_galleryselect = mysql_num_rows($galleryselect);
?>
<?php require("assets/wrapper/header.php"); ?>
<div align="center"><div class="mainlayout">
  <p></p>
<script type="text/javascript">

function add_file_field(){
var container=document.getElementById('file_container');
var file_field=document.createElement('input');
file_field.name='images[]';
file_field.type='file';
file_field.size='100';
container.appendChild(file_field);
var br_field=document.createElement('br');
container.appendChild(br_field);
}

</script>











<!--- first image1 block ---->
<!--- first image1 block ---->
<?php
if (isset($_POST['Submit'])) {
    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '../../hilltop_photos/full_image/'; //set upload directory
    /**
     * we get a $_FILES['images'] array ,
     * we procee this array while iterating with simple for loop
     * you can check this array by print_r($_FILES['images']);
     */
    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['images']['name'][$i];
            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                $number_of_moved_files++;
            }
 
        }
 
    }
    echo "Number of File fields created $number_of_file_fields.<br/> ";
    echo "Number of files submitted $number_of_uploaded_files . <br/>";
    echo "Number of successfully moved files $number_of_moved_files . <br/>";
    echo "File Names are <br/>" . implode(',', $uploaded_files);
}

?>








<div id="uploadbox">
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="mutiple_file_upload_form" id="mutiple_file_upload_form">
  <h1>&nbsp;</h1><div id="file_container">Please Select Gallery you want to upload photos to<select name="selectgallery">
    <?php
do {  
?>
    <option value="<?php echo $row_galleryselect['galleryname']?>"><?php echo $row_galleryselect['galleryname']?></option>
    <?php
} while ($row_galleryselect = mysql_fetch_assoc($galleryselect));
  $rows = mysql_num_rows($galleryselect);
  if($rows > 0) {
      mysql_data_seek($galleryselect, 0);
	  $row_galleryselect = mysql_fetch_assoc($galleryselect);
  }
?>
  </select>
    <input name="images[]" id="images" type="file" size="100" /><BR />

    <br />
    Description <BR />
    <input name="description" type="text" /><BR />
   
    Title<BR />
     <input name="title" type="text" /><BR />
     <input name="images2" type="hidden" value="<?php echo $image ; ?>" /><BR />
     <BR />
  </div>
  <a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
  <input type="submit" name="Submit" value="Submit" />
  <input type="hidden" name="MM_insert" value="mutiple_file_upload_form" />
</form>
  <p>&nbsp;</p>
</div>
</div>
<?php require("assets/wrapper/footer.php"); ?>
<?php
mysql_free_result($galleryselect);
?>
