Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

April 22, 2014

StackOverflow Like Pagination

Some while ago, I built a pagination system in PHP to display a long list of pagination links. The pagination system showed at most 5 links at a time and additional links were represented by ellipsis (...). However, I was not able to deal with certain edge cases, so I drew some inspiration from the pagination system used on StackOverflow to tackle them. This is what the end result looked like:

Pagination using ellipsis

November 4, 2012

How to Fix the Typical "My PHP Script Does Not Work" Problem

A PHP script that "does not work" because of an error will emit error messages such as "use of undefined variable", "an error in MySQL query", "permission denied", "parse error", etc. If you do not see an error when you should, it could mean that PHP is configured to suppress errors; a common practice in production environments. Developers who use AMP stacks unknowingly have PHP setup this way, and when they run into "does not work" problems and see no errors, they start asking silly questions.

The environment in which you develop code must be setup differently. Here is what you need to do in order to make PHP more verbose about errors.

August 21, 2012

Traversing Hierarchy Tree Using PHP: Adjacency List Model

Relational databases represent data as rows and columns inside a table; however, this representation is not suitable for hierarchical data. Certain models are available that allow you to store hierarchical data in these databases. One such method is the Adjacency List model. This article shows you how to manipulate adjacency list implementation of hierarchical (tree) data using PHP.

Hierarchical data consists of elements that have parent-child relationships. It can be visualized as a tree-like structure where each node can have one parent and many children. A node that does not have a parent is called root node while a node that does not have any children is called leaf node. Any connecting node is called branch node.

Adjacency list model is a naive solution used (unknowingly) by majority of programmers. In this implementation, each row stores a reference to its parent. This simplifies most database operations except for SELECTing a subtree (remember that the depth of the subtree is unknown).

Since a tree is a recursively defined data structure, it can be traversed naturally using recursion. The code demonstrated in this article uses (i) one query (ii) array to store all results (iii) index for faster iteration (iv) recursion. It does not use references or multiple queries.

June 25, 2011

Google Maps Marker Generator with Dynamic Labels

Few hours ago I stumbled upon a question on stackoverflow.com where someone asked if it was possible to overlay a number on top of a custom Google map marker? Something like creating:

Labelled Google map markers (example 1)

from this:

Blank Google map marker (example 1)

without using Photoshop!

March 31, 2011

Redirecting Your Visitors to Your Preferred Domain

A typical website is configured to serve the same content via two addresses: example.com and www.example.com. This redundancy will increase the chances of someone reaching that website directly; but poses a problem for search engines, and you.

Search engines treat a domain and its subdomains as different websites altogether. This means that content available on example.com and www.example.com will be indexed and ranked separately. Thus, a page from your website could be presented multiple times in search results, but not as high as it could possibly get. Moreover, search engines weigh the importance of a page in terms of the number of backlinks to that page. If websites backlink to YourAwesomeArticle.html in multiple ways (specifically by omitting or including the www prefix), this weight will be distributed between these URLs.

February 17, 2011

URL Rewriting On IIS/PHP Using PATH_INFO

IIS does not directly support URL rewriting. For this purpose you need extensions such as Helicon Tech ISAPI Rewrite, Ionic's Isapi Rewrite Filter or URL Rewrite Module for IIS 7.0. Without such extensions, PHP applications that capitalize on URL rewriting may work on IIS with not-so-clean URLs; or may not work at all. Yet some versatile applications can use PATH_INFO style clean URLs when mod_rewrite or its equivalent functionality is not available.

In the following article I will show you how to test and fix your PHP installation to make URLs containing PATH_INFO work as expected.

April 14, 2010

Running a Background Process from PHP on Linux

While PHP is capable of handling just about anything, there are times when you must take the load away from PHP and hand it over to another program. Processing huge amount of data is one case; examples include sending thousands of emails, moving thousands of files from one location to another, batch processing megabytes of image data or converting an uploaded video from one format to another.

Scenarios like these pose a few challenges. You cannot expect the user to wait for too long. You cannot handover the program control to a time consuming script. And you will have to deal with time limits both on the server and client sides.

A solution to these problems is to spawn a background process and transfer the processing load to it. A background process returns control immediately to the calling program and continues.

July 14, 2009

Export MySQL Data to CSV Using PHP

15 minutes into writing this article, I realized that PHP 5.1 now includes the fputcsv function that was missing from earlier versions of PHP. It formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle. However, it is not properly documented what exactly is the "newline" character; is it CR, LF or CRLF.

I have a habit of re-inventing the wheel, so I've put together a substitute for this function that echoes the CSV data instead of writing to a file and (tries to) closely follow the recommendations described in RFC 4180.

June 22, 2009

Generate Random Strings Using PHP

This is a simple yet highly customizable function that allows you to generate random strings using PHP. The random strings can be used for various purposes, including:

  • Login Systems
    • Random password for new registrations
    • Random password for users requesting password reset
  • Referral Codes
  • Promotional Codes
  • Primary Keys for tables where using integers as primary keys is not desirable

The following code example generates an 8 characters long random string that contains 1 digit:

April 13, 2009

Crop-To-Fit an Image Using ASP/PHP

Image Cropping refers to the removal of the outer parts of an image to improve framing, accentuate subject matter or change aspect ratio. In this article I will demonstrate a technique that combines resizing and cropping to fit an image in given dimensions. You can, for example, use this technique to generate square thumbnails for images with arbitrary dimensions and aspect-ratio.

November 4, 2008

Watermark Your Images with another Image Using PHP and GD Library

The following code sample demonstrates the use of GD library to watermark images on the fly. The method demonstrated here to watermark an uploaded image is to overlay the original image with another image, preferably a transparent PNG image.

PHP provides a rich set of functions to create and alter images on the fly. These functions require the GD library, which is bundled with PHP since version 4.3.

November 3, 2008

Watermark Your Images with Text Using PHP and GD Library

The following code sample demonstrates the use of PHP and GD library to watermark images on the fly. The method demonstrated here to watermark an uploaded image is to render a partially transparent line of text over the original image.

This example relies on the GD library, which is bundled with PHP since version 4.3.

Note: You may also want to have a look at a variation of this example that watermark images using image overlays.

October 28, 2008

Resize Images Using PHP and GD Library

PHP provides a rich set of functions to generate and manipulate images. Most of these functions require the GD library, which is bundled with PHP since version 4.3.

The following code sample demonstrates the use of GD library to resize an image to generate its thumbnail. The script first saves the uploaded image in a specific location, it then generates a thumbnail version of the image and finally saves the generated thumbnail image in JPEG format.