Skip to content

Instantly share code, notes, and snippets.

@devjosh12
Created March 18, 2015 10:52
Show Gist options
  • Save devjosh12/9b1e4c34a5d1808a35ff to your computer and use it in GitHub Desktop.
Save devjosh12/9b1e4c34a5d1808a35ff to your computer and use it in GitHub Desktop.
neweditthumb
<?php
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Product</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<style>
#container {
border: 1px solid #000;
float: left;
height: 50px;
overflow: hidden;
padding-bottom: 5px;
padding-left: 0;
padding-top: 5px;
position: relative;
width: 69px;
}
#main {
float: left; margin-left: 10px; padding-left: 0px;
}
.del {
cursor: pointer; margin-top: 10px; position: relative; top: 20px; margin-left: 10px;
}
.photoimg {
bottom: 0;
cursor: default;
left: 0;
opacity: 0.001;
position: absolute;
top: 0;
width: 69px;
height:60px;
}
</style>
</head>
<body>
<center><a href="products.php">Show Products</a>
<?php
$id = $_GET['id'];
include "config.php";
$query = "SELECT * FROM products where id='$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
<?php if($row['id']) { ?>
<table border=1 align="center">
<tr>
<td align=center>Edit Product</td>
</tr>
<tr>
<td>
<form method="post" id="thumb" name="myForm" action="" enctype="multipart/form-data" onsubmit="validate()">
<table>
<input type="hidden" id="hiddcat" name="id" value="<?php echo "$row[id]"; ?>">
<tr>
<td>Product title</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[title]"; ?>" onkeyup="checkc('cat_title');" id="cat_title">
</td>
</tr>
<tr>
<td>Product Image</td>
<td>
<input type="hidden" name="hidmainimg" value="<?php echo $row['filename'];?>">
<?php echo "<img src=\"thumbnail/" . $row['filename'] . "\" width='80' height='80' style='margin-left:30px;' />"; ?>
</td>
</tr>
<tr>
<td>Change Product Image</td>
<td>
<input type="file" name="proimage" style="width:75px;border:none;">
</td>
</tr>
<tr>
<td>Product Thumbnails</td>
<td>
<?php $thumbsql = "SELECT * from thumbnails where prod_id='".$id."'";
$thumbarray = mysql_query($thumbsql);
while($trow = mysql_fetch_assoc($thumbarray)) { ?>
<div id='main'><div id='container' title='Choose File'><img id="zoom<?php echo $trow['imageid']; ?>" value='<?php echo $trow['imagename']; ?>' src="thumbnail/<?php echo $trow['imagename']; ?>" style="margin-left:10px;width:50px;height:50px;"/>
<input name='photoimg' style="z-index:1;" alt="<?php echo $trow['imagename']; ?>" title="<?php echo $trow['imagename']; ?>" class='photoimg' rel="<?php echo $trow['imageid']; ?>" id="custom_img<?php echo $trow['imageid']; ?>" type='file'>
</div>
<span class='del' style='cursor:pointer;' id='<?php echo $trow['imagename']; ?>' rel='<?php echo $trow['imageid'];?>'>X</span></div>
<?php
}
?>
<?php
$thumbsql = "SELECT imagename from thumbnails where prod_id='".$id."'";
$thumbarray = mysql_query($thumbsql);
$hidthumbarray = array();
while($hidobject = mysql_fetch_array($thumbarray)) {
$imgname = $hidobject['imagename'];
array_push($hidthumbarray,$imgname);
}
?>
<?php //print_r($hidthumbarray); ?>
<?php $values = json_encode($hidthumbarray); ?>
<?php $newval = str_replace("[","",$values); ?>
<?php $newval1 = str_replace('"',"",$newval); ?>
<?php $newval2 = str_replace(']',"",$newval1); ?>
<?php //echo $newval2; ?>
<input type="hidden" name="thumbhidden" value="<?php echo $newval2; ?>">
<div id="preview"></div>
</td>
</tr>
<?php if(!empty($row['thumbnails'])) { ?>
<tr>
<td>Delete Images</td>
<td>
<a style="cursor:pointer;"><div style="width:60px;" class="delete_class" id="<?php echo $id; ?>">Delete All</a>
</td>
</tr>
<?php } ?>
<script type="text/javascript">
$(document).ready(function() {
$('.photoimg').live('change', function() {
var imgid = $(this).attr('rel');
var allowedExtension = ['jpeg','jpg','gif','png'];
var fileExtension = document.getElementById('custom_img'+imgid).value.split('.').pop().toLowerCase();
for(var index in allowedExtension) {
if(fileExtension === allowedExtension[index]) {
isValidFile = true;
break;
} else {
var isValidFile = false;
}
}
if(isValidFile == false) {
alert('Allowed Extensions are : *.' + allowedExtension.join(', *.'));
//alert(isValidFile);
}
else {
$("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
var imgid = $(this).attr('rel');
var fileInput = $("#custom_img"+imgid)[0];
var file = fileInput.files[0];
var proid = <?php echo $id; ?>;
var formData = new FormData();
formData.append('file',file);
formData.append('image_id',imgid);
formData.append('prod_id',proid);
$.ajax({
url: "ajaximage.php",
data: formData,
contentType: false,
processData: false,
cache: false,
type: "POST",
success:function(data){
if(data) {
$('#zoom'+imgid).attr("src",data);
jQuery('#preview').css("display","none");
}
}
});
return false;
}
});
});
</script>
<script>
$(document).ready(function(){
$(".del").click(function(){
var del_id = $(this).attr('id');
var imgid = $(this).attr('rel');
$.ajax({
type:'POST',
url:'delete_singlethumbnail.php',
data:'delete_name='+del_id+'&img_id='+imgid,
success:function(data) {
if(data) {
alert(data);
location.reload();
} else { // DO SOMETHING }
}
}
});
});
});
</script>
<script>
$(document).ready(function(){
$(".delete_class").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'delete_thumbnails.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) {
alert(data);
location.reload();
} else { // DO SOMETHING }
}
}
});
});
});
</script>
<tr>
<td>Add Product Thumbnails</td>
<td>
<input type="file" id="file" name="file[]" multiple="multiple" style="width:75px;border:none;"/>
<script>
$(document).ready(function() {
$(".add").click(function() {
$('<div><input name="file[]" type="file" style="width:75px;border:none;"><span class="rem"><a href="javascript:void(0);">Remove</span></div>').appendTo(".contents");
});
$('.contents').on('click', '.rem', function() {
$(this).parent("div").remove();
});
});
</script>
</td>
</tr>
<tr>
<td width="100"></td>
<td>
<div class="contents"></div>
<a href="javascript:void(0);" class="add">Add More</a></td>
</tr>
<tr>
<td>Content</td>
<td>
<textarea name="prod_content" value="<?php echo "$row[prod_content]"; ?>" onkeyup="checkc('prod_content');" id="prod_content"><?php echo "$row[prod_content]"; ?></textarea>
</td>
</tr>
<tr>
<td style="width:120px;float:left;">Select Category</td>
<td><?php $categoryList = fetchCategoryTree();?>
<select name="pcategory" id="pcat">
<?php foreach($categoryList as $cl) { ?>
<option value="<?php echo $cl["cat_id"] ?>" <?php if($cl['cat_id']==$row['category']) echo 'selected="selected"'; ?>><?php echo $cl["title"]; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td align="right">
<input name="update" type="submit" value="Submit" class="submit"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php } else {
echo "<br>";
echo "No Record Found";
}
?>
</body>
</html>
<?php
function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {
if (!is_array($user_tree_array))
$user_tree_array = array();
$sql = "SELECT cat_id, title, is_parent FROM categories WHERE is_parent = $parent ORDER BY cat_id ASC";
$query = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query);
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_object($query)) {
$user_tree_array[] = array("cat_id" => $row->cat_id, "title" => $spacing . $row->title);
$user_tree_array = fetchCategoryTree($row->cat_id, $spacing . '&nbsp;&nbsp;', $user_tree_array);
}
}
return $user_tree_array;
}
?>
<script type="text/javascript">
// Form validation code will come here.
function validate()
{
if( document.myForm.name.value == "" || document.myForm.cat_title.value.trim().length == 0)
{
alert( "Please provide title!" );
document.myForm.name.focus() ;
return false;
}
if( document.myForm.content.value == "" || document.myForm.prod_content.value.trim().length == 0)
{
alert( "Please provide content!" );
document.myForm.content.focus() ;
return false;
}
var allowedExtension = ['jpeg','jpg','gif','png'];
var fileExtension = document.getElementById('file').value.split('.').pop().toLowerCase();
var isValidFile = false;
for(var index in allowedExtension) {
if(fileExtension === allowedExtension[index]) {
isValidFile = true;
break;
}
}
if(!isValidFile) {
alert('Allowed Extensions are : *.' + allowedExtension.join(', *.'));
return false;
}
return isValidFile;
}
//return(true);
function checkc(id)
{
if(document.getElementById(id).value == '')
{
window.alert ("This field cant be left empty");
return true;
}
else
{
return false;
}
}
</script>
<?php if(isset($_POST['update'])) {
error_reporting(0);
$id = $_POST['id'];
$prod_title = $_POST['name'];
$prod_content = $_POST['prod_content'];
$pcategory = $_POST['pcategory'];
$thumbhidden = $_POST['thumbhidden'];
//print_r($_POST);
if(is_uploaded_file($_FILES['proimage']['tmp_name'])) {
if($_FILES['proimage']['name'] != "")
{
$save_path = 'thumbnail/';
$filemain = str_replace(" ", "_", $_FILES['proimage']['name']);
move_uploaded_file($_FILES['proimage']['tmp_name'],$save_path.time()."_".$filemain);
$imagename = $save_path.time()."_".$filemain;
$newname = str_replace("thumbnail/","", $imagename);
}
} else {
$newname = $_POST['hidmainimg'];
}
/////////////////////////////////////////////////////////////////
$destpath = "photos/";
while(list($key,$value) = each($_FILES["file"]["name"])) {
if(!empty($value))
{
if (($_FILES["file"]["type"][$key] == "image/gif") || ($_FILES["file"]["type"][$key] == "image/jpeg") || ($_FILES["file"]["type"][$key] == "image/png"))
{
$source = $_FILES["file"]["tmp_name"][$key] ;
$filename = str_replace(" ","_",$_FILES["file"]["name"][$key]);
move_uploaded_file($source, $destpath.time() . $filename) ;
//echo "Uploaded: " . $destpath . $filename . "<br/>" ;
//thumbnail creation start//
$tsrc="thumbnail/".time().str_replace(" ","_",$_FILES["file"]["name"][$key]); // Path where thumb nail image will be stored
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail imaage
/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
$add=$destpath . $filename;
if($_FILES["file"]["type"][$key]=="image/gif"){
//echo "hello";
$im=ImageCreateFromGIF($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
if (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
}
//chmod("$tsrc",0777);
////////// end of gif file thumb nail creation//////////
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail imaage
////////////// starting of JPG thumb nail creation//////////
if($_FILES["file"]["type"][$key]=="image/jpeg"){
echo $_FILES["file"]["name"][$key]."<br>";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
//////////////// starting of png thumb nail creation //////////
if($_FILES["file"]["type"][$key]=="image/png"){
//echo "hello";
$im=ImageCreateFromPNG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagepng")) {
//Header("Content-type: image/png");
ImagePNG($newimage,$tsrc);
}
//////////////// starting of png thumb nail creation //////////
if (function_exists("imagejpeg")) {
//Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
}
$imagenames = array();
foreach($_FILES['file']['tmp_name'] as $key => $val ) {
$thumbfilename = $_FILES['file']['name'][$key];
$newimagenames = str_replace(" ","_",$thumbfilename);
array_push($imagenames,$newimagenames);
}
$astring = implode("','", $imagenames);
$astringTwo = "'".$astring."'";
// thumbnail creation end---
} else {
echo "<script>alert('Please Upload Image formats jpg, jpeg, gif and png only.')</script>";
}
} else {
$queryup = "UPDATE products
SET title='".htmlspecialchars($prod_title)."',
prod_content='".htmlspecialchars($prod_content)."',
category='$pcategory',
filename='$newname'
WHERE id='$id'";
$resultup = mysql_query($queryup);
if($resultup){
//echo("data is updated");
header("location:edit_product.php?id=".$id);
exit();
}
}
} // while loop ends
$query = "UPDATE products
SET title='".htmlspecialchars($prod_title)."',
prod_content='".htmlspecialchars($prod_content)."',
category='$pcategory',
filename='$newname'
WHERE id='$id'";
$result = mysql_query($query);
foreach($imagenames as $image) {
$thumbsql = "insert into thumbnails (prod_id,imagename) values ('".$id."','".time().$image."')";
$result2 = mysql_query($thumbsql) or die ("Could not insert data into DB: " . mysql_error());
}
if($result || $result2){
//echo("data is updated");
header("location:edit_product.php?id=".$id);
exit();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment