The Final Part: Pulling the Information Out

The first thing we are going to do is create 2 files in our root folder: index.php and database.php.

Open up database.php first. It should be a blank file; no HTML or anything. When writing PHP code, it should be contained within the tags. That way, the server knows to read it as PHP code and process it as such. We are going to reference our database and be able to work with it in our PHP file.

Type in the following code:

1.$host = "localhost";
2.$user = "root";
3.$password = "";
4.$db = "tut-portfolio";
5. 
6.$mysqli = new mysqli($host, $user, $password, $db) or die('Could not connect to database.');

In PHP, the $ sign represents a variable. A variable is a method of storing data; in our case, we are simply storing strings, and indicating the start of a new function.

What we have done is opened up a connection to our MySQL database. The $host is the location of the database; in our case, since it’s on our own computer, we will reference it as localhost. The $user is the username to the database; if you are using WAMP, it will be “root” unless you changed it. The $password is obviously the password; for WAMP, the default password is nothing, so leave it as such. Finally, the $db is the name of the database. I called mine tut-portfolio. It must be exactly the same as the one you created in phpmyadmin.

The last line is where the magic happens. We start a new variable called mysqli; within it, we are using the MySQL Improved method to make our database connection. You may have seen people using mysql_connect, but this method is semantically correct and a much better way. We indicate the start of the mysqli class by writing “new mysqli”. Within brackets, we call the variables we set earlier; when starting a new mysqli class, the parameters go in the following order: host, username, password, database. You could either type it in within strings (in quotation marks), or use variables like we did. I prefer the variable method; it allows you to use those variables multiple times and update them easily. The point of PHP is to simplify the amount of code you have to write; if you are typing in the same strings over and over again, when you go back to change even one character, you’ll have to do it multiple times. For example, what if you change the password? Having a variable will allow you to easily update the entire code by changing it in one place.

The or die() statement will only be shown if the script fails. If, for whatever reason, you are unable to connect to the database, the string, “Could not connect to the database.” will be shown. It will also stop the script from going any further. This helps as it points out exactly what’s wrong with your page according to your specifications, and tells the viewer that something is not right rather than just displaying a blank page.

Always remember that each PHP statement must be ended by the semi-colon, otherwise the browser will throw an error.

Alright, we’re done with database.php, so close that up. Now, open index.php. Since we’re going to be combining PHP and HTML on this page, you should start off with the standard HTML code (doctype, title declaration, etc.) On top of the doctype declaration, we will start our PHP code. Start your PHP tags () and enter this code:

1.require 'database.php';
2. 
3.$query = 'SELECT image, description, name, link, technologies_used FROM clients' or die('Could not complete query.');
4.$results = $mysqli->query($query) or die('Could not process results.');

Alright, let’s analyze this code. The “require” call selects the file we specify (in this case, database.php) and makes it absolutely necessary for the script to run. There are other calls like this, such as include, but I am using require because without connecting to the database, our page won’t run. If the script doesn’t find database.php, the script dies right there and does not execute any further.

1.$query = 'SELECT image, description, name, link, technologies_used FROM clients' or die('Could not complete query.');

We then create a new variable called query. A query is a statement that will be given to the MySQL database, and will perform the actions we tell it to. This is SQL code, by the way. Everything you see in caps is a command we’re giving to the database, basically. We’re telling it to SELECT certain fields from the database. There are two ways to select fields; you can either type their names out if you only want some of them, or you can use the asterisk (*) to select all the fields. We’re selecting all the fields, however, we’re listing them all out. Why? Because if we happen to use the asterisk and add, for example, 6 more fields to the table that don’t pertain to our script, those fields would be selected too. Listing them all out is not only a safer method in terms of overloading the script with data, but it is also easier to see which fields are being used.

The FROM states which table we’re selecting the fields from, which is called clients for us. We then end our query variable with an or die statement, just to stop the script from executing further if this part fails. Basically, what this query will do is get all the items under the fields we specified.

1.$results = $mysqli->query($query) or die('Could not process results.');

After our query variable, we start another variable called results. This variable will contain the results from our query. But we have only created a query variable; we have not actually sent it to the database. The way we do that is access our database connection variable ($mysqli) and use an arrow (->) to access the query parameter. Basically, what we’ve done is accessed our database, and used the query call to send our $query variable to the database. If that doesn’t work, the script dies and tells you that it could not process the results.

That’s it for our PHP ABOVE the HTML. This accesses the database, sends the query, and keeps the the results in a variable to be used later.

We have our results…but they’re not displayed on the page. The way we’re going to make them display is by using PHP code within our body. I created a div called page-wrap (just to center the page; I’m not getting into the CSS bit) and put the following PHP code in there:

