Creating a Top 10 chart with less code
- Category: Personal Home Page
- Hits: 21774
I've decided to put something in here as it took me an age to find out how I could do it.
This is when using a MySQL query within a PHP script. The process is used often to do a statistics table or top ten chart of your data (eg. movies, music, etc).
My aim is to do the following:
- retrieve data from a table,
- count the number of times each data exists,
- sort it in reverse order so that the most frequent is at the top of the list
- print out each row with the number of times that particular data appeared in a row
My old method was to:
Outputting PHP files into different file formats
- Category: Personal Home Page
- Hits: 12240
I'm beginning a list as I've just spent an age trying to get PHP output to create a text file. Then my client showed me how she then opens the text file in Excel, so I said we could get the script to do that instead.
PHP to TXT
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename=foo.txt');
echo 'contents of file';
PHP to XLS
$export_file = "my_name.xls";
ob_end_clean();
ini_set('zlib.output_compression','Off');
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache");
header("Expires: 0");
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;');
header("Content-type: application/x-msexcel");
header('Content-Disposition: attachment; filename="'.basename($export_file).'"');
Converting SQL date in PHP to European date format and vice-versa
- Category: Personal Home Page
- Hits: 21488
If you've ever made PHP scripts to process data within a LAMP environment (Linux, Apache, MySQL, PHP) then this happens a lot. In the following example, our HTML form will allow the user to specify a date (so excludes hours, minutes and seconds). For demonstration purposes, I'm going to be using the European date format so DD/MM/YYYY.
The SolutionUsing a HTML form and PHP to upload a file
- Category: Personal Home Page
- Hits: 30642
Basically you have a HTML form with an input field type of 'FILE' (ie. <input type="file" name="file_to_upload" />) and want a PHP file to process this. This example applies to a Linux Apache MySQL PHP (LAMP) environment.
The Solution
1. The first thing to do is check that your HTML form is setup to do this:
Counting the occurence of a word within a string: Benchmark
- Category: Personal Home Page
- Hits: 17770
I added this article because when I was only trying to look up how to do this, this nut "the Storyteller" went and carried out a benchmark test on the most popular ways of counting the occurrence of a specific word within a string of text.

