function productInsert($product,$photo){ // {{{
global $db_username,$db_password,$db_server,$db_name;
$valid_keys = array(
'sku',
'pentad_sku',
'newsku',
'name',
'overview',
'experience',
'specifications',
'assembly',
'capactiy',
'application',
'additional',
'weight',
'true_cost',
'public_price',
'sale_price',
'keywords',
'category',
'list_weight',
'family'
);
$valid_empty = array_fill_keys($valid_keys, NULL);
$product = array_intersect_key(($product + $valid_empty),$valid_empty);
$required_keys = array(
'sku',
'name',
'family',
'weight',
'public_price'
);
foreach ($required_keys as $key) {
if ($product[$key] == NULL) {
$product['fail'][] = $key;
}
}
if ($product['fail']) {
return $product;
}
if ($query=mysql_query("SELECT id FROM products WHERE sku='$product[sku]'")){
$id_array=mysql_fetch_array($query);
$id=$id_array['id'];
}
#fix for preventing orphan products by checking if a requested family already has a parent
if ($query=mysql_query("SELECT parent FROM products WHERE family='$product[family]'")){
if(mysql_num_rows($query) == 0){
$parent="1";
}
}
#also added '$parent' at the end of VALUES block below, so the value gets passed into the db if it is set
#endfix
mysql_query("
INSERT
products (
id,
sku,
pentad_sku,
name,
overview,
experience,
specifications,
assembly,
capacity,
application,
additional,
weight,
true_cost,
public_price,
sale_price,
keywords,
category,
list_weight,
family,
parent
)
VALUES (
'$id',
'$product[sku]',
'$product[pentad_sku]',
'$product[name]',
'$product[overview]',
'$product[experience]',
'$product[specifications]',
'$product[assembly]',
'$product[capacity]',
'$product[application]',
'$product[additional]',
'$product[weight]',
'$product[true_cost]',
'$product[public_price]',
'$product[sale_price]',
'$product[keywords]',
'$product[category]',
'$product[list_weight]',
'$product[family]',
'$parent'
)
ON DUPLICATE KEY UPDATE
sku='$product[sku]',
pentad_sku='$product[pentad_sku]',
name='$product[name]',
overview='$product[overview]',
experience='$product[experience]',
specifications='$product[specifications]',
assembly='$product[assembly]',
capacity='$product[capacity]',
application='$product[application]',
additional='$product[additional]',
weight='$product[weight]',
true_cost='$product[true_cost]',
public_price='$product[public_price]',
sale_price='$product[sale_price]',
keywords='$product[keywords]',
category='$product[category]',
list_weight='$product[list_weight]'
family='$product[family]'
");
// echo mysql_error();
$num=1;
foreach($photo as $each => $values){
//echo "each=".$photo[$each]['tmp_name'];
if ($photo[$each]['tmp_name']){
$image_full = new Imagick($photo[$each]['tmp_name']);
if ($image_full->getImageWidth() >= "1000"){
$image_full->adaptiveResizeImage(1000,1000,true);
}
$im_thumb1 = new Imagick($photo[$each]['tmp_name']);
$im_thumb1->adaptiveResizeImage(64,39);
$im_thumb1->roundCorners(3,3);
$im_thumb1_sh = $im_thumb1->clone();
$im_thumb1_sh->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$im_thumb1_sh->shadowImage( 80, 1, 1, 2 );
$im_thumb1_sh->compositeImage( $im_thumb1, Imagick::COMPOSITE_OVER, 0, 0 );
$image_thumb1 = $im_thumb1_sh->clone();
$image_thumb1->colorFloodFillImage('#FFFFFF', 100, '#777777', 0, 0);
$image_thumb1->compositeImage($im_thumb1_sh, Imagick::COMPOSITE_OVER, 0, 0);
$image_thumb1->setImageFormat('jpeg');
$image_thumb1->flattenImages();
$im_thumb2 = new Imagick($photo[$each]['tmp_name']);
$im_thumb2->adaptiveResizeImage(134,88);
$im_thumb2->roundCorners(5,5);
$im_thumb2_sh = $im_thumb2->clone();
$im_thumb2_sh->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$im_thumb2_sh->shadowImage( 80, 1, 1, 5 );
$im_thumb2_sh->compositeImage( $im_thumb2, Imagick::COMPOSITE_OVER, 0, 0 );
$image_thumb2 = $im_thumb2_sh->clone();
$image_thumb2->colorFloodFillImage('#FFFFFF', 100, '#777777', 0, 0);
$image_thumb2->compositeImage($im_thumb2_sh, Imagick::COMPOSITE_OVER, 0, 0);
$image_thumb2->setImageFormat('jpeg');
$image_thumb2->flattenImages();
$im_thumb3 = new Imagick($photo[$each]['tmp_name']);
$im_thumb3->setImageBackgroundColor( new ImagickPixel( 'white' ) );
$im_thumb3->adaptiveResizeImage(199,136);
$im_thumb3->roundCorners(5,5);
$im_thumb3_sh = $im_thumb3->clone();
$im_thumb3_sh->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$im_thumb3_sh->shadowImage( 80, 1, 1, 5 );
$im_thumb3_sh->compositeImage( $im_thumb3, Imagick::COMPOSITE_OVER, 0, 0 );
$image_thumb3 = $im_thumb3_sh->clone();
$image_thumb3->colorFloodFillImage('#FFFFFF', 100, '#777777', 0, 0);
$image_thumb3->compositeImage($im_thumb3_sh, Imagick::COMPOSITE_OVER, 0, 0);
$image_thumb3->setImageFormat('jpeg');
$image_thumb3->flattenImages();
$connect = new mysqli($db_server, $db_username, $db_password, $db_name);
$queryline = "UPDATE products SET
image".$num."_full = ?,
image".$num."_thumb1 = ?,
image".$num."_thumb2 = ?,
image".$num."_thumb3 = ?
WHERE id = ?";
//echo $queryline;
if ($query = $connect->prepare($queryline)) {
$null = NULL;
$query->bind_param('bbbbi',$null,$null,$null,$null,$id);
//echo $id;
$query->send_long_data(0, $image_full);
//var_dump($image_full);
unset($image_full);
$query->send_long_data(1, $image_thumb1);
unset($image_thumb1);
$query->send_long_data(2, $image_thumb2);
unset($image_thumb2);
$query->send_long_data(3, $image_thumb3);
unset($image_thumb3);
$query->execute();
} else {
printf("Errormessage: %s\n", $connect->error);
printf("Errormessage: %s\n", $connect->sqlstate);
printf("Errormessage: %s\n", $connect->dump_debug_info);
}
}
$num++;
}
} //}}}
function productEdit($product = NULL, $fail=NULL){ //{{{
//var_dump($fail);
?>
<form method="post" enctype="multipart/form-data" action="/admin/product/insert">
<input type="hidden" name="sku" value="<? echo $product['sku'] ?>">
<label>SKU:</label><input type="text" name="sku" value="<? echo $product['sku'] ?>">
<label>Pentad SKU:</label><input type="text" name="pentad_sku" value="<? echo $product['pentad_sku'] ?>">
<p class="name">
<label>Name:</label>
<input type="text" name="name" value="<? echo $product['name'] ?>">
</p>
<? if ($fail && !$product['name']) { ?>
<p class="error">* Please enter a name before submitting.</p>
<? } ?>
<p class="overview">
<label>Overview:</label>
<textarea name="overview"><? echo $product['overview'] ?></textarea>
</p>
<? //if ($fail && !$product['overview']) { ?>
<!-- <p class="error">* Please enter a descriptive product overview before submitting.</p> -->
<? //} ?>
<p class="experience">
<label>Experience:</label>
<textarea name="experience"><? echo $product['experience'] ?></textarea>
</p>
<p class="specifications">
<label>Specifications:</label>
<textarea name="specifications"><? echo $product['specifications'] ?></textarea>
</p>
<p class="assembly">
<label>Assembly:</label>
<textarea name="assembly"><? echo $product['assembly'] ?></textarea>
</p>
<p class="capacity">
<label>Capacity:</label>
<textarea name="capacity"><? echo $product['capacity'] ?></textarea>
</p>
<p class="application">
<label>Application:</label>
<textarea name="application"><? echo $product['application'] ?></textarea>
</p>
<p class="additional">
<label>Aditional:</label>
<textarea name="additional"><? echo $product['additional'] ?></textarea>
</p>
<p class="weight">
<label>Weight:</label>
<input type="text" name="weight" value="<? echo $product['weight'] ?>">
</p>
<? if ($fail && !$product['weight']) { ?>
<p class="error">* Please enter a weight before submitting.</p>
<? } ?>
<p class="true_cost">
<label>True Cost:</label>
<input type="text" name="true_cost" value="<? echo $product['true_cost'] ?>">
</p>
<? if ($fail && !$product['true_cost']) { ?>
<p class="error">* Please enter a true cost before submitting.</p>
<? } ?>
<p class="public_price">
<label>Public Price:</label>
<input type="text" name="public_price" value="<? echo $product['public_price'] ?>">
</p>
<? if ($fail && !$product['public_price']) { ?>
<p class="error">* Please enter a public price before submitting.</p>
<? } ?>
<p class="sale_price">
<label>Sale Price:</label>
<input type="text" name="sale_price" value="<? echo $product['sale_price'] ?>">
</p>
<p class="keywords">
<label>Keywords:</label>
<input type="text" name="keywords" value="<? echo $product['keywords'] ?>">
</p>
<? if ($fail && !$product['keywords']) { ?>
<p class="error">* Please enter at least one keyword before submitting.</p>
<? } ?>
<p class="category">
<label>Category:</label>
<select name="category" value="<? echo $product['category'] ?>">
<?
$query = mysql_query('SELECT * FROM categories');
while ($current_row = mysql_fetch_array($query)){
?>
<option <? if ($product['category'] == $current_row['category']){ ?>selected="selected"<? } ?> value="<? echo $current_row['category']?>"><? echo $current_row['category_name'] ?></option>
<? } ?>
</select>
</p>
<? if ($fail && !$product['category']) { ?>
<p class="error">* Please choose a category before submitting.</p>
<? } ?>
<p id="list_weight">
<label>List Weight:</label>
<input type="text" name="list_weight" value="<? echo $product['list_weight'] ?>">
</p>
<p id="family">
<label>Product Family:</label>
<input type="text" name="family" value="<? echo $product['family'] ?>">
</p>
<label>Primary Product Photo:</label>
<div><img src="/images/products/category-name-or-keywords-here/1/full/<? echo $product['sku'] ?>.jpg"></div>
<p class="photo">
<label>Upload: Photo:</label>
<input type="file" name="photo1">
</p>
<label>Second Product Photo:</label>
<div><img src="/images/products/category-name-or-keywords-here/2/full/<? echo $product['sku'] ?>.jpg"></div>
<p class="photo">
<label>Upload: Photo:</label>
<input type="file" name="photo2">
</p>
<label>Third Product Photo:</label>
<div><img src="/images/products/category-name-or-keywords-here/3/full/<? echo $product['sku'] ?>.jpg"></div>
<p class="photo">
<label>Upload: Photo:</label>
<input type="file" name="photo3">
</p>
<label>Fourth Product Photo:</label>
<div><img src="/images/products/category-name-or-keywords-here/4/full/<? echo $product['sku'] ?>.jpg"></div>
<p class="photo">
<label>Upload: Photo:</label>
<input type="file" name="photo4">
</p>
<label>Fifth Product Photo:</label>
<div><img src="/images/products/category-name-or-keywords-here/5/full/<? echo $product['sku'] ?>.jpg"></div>
<p class="photo">
<label>Upload: Photo:</label>
<input type="file" name="photo5">
</p>
<input class="submit" type="submit" value="Submit to Database">
</form>
<?
} // }}}
function productValidate($product){ // {{{
$valid_keys = array(
'sku',
'pentad_sku',
'newsku',
'name',
'overview',
'experience',
'specifications',
'assembly',
'weight',
'true_cost',
'public_price',
'sale_price',
'keywords',
'category',
'family'
);
$required_keys = array(
'sku',
'name',
'weight',
'true_cost',
'public_price',
'keywords',
'category'
);
$valid_empty = array_fill_keys($valid_keys, NULL);
$product = array_intersect_key(($product + $valid_empty),$valid_empty);
foreach ($required_keys as $req) {
if ($product[$req] == NULL) {
return false;
}
}
return true;
}//}}}
function productFlag($post){ // {{{
$post['sku_list'] = preg_replace('/,$/','',$post['sku_list']);
$products = explode(',',$post['sku_list']);
foreach($products as $sku){
$parent_name='parent|'.$sku;
$featured_name='parent|'.$sku;
$enabled_name='enabled|'.$sku;
if ($post[$enabled_name]){
mysql_query("UPDATE products SET enabled=true WHERE sku='$sku'");
} else {
mysql_query("UPDATE products SET enabled=false WHERE sku='$sku'");
}
if ($post[$parent_name] && $post[$featured_name]){
mysql_query("UPDATE products SET parent=true,featured=true WHERE sku='$sku'");
} else if ($post[$parent_name]){
mysql_query("UPDATE products SET parent=true WHERE sku='$sku'");
} else if ($post[$featured_name]){
mysql_query("UPDATE products SET featured=true WHERE sku='$sku'");
} else {
mysql_query("UPDATE products SET parent=NULL,featured=NULL WHERE sku='$sku'");
}
}
}//}}}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE