Articles in: Code

Home » Archive » Code

Check if Field Exists in Mysql Table

Sometimes it is useful to know if a field exists in a mysql table before running a query using that field name, especially when the field name is coming from some kind of user input.
So to do this we use the function mysql_list_fields to grab the fields out of a table and run through them with a for loop until …

Avoid Duplicate Content Pages

Many people do not think about the possibility of duplicate content pages on their website, when this is very common and easily avoidable. All it takes is a little HTML in the head tags of the page.
What is a duplicate content page?
Duplicate content is content that is showing up on the Internet by multiple URLs. Another words, a …

HTML Div Float Property

Using the HTML tag – <div> and the float style property, you can make designs for your websites. Well, you can make layouts for where design could be. This is a good structural tool in laying out where content will be on your web pages, images, blurbs, etc.
Let’s say we want a page to look like this:
First I’m going to …

PHP fopen() Function

The fopen() function will open any valid file or url.
If the function fails it will return false along with an error generated. You can hide the error message by adding an “@” in front of the function name.
Syntax

fopen(filename, mode, include_path, context);

Parameters
filename (String | Required)
This specifies the URL or file to open. Example: “./files/myfile.zip“.
mode (String | Required)
The type of access you …

PHP Pagination with Mysql

So you have a Mysql table you want to pull data from, but you don’t want to flood the page with everything in the table right! So you need some pagination to seperate all the content in the table into easy to open pages.
So let’s say this is your mysql query:

$sql = mysql_query(“SELECT * FROM table1″) or die(mysql_error());

So this will …

PHP Date() Function

The date() function converts a given timestamp into a readable date format.
Here is the syntax for this function:
Syntax

date(format,timestamp);

Parameters:
format
This parameter determines the format and how the date will read.
For example: “Saturday, Nov. 16th 2009” would be the following code:

$timestamp = time();
date(“l, M. jS Y”,$timestamp);

timestamp
This parameter is the timestamp you want to use to format a date from. This timestamp is a …

PHP Date Formats Reference

Here is a table for formatting the PHP function date() and the letters used to represent different parts of a timestamp.

a
‘am’ or ‘pm’

A
‘AM’ or ‘PM’

B
Swatch Internet time

d
day of the month, 2 digits with leading zeros; i.e. ’01′ to
’31′

D
day of the week, textual, 3 letters; i.e. ‘Fri’

F
month, textual, long; i.e. ‘January’

g
hour, 12-hour format without leading zeros; i.e. ’1′ to ’12′

G
hour, 24-hour …

PHP Upload and Resize Image

Many times when you upload a image somewhere you want to resize it to different dimensions based off of a maximum width or height. Here is a simple script that does this for you, using a HTML form and a PHP script. We start with the PHP script that will run if our $_GET['do'] is set to “upload.” Then we …

PHP Ordinalize Numbers – Add Suffix

Here is a very simple function to use to ordinalize numbers in PHP. This adds the place value suffix to numbers. So you can turn numbers like 1, 2, 3 into 1st, 2nd, 3rd.
Here is the code:

function ordinalize($int){
if(in_array(($int % 100),range(11,13))){
return $int . “th”;
} else {
switch(($int % 10)){
case 1:
return $int . “st”;
break;
case 2:
return $int . “nd”;
break;
case 3:
return $int . “rd”;
break;
default:
return $int …

Mysql Rows in HTML Option Tag

Let’s say you want to have a simple HTML <select> form as a drop down for rows in a Mysql table. This could be for things like categories, pages, games, anything you want to have in a drop down to navigate to another page or submit a form. What ever the case is, I’m going to make a simple layout …

Sponsors