01.if($results) {
02. 
03.while($row = $results->fetch_object()) {
04.$name = $row->name;
05.$image = $row->image;
06.$description = $row->description;
07.$link = $row->link;
08.$technologies_used = $row->technologies_used;
09. 
10.echo "<div class='project'>";
11. 
12.echo "<img src='$image' alt='$name' />";
13. 
14.echo "<div class='projects_links'>";
15.echo "<ul>";
16.echo "<li><a class='link'' href='#'>View Details</a></li>";
17.echo "<li><a class='link' href='$link'>View Site</a></li>";
18.echo "</ul>";
19.echo "</div>";
20. 
21.echo "<div class='technologies_used'> Technologies Used: $technologies_used </div>";
22. 
23.echo "<img src='images/projectSeparator.jpg' alt='project_separator' class='project_separator' />";
24. 
25.echo "<div class='project_title'>$name</div>";
26. 
27.echo "<div class='project_desc'>$description</div>";
28. 
29.echo "</div>";
30.}
31.}

Alright, this last chunk of code is pretty repetitive. We’re using an if statement to run our code; if there is data in our $results variable, then all this code will be run. The while statement will repeat itself as long as there are rows of data in the $results variable; we take each row one at a time and put all the data in a temporary variable called $row. This data will be contained as an array, which, in layman’s terms, contains multiple objects in 1 variable. I’ll get into more of that in another tutorial. Then, we fetch that data using the fetch_object command, and set each of the fields from the database to a variable. The way we target each field is using the $row->[field name] method; when using arrays, you call the variable first, use and use an arrow to the specify the object you want to use.

After we store each field as a variable, we begin to echo out our information. I’m not going to get into the HTML because it’s the way Total Pixelation Studios’ portfolio page is done (and it’s irrelevant to this tutorial). Basically what I’ve done is create a div with a class of “project”, and echoed out all our information within it.

The echo command basically spits out everything in front of it as HTML.

1.echo "<img src='$image' alt='$name' />";

We echo out an image tag, but instead of statically referencing an image, we put our variable there. Every row of data has a different image; since this echo statement is in the while loop, it will show a different image for every project. We also set the alt tag to the name of the project.

After that, I’ve basically done the same thing for each line. For the project links, I’ve made a simple unordered list navigation and put in the $link variable for the location. The project title is stored in the $name variable, so within the “project_title” div, I put wrote $name, rather than an actual name.

Now echoing all this information out may not be the best way to do this, but it sure is the simplest. I wanted to keep this tutorial simple for the newcomers to PHP, so there you go.

After the while loop is complete, it will run over and over again until there are no rows of data left within the $results variable. This way, it displays all the data that was retrieved from the MySQL database.

With a little bit of CSS, you can have a portfolio page that looks like this:

final
Final Product

I hope this helped you on your trek to learn PHP, and get coding!

Tools for Web Design and Development

I thought that an introduction to tools used in web development would be a good start for a first article. This will be good for beginners as a starting point, and will give you an idea of what you need to get started with your first website. By the way, this is a long article, so make sure you have time on your hands. So, let’s get started!

Graphics Software

It is virtually impossible to create a good website without developing a visually appealing layout first. Whether it is something simple such as a two-column layout or a complex website with loads of JavaScript and CSS hacks, you need to first develop a layout. The premier program for this is Adobe Photoshop. As one of the most versatile graphic-editing programs available, Photoshop is the most commonly used software by web designers. It’s multitudinous features allow for endless possibilities by simply tapping into your creative pool. Only problem is that, at $699, Photoshop is a little bit towards the expensive side. Although there are other programs such as GIMP available, I stand by my opinion the Photoshop is the best one. Seeing as it’s the only program I ever used, I’m just going to describe it a little here.

