Last active
August 29, 2015 14:17
-
-
Save devjosh12/96f355c02f7026be0c00 to your computer and use it in GitHub Desktop.
addpro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ob_start(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Add Product</title> | |
<!-- scripts --> | |
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<center><a href="view_categories.php">Show Categories</a> | |
<a href="products.php">View Products</a> | |
<a href="index.php">Add Category</a></center> | |
<center>Add Product</center> | |
<form method="post" name="myForm" action="<?php $_PHP_SELF ?>" enctype="multipart/form-data" onsubmit="return(validate());" style="margin-top:10px;"> | |
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center"> | |
<tr> | |
<td width="100">Product Title</td> | |
<td><input name="prod_title" type="text" id="prod_title" onkeyup="checkc('prod_title');" ></td> | |
</tr> | |
<tr> | |
<tr> | |
<td width="100">Product Image</td> | |
<td><input type="file" id="mainimg" name="proimage"> | |
<span id="error1" style="color:red;display:none;">Upload Image</span> | |
</td> | |
</tr> | |
<tr> | |
<td width="100">Product Thumbnails</td> | |
<td><input type="file" id="file" name="file[]" multiple="multiple"/> | |
<span id="error" style="color:red;display:none;">Upload thumbnails</span> | |
<script> | |
$(document).ready(function() { | |
var fileName = $("input[type='file']").val(); | |
//alert(fileName); | |
$(".add").click(function() { | |
$('<div><input name="file[]" type="file" ><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> | |
<!--<input type="submit" name="upload" value="Upload"/>--></td> | |
</tr> | |
<tr> | |
<td width="100">Product DOCUMENT in <strong>doc FORMAT</strong></td> | |
<td><input type="file" name="doc" id="doc" accept="application/msword"></td> | |
</tr> | |
<tr> | |
<td width="100">Product DOCUMENT in <strong>pdf FORMAT</strong></td> | |
<td><input type="file" name="pdf" id="pdf" accept="application/pdf"></td> | |
</tr> | |
<?php | |
function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') { | |
include('config.php'); | |
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 . ' ', $user_tree_array); | |
} | |
} | |
return $user_tree_array; | |
} | |
?> | |
<tr> | |
<td style="width:120px;float:left;">Select Category</td> | |
<td><?php $categoryList = fetchCategoryTree();?> | |
<select name="pcategory" id="pcat"> | |
<option value="0" selected="selected">Please Select</option> | |
<?php foreach($categoryList as $cl) { ?> | |
<option value="<?php echo $cl["cat_id"] ?>"><?php echo $cl["title"]; ?></option> | |
<?php } ?> | |
</select> | |
</td> | |
</tr> | |
<tr> | |
<td width="100">Product Content</td> | |
<td><textarea id="prod_content" name="prod_content" onkeyup="checkc('prod_content');"></textarea></td> | |
</tr> | |
<tr> | |
<td width="100"> </td> | |
<td> | |
<input name="addproduct" type="submit" id="add" value="Add Product"> | |
</td> | |
</tr> | |
</table> | |
</form> | |
</body> | |
</html> | |
<script type="text/javascript"> | |
<!-- | |
// Form validation code will come here. | |
function validate() | |
{ | |
if( document.myForm.prod_title.value == "" || document.myForm.prod_title.value.trim().length == 0) | |
{ | |
alert( "Please provide title!" ); | |
document.myForm.prod_title.focus() ; | |
return false; | |
} | |
var mainimg = $('#mainimg').val(); | |
if(mainimg=='') | |
{ | |
alert("Product Image cannot be empty"); | |
jQuery("#error1").css("display","block"); | |
return false; | |
} | |
var imgVal = $('#file').val(); | |
if(imgVal=='') | |
{ | |
alert("Product thumbnails cannot be empty"); | |
jQuery("#error").css("display","block"); | |
return false; | |
} | |
if( document.myForm.prod_content.value == "" || document.myForm.prod_content.value.trim().length == 0) | |
{ | |
alert( "Please provide content!" ); | |
document.myForm.prod_content.focus() ; | |
return false; | |
} | |
return(true); | |
} | |
function checkc(id) | |
{ | |
if(document.getElementById(id).value == '') | |
{ | |
window.alert ("This field cant be left empty"); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
$('#doc').on('change', function() { | |
myfile= $( this ).val(); | |
var ext = myfile.split('.').pop(); | |
if(ext=="doc"){ | |
return false; | |
} else { | |
alert("Please Upload only .doc format"); | |
} | |
}); | |
$('#pdf').on('change', function() { | |
myfile= $(this).val(); | |
var ext = myfile.split('.').pop(); | |
if(ext=="pdf"){ | |
return false; | |
} else { | |
alert("Please Upload only .pdf format"); | |
} | |
}); | |
</script> | |
<?php | |
/*if(isset($_POST['addproduct']) && is_uploaded_file($_FILES['file']['tmp_name'][0])) | |
{ | |
$prod_title = $_POST['prod_title']; | |
$prod_content = $_POST['prod_content']; | |
$prod_cat = $_POST['pcategory']; | |
$docname = $_FILES["doc"]["name"]; | |
$pdfname = $_FILES["pdf"]["name"]; | |
//$imagename = $_FILES['file']['name'][0]; | |
//echo $firstimage; | |
//print_r($_POST); | |
//die; | |
if($_FILES['file']['name'][0] != "") | |
{ | |
$filename = str_replace(" ", "_", $_FILES['file']['name'][0]); | |
move_uploaded_file($_FILES['file']['tmp_name'][0],"thumbnail/" . $filename); | |
move_uploaded_file($_FILES["doc"]["tmp_name"],"docs/" . $_FILES["doc"]["name"]); | |
move_uploaded_file($_FILES["pdf"]["tmp_name"],"pdfs/" . $_FILES["pdf"]["name"]); | |
echo "Stored in: " . "docs/" . $_FILES["doc"]["name"]; | |
echo "Stored in: " . "pdfs/" . $_FILES["pdf"]["name"]; | |
$sql = "insert into products (title, prod_content, category, filename, docname, pdfname, created_time, update_time) values ('".htmlspecialchars($prod_title)."', '".htmlspecialchars($prod_content)."', '$prod_cat','".$filename."','".$docname."','".$pdfname."', NOW(), NOW())"; | |
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); | |
if($result) { | |
header("Location: products.php"); | |
//exit; | |
} | |
} | |
else { | |
echo "Could not move the file."; | |
} | |
}*/ | |
?> | |
<?php | |
if(isset($_POST['addproduct'])){ | |
$prod_title = addslashes($_POST['prod_title']); | |
$prod_content = addslashes($_POST['prod_content']); | |
$prod_cat = $_POST['pcategory']; | |
$docname = $_FILES["doc"]["name"]; | |
$pdfname = $_FILES["pdf"]["name"]; | |
if($_FILES['proimage']['name']!= "") | |
{ | |
if (($_FILES["proimage"]["type"] == "image/gif") | |
|| ($_FILES["proimage"]["type"] == "image/jpeg") | |
|| ($_FILES["proimage"]["type"] == "image/png")) { | |
$mainimage = str_replace(" ", "_", $_FILES['proimage']['name']); | |
move_uploaded_file($_FILES['proimage']['tmp_name'],"thumbnail/" . $mainimage); | |
} else { | |
echo "<script>alert('Product Image Format should be in gif, jpg, jpeg or png')</script>"; | |
} | |
move_uploaded_file($_FILES["doc"]["tmp_name"],"docs/" . $_FILES["doc"]["name"]); | |
move_uploaded_file($_FILES["pdf"]["tmp_name"],"pdfs/" . $_FILES["pdf"]["name"]); | |
//echo "Stored in: " . "docs/" . $_FILES["doc"]["name"]; | |
//echo "Stored in: " . "pdfs/" . $_FILES["pdf"]["name"]; | |
} | |
$sql = "insert into products (title, prod_content, category, filename, docname, pdfname, created_time, updated_time) values ('".htmlspecialchars($prod_title)."','".htmlspecialchars($prod_content)."', '$prod_cat','".$mainimage."','".$docname."','".$pdfname."', NOW(), NOW())"; | |
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); | |
$prod_id = mysql_insert_id(); | |
$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 . $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 | |
echo $tsrc; | |
$thumbnail = str_replace("thumbnail/","",$tsrc); | |
$thumbsql = "insert into thumbnails (prod_id,imagename) values ('".$prod_id."','".$thumbnail."')"; | |
$result2 = mysql_query($thumbsql) or die ("Could not insert data into DB: " . mysql_error()); | |
$n_width=100; // Fix the width of the thumb nail images | |
$n_height=100; // Fix the height of the thumb nail imaage | |
$add=$destpath.$filename; | |
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 GIF thumb nail creation/////////// | |
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); | |
chmod("$tsrc",0777); | |
} | |
/////////////////////////////////////////////// Ending of GIF thumb nail creation/////////// | |
////////////// 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); | |
} | |
//////////////// ending of JPEG thumb nail creation ////////// | |
////////////// 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); | |
chmod("$tsrc",0777); | |
} | |
////////////// ending of PNG thumb nail creation////////// | |
if (function_exists("imagegif")) { | |
Header("Content-type: image/gif"); | |
ImageGIF($newimage,$tsrc); | |
} | |
if (function_exists("imagejpeg")) { | |
Header("Content-type: image/jpeg"); | |
ImageJPEG($newimage,$tsrc); | |
} | |
if (function_exists("imagepng")) { | |
Header("Content-type: image/png"); | |
ImagePNG($newimage,$tsrc); | |
} | |
} | |
else{echo "error in upload";} | |
} | |
} | |
$imagenames = array(); | |
foreach($_FILES['file']['name'] as $key => $val ) { | |
if (($_FILES["file"]["type"][$key] == "image/gif") || ($_FILES["file"]["type"][$key] == "image/jpeg") || ($_FILES["file"]["type"][$key] == "image/png")) | |
{ | |
$thumbfilename = $_FILES['file']['name'][$key]; | |
$newimagenames = str_replace(" ","_",$thumbfilename); | |
} | |
array_push($imagenames,$newimagenames); | |
} | |
/*foreach($imagenames as $imagename) { | |
$thumbsql = "insert into thumbnails (prod_id,imagename) values ('".$prod_id."','".time().$imagename."')"; | |
$result2 = mysql_query($thumbsql) or die ("Could not insert data into DB: " . mysql_error()); | |
}*/ | |
if($result && $result2) { | |
header("Location: products.php"); | |
exit; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment