Part One
Part Two
Part Three

11:13am
I’ve got a few functions of the administration dashboard set up now! Because I built the PHP functions to be so abstract, it was easy to set up an insertProduct and updateProduct function for use with the dashboard. I call the functions with AJAX.

The insertProduct is incredibly simple, thankfully.

[php]
function insertProduct ($product ){
$productValues = array(
‘name’ => $product[‘name’],
‘description’ => $product[‘description’],
‘price’ => $product[‘price’],
‘quantity’ => $product[‘quantity’],
‘image’ => $product[‘image’],
‘sale’ => $product[‘sale’],
‘id’ => $product[‘id’]
);
insert(‘products’,$productValues);
}
[/php]

Next up, the updateProduct function:

[php]
function updateProduct( $product ){
$productValues = array(
‘name’ => $product[‘name’],
‘description’ => $product[‘description’],
‘price’ => $product[‘price’],
‘quantity’ => $product[‘quantity’],
‘image’ => $product[‘image’],
‘sale’ => $product[‘sale’],
‘id’ => $product[‘id’]
);
update(‘products’,$productValues);
}
[/php]

The next thing I’ll be adding is the deleteProduct AJAX call – should be easy enough, since I already have the PHP deleteProduct method complete.

11:45am
Woohoo! The admin page can now add, update and delete products from the products table.
Here’s what the stack looks like.

Each product has a delete button, which has an onclick event of deleteProduct($product[6]); where $product[6] is the item ID.

deleteProduct jQuery
[js]
function deleteProduct( id ){
console.log(‘deleting product ‘ + id);
ajax(‘POST’, id, ‘assets/php/formsubmit.php?method=deleteProduct’);
}
[/js]

This then calls formsubmit.php and passes the parameter deleteProduct, which triggers a switch inside and calls the PHP function deleteProduct(id) in the main library.

[php]
//deleteProduct
function deleteProduct ( $id ){
error_log("deleting product $id");
return delete(‘products’, $id );
}

//main delete function, if you remember from part two
function delete( $tableName, $value ){
switch($tableName) //switch tablename so we can pick products or cart table
{
case "products":
$tbl = ‘products’;
break;
case "cart":
$tbl = ‘cart’;
break;
}
error_log($value);
//no switch for property here. We only want to delete by ID for safety
$db = dbConnect(); //connect to our database
if( strcmp($value, ”) != 0 ){
error_log("deleting $value from cart");
$query = "DELETE FROM $tbl WHERE id=:value";
$stmt = $db->prepare($query);
//bind params
$stmt->bindValue(‘:value’, $value);
}
else{ //$value is empty; therefore we clear the table
error_log("clearing the cart");
$query = "DELETE FROM $tbl";
$stmt = $db->prepare($query);
}
//execute
try{
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
}
catch(Exception $e){
return $e;
}
}
[/php]

Next steps: I realized I should add a boolean onSale column in the products table to determine if a product is on sale. Hopefully, this won’t screw with my existing code too much.

Also, I added a cool code syntax highlighter to my WordPress installation called SyntaxHighlighter Evolved! It’s pretty great. I’ll be going back and highlighting the code in my posts once I’m done.

12:17pm
It wasn’t too difficult to add that onSale boolean, thankfully. For the administration dashboard, I’ve added these cool toggle switches.

2:26pm
I’ve set the homepage to automatically display the items that are on sale. Now, I’m going to be adding MixItUp to my catalog page to allow for quick and pretty item sorting. I’ve used it before, and really enjoy the results.

3:45pm
I’ve set up the admin dashboard page to be a bit prettier thanks to Bootstrap’s tab-panes class. Also, I added a little styling to the footer (thanks, StackOverflow!).
[css]
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 60px;
background-color: #f5f5f5;
}
[/css]

Leave a Reply

Your email address will not be published. Required fields are marked *