There are lots of tools in Photoshop that help to create a layout. Here are the most useful ones; you will get a more hands-on look at them during my first screencast, which will teach you how to create a layout in Photoshop.

    • Marquee Tool: The marquee tool allows you to make selections of varying shapes. Although it may seem basic, combining selections (adding and subtracting) can allow for some cool concepts in your design.
    • Move Tool: The move tool is self-explanatory; it may seem silly to mention it here, but it is obviously an important asset in Photoshop.
    • Brush/Pencil Tool: The brush tool is primarily used for design; you can select brushes of various styles (ex. grunge, tech, splatter, etc) and make use of them in your design. With the large amount of brushes available to download, the possibilities of creating a unique layout are endless. The pencil tool is a non-anti-aliased version of the brush tool – that’s not to say it isn’t useful, though. Use the pencil tool to add simple touches such as a 1 pixel line separator to your website; it may seem simple, but it does loads to enhance your design.
    • Fill/Gradient Tool: The fill tool fills a selection with a certain color; it is present even the simplest programs such as Paint, so I’m almost 100% sure you would know what it is. The Gradient tool fills a selection with a gradient, which is a gradual change from one color to another. Using subtle gradients can greatly liven up your design.
    • Eraser Tool: The eraser tool erases things. I don’t think I need to say more about it.
    • Pen Tool: The pen tool cannot simply be described in one little blurb. It is a versatile and important tool in the field of design, but it comes with the price of being extremely complex. You can use it to cut a person out of an image, make selections, use it to create gradual strokes, and much more. I might just dedicate a whole article to this tool.
    • Text Tool: The text tool allows you to write stuff. It comes with the option to write vertical/horizontal text. You can use the pen tool to create a line on which you can write text, and the text will follow the line exactly.

Those are just some of the tools that you see when you open Photoshop. There are many more features that are regularly used when creating a website. I suggest that you just play around in Photoshop, follow tutorials (resources at the end of this article), and get familiar with the program. Also learn the shortkeys, they can save you TONS of time.

Code Editor

The code editor is the most important program that is used for web development. After all, it doesn’t matter if you have a layout if you can’t put it into a browser, does it?

There are hundreds of code editors out there, and each of them advertise themselves as the best. I’ve experimented with many code editors, but I have found Aptana to be my favorite. It’s code hinting is very useful, and you can theme it to change its appearance. Of course, it comes with code highlighting, as do most editors. Also, it does not come installed with support for every single language; it has plugins that you can download to install support for the script you want. For example, let’s say you need to code for the iPhone. All you have to do is download their Apple iPhone plugin, and you have full support for that particular language. Yeah, it’s that easy. Best of all, it’s free.

Another code editor that I avidly used was Adobe Dreamweaver. Unlike Aptana, it has built-in support for all scripting languages. It doesn’t have the theme capabilities of Aptana, which I, personally, don’t like. I prefer working on a dark background as opposed to a white one; it’s a little less harrowing to the eyes. Dreamweaver also has a large snippet library, which comes useful when you’re still learning all the various codes. This is an expensive program at $399, but it is worth the money, considering its multitudinous features.

A great article was written by Andrew Burgess of Nettuts+. It outlines 18 IDEs for Windows, Mac, and Linux. You can view it here.

Other Programs/Tools/Tips

You need an FTP program to upload files onto your server. I recommend using FileZilla; it is a free program that uses minimal RAM and works efficiently. It looks ugly, but does that really matter?

As a rule of thumb, always design for Firefox. After you finish your coding for Firefox, go into other browsers (ex. Chrome, Safari, Opera, IE8, IE7, and the dreaded IE6) and fix anything that’s wrong there. If need be, you can use a browser-specific stylesheet. Anyways, there are two extremely useful addons for Firefox that you MUST have. Trust me, when you run into trouble, you will be able to easily figure the problem out using these two addons. The first one, the Web Developer Toolbar, has so many useful features that it isn’t possible to list them all here. Using it, you can view your site (and other people’s websites as well) with JavaScript disabled, cookies turned off, styles disabled (I haven’t found a reason to do that yet, though), and more. You can also inspect the various elements of a page by pressing Ctrl+Shift+F. This highlights each element and provides all the information you can imagine about it. You can also edit CSS live using this addon! Check it out here, and download it. Now.

The second addon is called Firebug, which allows you to inspect elements, just like the Web Developer Toolbar. However, Firebug pinpoints the area in the HTML where that element is present, as well as all the locations it is styled in ALL attached stylesheets. Talk about making life easier. You can edit and monitor HTML, CSS, Javascript, and a lot more. It also comes with a console that can be used for debugging (that’s much more advanced stuff), and a lot of other stuff.

A program that makes my web design life way easier is called FasterFox. Contrary to what it sounds like, it is NOT a Firefox addon. This program allows you to write snippets of code that are activated when a certain key combination is pressed. It makes writing code a lot faster. For example, if I type in “img” and press tab, it writes the whole HTML code for embedding an image. You have to write all the snippets yourself, but it’s a great time saver.

Alright, I think that I have covered a fair amount here. I am going to create a screencast (or a series, I haven’t decided yet) in which I will create a website in Photoshop, and code it using HTML/CSS. Hopefully, I can show you some features of everything I mentioned here in that screencast. I originally intended for this to be a short article and make a screencast introducing all these programs, but I saw no reason for that once I started writing this. Anyways, I hope this helped you out, and check back soon for that screencast!