Entries tagged "PHP"

Understanding How the Modulus Operator Works

I've used the modulus operator in PHP for a while, but I didn't truly understand how it works. Now, I know how to divide. I also know that the modulus operator returns the remainder when one number is divided by another. However, my calculation just didn't match the returned result. So let's look at where I went wrong. [Continue reading]

Build HTML Tables Dynamically with PHP Part 3: CSS Alternative

Using HTML tables for design is typically frowned upon in the Web community and the last two posts talked about using tables to display pictures and names in rows of three. For those unfamiliar with the CSS alternative, I didn't want to leave you hanging. So let's look into solving our problem with CSS. [Continue reading]

Build HTML Tables Dynamically with PHP Part 2: Simplify with array_chunk()

Last week we built an HTML table on the fly using PHP. The process required a counter for monitoring which column was being displayed and some tests to determine when to add row tags. Let's look into simplifying the process with a built-in PHP function called array_chunk(). [Continue reading]

Build HTML Tables Dynamically with PHP Part 1

There is a built-in PHP function which would have really been useful back when using HTML tables for design was popular. Instead of setting up counters and testing when to add the opening and closing tags, we could have just read in the data and displayed it. Let's pretend we're back in the heyday of table hacking and look how the function saves time. [Continue reading]

Using phpMyAdmin as a Checklist for Columns Used in a Script

One of my many coding habits I've been changing is the use of select all (SELECT *) in MySQL queries. Scripts usually don't need all the columns and grabbing unnecessary data reduces efficiency. Fixing the references usually involves me digging through the code, remembering the column names actually being used, and updating the query. This method works well when there are only a few columns to worry about. It gets more complicated with more columns or when they're used throughout a large script. That's where phpMyAdmin comes to the rescue. [Continue reading]

Populate Forms Which Have Been Disconnected from Google Docs

Due to popular demand, we're going to look at pre-populating Google forms using PHP. Last week's post showed how forms can be liberated from Google Docs. Since they're disconnected, we won't be able to depend on the standard method provided by Google. We're responsible for developing our own solution. Luckily, this is fairly straight forward with server-side languages like PHP. [Continue reading]

Avoid MySQL Queries Within Loops

One consistent piece of advice that's given on PHP help forums is that queries shouldn't be executed within loops. However, they don't usually provide an alternate means for accomplishing the task at hand. So I wanted to share a solution which fixed one of my scenarios. [Continue reading]

Avoid Broken Website Links by Eliminating Whitespace in GET Variables

When naming pages for a website, its best practice to avoid spaces. Well the same goes for the GET variables (and their values) passed via the URL. Website addresses will likely work with or without spaces. However, that doesn't mean they will be free from problems. Visitors may copy a website address into an e-mail message, for example, and those spaces prevent e-mail clients like Microsoft Outlook from converting the address into a clickable link. At least a clickable link that works. [Continue reading]

Prevent Duplicate Items from Being Added to a Shopping Cart with the Header Redirect

For the longest time, I've been ignoring header redirects. They typically seem to be used for processing form requests. One page collects the form data and if everything checks out, the script redirects visitors to a confirmation page. Otherwise, the visitor is redirected back to the form. This is a perfectly acceptable way to build forms, but my preference has been to keep everything under the same page. Using a series of if constructs makes this possible. Lately, however, the header redirect has been sneaking into my scripts. In case I'm not the only one shying away from the header() function, let's talk about one possibility. [Continue reading]

Remove White Space from Form Data to Avoid Potential Headaches

When processing form submissions, it's important to remove white space before and after each value. Those extra spaces will likely go unnoticed by most visitors, but luckily this won't be a problem most of the time. In some cases, however, those spaces can lead to a lot of confusion. [Continue reading]