Entries tagged "PHP"

Deactivate Code Automatically on a Test Server

When fixing a bug or adding a new feature to a website, it may be beneficial to test the changes on a development server before going live. The problem is that some aspects of the code probably shouldn't be active on the development server. For example, we probably don't Google Analytics tracking any of those visits. The code could be manually disabled during the test phase, but are we going to remember? Instead let's look into automating the process. [Continue reading]

Reuse GET and POST Instead of Duplicating Variables

When reading data from the GET or POST array, why do we assign the information to another variable? Why can't we just use the GET or POST variables? It's not like we unset those variables after the new ones are created. Instead of duplicating variables, consider keeping the ones created for us by PHP. [Continue reading]

Why Does the fetch_array() Function Even Exist?

When looping through a MySQL result set, which function do you use? My preference has always been for the fetch_array() function since it allows access to the data using the database column names. However, is that the most efficient function to use? [Continue reading]

Log Online Form Requests Part 2: Add the Field Separator Dynamically with implode()

When saving data to text files, each field is usually separated by something like the pipe character (|). The character is useful for importing and dividing the fields into a spreadsheet solution like Microsoft Excel. Normally, I add the character between each field manually, but there's an alternate using PHP's implode() function. [Continue reading]

Log Online Form Requests in Case Something Happens with the E-mailed Results

When building data-collection forms online, clients occasionally want the information sent directly to them via e-mail. Although it's convenient, there are issues with that delivery method. Spam filters may catch the messages. Messages may be overlooked and accidentally deleted due to the sheer number of e-mails a person receives per day. Or something could just go wrong with the mail server and the message never gets sent. With all the potential ways for e-mails to get lost, it's important to backup the information collected. One backup option is to store the data into a text file. [Continue reading]

End PHP Scripts Gracefully After a Failed Database Connection

Last week's post talked about externalizing the MySQL connection script for easier maintenance. The problem with the previous code is that it's not very user friendly. If the database connection fails, it just drops everything and displays an error. Well, there's probably other content on the web page that can be viewed without database access. The website navigation, for example, most likely doesn't require a database. The navigation will probably lead to other pages that don't need that database. To minimize the impact to the visitor, let's look at a more graceful solution for handling connection failures. [Continue reading]

Maintain Your Database Login Information with Ease by Externalizing the Connection Script

When working with databases like MySQL, a connection needs to be established before processing any queries. That connection script could be added to the top of every page needing access, but what happens when the database password needs updating. Who wants to update dozens (or thousands) of files? As a solution, let's look at externalizing the code that makes the connection and importing it instead. [Continue reading]

Rethinking the Structure of My Templates

Over the past few years I've been working on an initiative which shares a single template over several websites. The overall process has gone fairly well, but my standard process for building templates in PHP isn't holding so well. The number of files could use some pruning and there have been issues with variables and helper functions not being available when needed. Plus, it would be nice to have a common place where overall website setting could be changed to affect all pages. So it's time to rethink my template-building process. [Continue reading]

How to Avoid Conflicting Variable Names

On occasion, variables in PHP may conflict. This is easy to avoid when all the code appears in a single file. What happens when code is scattered throughout the website? Maybe there are variables tucked away in a template file which is brought in with a PHP include. How do you avoid conflicting variables within a file that's used by many other pages on the website? [Continue reading]

Remove Test Code Quickly with a Simple Dreamweaver Search

Adding test code throughout your scripts may be necessary for troubleshooting or adding new features, but how do you go about removing the code? In a previous post, a few techniques for locating the blocks of test code were discussed, but the code still needs to manually removed. Instead, let's tap into Dreamweaver's search for HTML tag feature. [Continue reading]