Hi there. I'm Matt. I don't do marketing to make money. I make money to do marketing1.

12 October, 2009 | Comments

This is the 4th and final post in a series on how to make a website. Why am I doing this? For marketers, the knowledge required to create a well-designed site is buried in hard-to-understand tutorials, books, and forums for computer programmers. Knowing nothing about web programming one year ago, this series explains everything that I’ve learned.

We’re making a webpage identical to this one. Before we finish our site, make sure your code matches what we’ve done so far here in lesson #2.

Entering Data

We need a way to enter calorie data into the website. We’ll use a simple form for this:

<form action="<?php $self ?>" method="GET">
<input name="usercalorie" type="text" cols="20" value="Total Calories. e.g., 400" id="input"/>
<input type="hidden" name="foodid" value="<?php echo $foodid; ?>" />
<input type="hidden" name="actualcalorie" value="<?php echo $actualcalorie; ?>" />
<input type="hidden" name="image" value="<?php echo $image; ?>" />
<input name="send" type="hidden" />
<input type="submit" value="submit" />
</form>

This form creates a text box for users to enter calorie data. When user clicks “submit,” it will pass the data from the text box along with a few other variables that we had assigned previously in PHP.

The first line states that the method is “GET.” All data from the form will be sent via the URL. So for the next line we have a text box named “usercalorie.” This means that if someone types “10″ in the text box, it will be appended to the URL after clicking “submit” as http://www.mdaniels.com/calories?usercalorie=10. Another example: the ID for the food presented is appended as http://www.mdaniels.com/calories?usercalorie=10&foodid=3. We will append on the data for the actual calorie total and image name.

Example: here’s what happens after someone guesses a calorie total:

http://www.mdaniels.com/calories/?usercalorie=400&foodid=6&actualcalorie=1265&image=cheesecake.jpg&send=

We want to take this data and covert into variables. 400 needs to turn into $usercalorie. 1265 as $actualcalorie. This way we can compare the variables and interpret the user’s guess.

To do this, we need the following code:

if(isset($_GET['send'])) {
$usercalorie = htmlspecialchars($_GET['usercalorie']);
$actualcalorie = htmlspecialchars($_GET['actualcalorie']);
$foodid = htmlspecialchars($_GET['foodid']);
$image = htmlspecialchars($_GET['image']);
}

We add this to the top of our code, right after connecting to the database. First, it checks IF the “send” variable is set in the URL. In the example URL above, you can see that it is there at the end of the URL (…&send=). PHP will then convert each of the items into variables. For example, “image=cheesecake.jpg” becomes “$image.”

If done correctly, the user’s information is stored as variables: $usercalorie (what they guessed), $actualcalorie (the actual total calories of the food), $foodid (the food id number), $image (food’s image name).

Manipulating User Data

Let’s capture all of this information in our database. Recall that we created a table to do just that–columns for foodid, usercalorie, and date. After assigning the user’s data to variables, insert into the database:

$sql = "INSERT INTO submit SET foodid='$foodid', usercalorie='$usercalorie', date=now()";
if (@mysql_query($sql)) {
} else {
exit('<p>error adding to rankings table: ' . mysql_error() . '</p>');
}

This is a simple SQL statement. We are inserting data into the table “submit” (created in lesson #2), setting each column to the respective variable created earlier. The “date” item uses a simple command that inserts into the table the current time/date.

databasetable

By capturing this data we can calculate an “average user guess,” as shown on the live version of the calorie website.

Check your code so far here. That’s pretty much the general structure of the site! Not sure what just happened? Here’s a quick map of our code:

Major Coding Steps:

map

This is a cycle–every time the user submits something, the site reloads. So as soon as a user submits a calorie total, the website reloads, checks if the user submitted something (yes), captures the user’s guess, grabs one new food row, and prompts the user to evaluate the new food item.

Web Design: CSS

We completed the major steps of the site. How do we make it look pretty? This is where CSS comes in. Check out the Tizag CSS guide here. There is also another amazing CSS tutorial from Smashing Magazine here.

Within the <head> tag, add style tags: <style></style>. Within these tags we can assign CSS to our code. Suppose we want to add some flavor to our form. First add a <div> to the form. The div tag is purely aesthetic. It assigns style to all items within the div tag.

<div id="form">
<form action="<?php $self ?>" method="GET">
....
</form>
</div>

I assigned the div an id=”form”. Within the style tags I write:

#form{
margin-top:10px;
border:10px solid black;
color:#464646;
font-size:25px;
}

By giving our <div> the id “form,” we can refer to it in our CSS as #form, assigning it a few design elements, like font-size and a border. Check out this awesome border for our form:

