Outside on a beautiful day at RIT
We’re lucky enough to have another beautiful day in Rochester! It’s 80 degrees and feels like Summer still, so I’ll be working outside for a while today.

For my Server Programming course, I’ve started to put together a basic eCommerce website that I’ll be developing in PHP. Here is the requirements document. As I work through it, I’ll be writing my progress and linking to relevant materials for future reference. Here goes everything! The GitHub project is located here.

Part One
2:11pm
So far, I’ve begun the boilerplate for the front end and set up a LAMP server for testing. That tutorial is ultra helpful for making sure there aren’t any quirks in my LAMP setup.

2:25pm
After messing with this blog post for a bit, I’m going to start planning the structure of my PHP classes and database tables. Thankfully, the requirements document is descriptive and helpful, so I shouldn’t have much of a hard time with this.

2:38pm
I have two tables set up – Cart and Products. Cart has four columns – name (VARCHAR), description (VARCHAR), quantity (INT), and price (VARCHAR), as per requirements. Products has more – it includes the previous four columns, plus an image link (VARCHAR) and a sale price (VARCHAR). PHPMyAdmin is my tool of choice to set up these tables. Now, I’m going to populate the products table with some fake products for testing. That reminds me – I guess I should figure out what I’m going to pretend to sell!

2:41pm
I’ve decided I’ll pretend to sell wood furniture. I’ve developed a passion for woodwork lately, and have some pictures of my work on this blog, so it shouldn’t be too difficult. Plus, if I get some more good pictures, it’ll look great contrasted with a sleek, minimalist website.

2:50pm
Okay, I have three dummy items inserted into the products database – it’s tough to come up with convincing product descriptions! I’m heading home now.

4:56pm
I’ve gotten a few PHP functions written. Now, I’m able to list all my products and look up a product by ID. I’m using parameterized queries and OOP methodology to avoid SQL injections. At first, I got thrown by PHP’s bind_result function, but now it’s all working. Looking at the next steps, I’m just going to leave the client-side formatting pretty plain until I get everything planned out.

7:49pm
I finished the products listing, and then realized my database connect function wasn’t flexible enough to read the cart page, too. So I had to rewrite a bit.

Old:
[php]/* function connectWithStatement
* currently BROKEN
* Accepts: String $statement, int $numColumns
* Returns: Array of Products from database
*/
function connectWithStatement( $statement ) {
// Create connection
$con=mysqli_connect("localhost","root","mypasswordhere","ecom");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

/* prepare statement */
if ($stmt = $con->prepare( $statement )) {
$stmt->execute();

/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2, $col3, $col4, $col5, $col6, $col7);

$arr = array();
/* fetch values */
while ($stmt->fetch()) {
array_push($arr, array( $col1, $col2, $col3, $col4, $col5, $col6, $col7 ));
}
return $arr;

/* close statement */
$stmt->close();
}
/* close connection */
$con->close();
}
[/php]

The new way is much easier, and less lines of code. I’ll just use the prepared statements for insert functions, at least for now.
New:
[php]
/*
* function dbConnect
*
*/
function dbConnect( $statement ) {
$mysqli = mysqli_connect("localhost","root","mypasswordhere","ecom");
$res = mysqli_query($mysqli,$statement);
$row = mysqli_fetch_all($res);
return $row;
}
[/php]

Finishing up, here’s the commit!

Leave a Reply

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