border

I will not write all of the CSS that I used for the calorie site. It is far too complex to explain in a tutorial, but I do encourage you to check out the source code at the end of this tutorial and work out how to create something just like it.

Finishing Up

You should now have a working website. Add a few more food items to the site using the MySQL database.

The source code at the bottom adds much more code. It includes all of the copy and the text fields for “your guess” and “average guess.” It also includes the code required to grab a random food item rather than starting in the same spot every time the site loads.

So that’s it. Here’s the entire source code for the website.

Click here to comment.
6 October, 2009 | Comments

This is the 3rd post in a series on how to make a website. Why am I doing this? For marketers, the knowledge required to create a well-designed site is buried in hard-to-understand tutorials, books, and forums for computer programmers. Knowing nothing about web programming one year ago, this series explains everything that I’ve learned.

We’re making a webpage identical to this one. My last post explained how to create a MySQL Database. Now we’ll connect that database to our website and write some code! If you haven’t already, definitely review this intro to PHP tutorial from Tizag.

Connecting to your Database

Recall from the last lesson that you created your database–you gave it a name, username, and password. We’ll use this information so that we can access the database on our website.

Using your text editor, create a document called ‘db.php’. Enter the following–my notes are in [brackets].

<?php
$host = '[your server name]'; //remember that this was automatically messaged to you when you created your database
$username = '[your username]'; //your username assigned to your database
$password = '[password]'; //your password assigned to your user & database
$database = '[database name]'; //My database was called 11159_tutorial23

Save this file on the same level as ‘index.php’ using your FTP program. Once you have this file created, go back to the document that we started in the last post, ‘index.php’.

Right under the body tag, paste the following text:

include('db.php');

$dbcnx = @mysql_connect ("$host", "$username", "$password");
if (!$dbcnx) {
echo '<p> cannot connect </p>';
exit ();
}

if (!@mysql_select_db('[your database name]')) {
exit('<p>Unable to connect to database at this time.</p>');
}

Note: replace the red text with your database name. We are connecting the ‘index.php’ file to the database information. The function include() takes db.php (created earlier) and copy and pastes all of the file’s information as if it were right into the document.

The $dbcnx and if() strings are PHP for connecting to a MySQL database. Don’t worry about the specifics–just know that this code is required when creating a database-driven website.

If everything is working, your website will load normally and not show an error (i.e., “unable to connect to database at this time.”).

At this point, confirm that your site has the right text by clicking here.

Now we need to get a food item from the database to display on the webpage. Remember in lesson #2 that we entered a pizza slice’s info into the database. We want to pull this item and all of the data in each of the columns. Using a bit of SQL, we select an ID number from the food table.

$query = "SELECT * FROM food LIMIT 1";
$result = @mysql_query("$query") or die('<p>There was an unexpected error grabbing studies from the database.</p>');

This reads, “Select all items (*) from the table food and limit to one row (LIMIT 1 in the SQL). Stored in $result is our food item row. It includes the image name, description, and calorie total.  We now need to get this info out of $result.

while ($row = mysql_fetch_array($result)) {
$foodid = stripslashes($row['ID']);
$actualcalorie = stripslashes($row['actualcalorie']);
$image = stripslashes($row['image']);
$description = stripslashes($row['description']);
}

This function (while) takes the variable $result and parses out the column information from our database. For example, the pizza row contained the data “248″ for the “actualcalories” column. We grab this data by assigning it to the $actualcalorie variable using the above code.

Now we have the pizza’s row data all assigned to variables. Its ID is in $foodid. Its calorie total in $actualcalorie. $image is the pizza slice image name. Description is $description. Let’s display these variables on your webpage:

echo $description;
echo "<img src='http://www.yourwebsite.com/images/".$image."' />";

This displays the pizza slice’s description and image on your website. The image, however, will not work because you have not uploaded it to your webhost’s servers. Download this image of a pizza slice and save it as pizza.jpg. Remember, when entering the data into your database, this is what we entered into the pizza row in the image column. Upload it to your website using FTP (refer to lesson #1 for directions). Create a folder under your domain name called “images” and upload the pizza image there.

folder

Try loading your site, you should see the displayed pizza description and image.

That’s it for now. Not sure if you got everything? See all of the code that I covered thus far here. Make sure you change all references to mdaniels.com.

Click here to comment.

Hey. I'm Matt Daniels

I'm a B-School grad and brand-strategy consultant for Prophet in NYC. I write about digital biznass, with the occasional review of Gossip Girl.


You can also hit me up at matt [at] mdaniels.com