Category: Web Programming

PHP Date() Function

The date() function converts a given timestamp into a readable date format.

Here is the syntax for this function:

Syntax

[code lang=”php”]
date(format,timestamp);
[/code]

Parameters:

format
This parameter determines the format and how the date will read.

For example: “Saturday, Nov. 16th 2009” would be the following code:

[code lang=”php”]
$timestamp = time();
date(“l, M. jS Y”,$timestamp);
[/code]

timestamp
This parameter is the timestamp you want to use to format a date from. This timestamp is a number of seconds since a certain date, since we used the current timestamp of time() we are making the date for the current time.

We’ll break down our example to see what each set of letters represents as part of the date’s format.

  • l – day of the week, textual, long; i.e. ‘Friday’
  • M – month, textual, 3 letters; i.e. ‘Jan’
  • j – day of the month without leading zeros; i.e. ’1′ to ’31′
  • S – English ordinal suffix, textual, 2 characters; i.e. ‘th’,‘nd’
  • Y – year, 4 digits; i.e. ’1999′

View the complete date format reference here.

Filed under: PHP, Web ProgrammingTagged with: , ,

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 format without leading zeros; i.e. ‘0’ to ’23’
h hour, 12-hour format; i.e. ’01’ to ’12’
H hour, 24-hour format; i.e. ’00’ to ’23’
i minutes; i.e. ’00’ to ’59’
I (capital i) ‘1’ if Daylight Savings Time, ‘0’ otherwise.
j day of the month without leading zeros; i.e. ‘1’ to ’31’
l (lowercase ‘L’) day of the week, textual, long; i.e. ‘Friday’
L boolean for whether it is a leap year; i.e. ‘0’ or ‘1’
m month; i.e. ’01’ to ’12’
M month, textual, 3 letters; i.e. ‘Jan’
n month without leading zeros; i.e. ‘1’ to ’12’
r RFC 822 formatted date; i.e. ‘Thu, 21 Dec 2000 16:01:07 +0200’
(added in PHP 4.0.4)
s seconds; i.e. ’00’ to ’59’
S English ordinal suffix, textual, 2 characters; i.e. ‘th’,
‘nd’
t number of days in the given month; i.e. ’28’ to ’31’
T Timezone setting of this machine; i.e. ‘MDT’
U seconds since the epoch
w day of the week, numeric, i.e. ‘0’ (Sunday) to ‘6’ (Saturday)
Y year, 4 digits; i.e. ‘1999’
y year, 2 digits; i.e. ’99’
z day of the year; i.e. ‘0’ to ‘365’
Z timezone offset in seconds (i.e. ‘-43200’ to ‘43200’). The
offset for timezones west of UTC is always negative, and for those east
of UTC is always positive.
Filed under: PHP, Web ProgrammingTagged with: , ,

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 include the HTML form that has three inputs (max width, max height, image file).

Use the Upload and Resize Demo Here!

Download This Script!

Here is the PHP for our function – generate_resized_image() – which will take the given image file, create a new file on our server, and then copy the uploaded image to our local image file with the new width and height:

[codesyntax lang=”php” title=”Upload and Resize PHP Code”]
<?php

// index.php

function generate_resized_image(){

$max_dimension = 800; // Max new width or height, can not exceed this value.
$dir = “./images/”; // Directory to save resized image. (Include a trailing slash – /)

// Collect the post variables.
$postvars = array(
“image”    => trim($_FILES[“image”][“name”]),
“image_tmp”    => $_FILES[“image”][“tmp_name”],
“image_size”    => (int)$_FILES[“image”][“size”],
“image_max_width”    => (int)$_POST[“image_max_width”],
“image_max_height”   => (int)$_POST[“image_max_height”]
);

// Array of valid extensions.
$valid_exts = array(“jpg”,”jpeg”,”gif”,”png”);

// Select the extension from the file.
$ext = end(explode(“.”,strtolower(trim($_FILES[“image”][“name”]))));

// Check not larger than 175kb.
if($postvars[“image_size”] <= 179200){

// Check is valid extension.
if(in_array($ext,$valid_exts)){

if($ext == “jpg” || $ext == “jpeg”){
$image = imagecreatefromjpeg($postvars[“image_tmp”]);
}
else if($ext == “gif”){
$image = imagecreatefromgif($postvars[“image_tmp”]);
}
else if($ext == “png”){
$image = imagecreatefrompng($postvars[“image_tmp”]);
}
// Grab the width and height of the image.
list($width,$height) = getimagesize($postvars[“image_tmp”]);

// If the max width input is greater than max height we base the new image off of that, otherwise we
// use the max height input.
// We get the other dimension by multiplying the quotient of the new width or height divided by
// the old width or height.

if($postvars[“image_max_width”] > $postvars[“image_max_height”]){

if($postvars[“image_max_width”] > $max_dimension){
$newwidth = $max_dimension;
} else {
$newwidth = $postvars[“image_max_width”];
}
$newheight = ($newwidth / $width) * $height;

} else {

if($postvars[“image_max_height”] > $max_dimension){
$newheight = $max_dimension;
} else {
$newheight = $postvars[“image_max_height”];
}
$newwidth = ($newheight / $height) * $width;
}

// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);

// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

// Create random 4 digit number for filename.
$rand = rand(1000,9999);
$filename = $dir.$rand.$postvars[“image”];
// Create image file with 100% quality.
imagejpeg($tmp,$filename,100);
return “<strong>Image Preview:</strong><br/>
<img src=\””.$filename.”\” border=\”0\” title=\”Resized  Image Preview\” style=\”padding: 4px 0px 4px 0px;background-color:#e0e0e0\” /><br/>
Resized image successfully generated. <a href=\””.$filename.”\” target=\”_blank\” name=\”Download your resized image now!\”>Click here to download your image.</a>”;

imagedestroy($image);
imagedestroy($tmp);
} else {
return “File size too large. Max allowed file size is 175kb.”;
}
} else {
return “Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).”;
}
}

?>
[/codesyntax]

This function you can define in your header PHP files along with other functions or where ever you would like, so long as it is defined before the call of the function itself.

Here is the HTML for our form to submit the image file and the new dimensions:

[codesyntax lang=”html4strict” title=”HTML Form”]
<form action=”./index.php?do=upload” method=”post” enctype=”multipart/form-data”>
<table width=”100%” align=”center” border=”0″ cellpadding=”2″ cellspacing=”0″>
<tr>
<td align=”left” width=”100″>
New Width:</td>
<td align=”left”><input name=”image_max_width” style=”width: 120px” type=”text” maxlength=”4″ /> px.</td>
</tr>
<tr><td colspan=”2″><strong>OR</strong></td></tr>
<tr>
<td align=”left”>
New Height:</td>
<td align=”left”><input type=”text” name=”image_max_height” style=”width: 120px” maxlength=”4″ /> px.</td>
</tr>
<tr>
<td align=”left”>
Image:</td>

<td align=”left”><input type=”file” name=”image” size=”40″ /></td></tr>
<tr>
<td align=”left” colspan=”2″>
<ol style=”margin:0;padding:3px 0px 3px 15px”>
<li>Max file size: 175 KB.</li>
<li>Allowed extensions: jpg, jpeg, gif, png.</li>
<li>Max Dimension: <em>800</em>px.</li>
</ol>
</tr>
<tr>
<td align=”left” colspan=”2″>
<input type=”submit” name=”submit” value=”Submit!” style=”font: 10pt verdana” />
</td>
</tr>
</table>
</form>
[/codesyntax]

This will create a form that looks like this:

Max Width: px.
Max Height: px.
Image:
  1. Max file size: 175 KB
  2. Allowed extensions: jpg, jpeg, gif, png.
  3. Max Dimension: 800px.

 

Okay, so now we have our PHP function defined, included and our HTML contains this form submitting the image file and its new dimensions. All we need now is a short code of PHP to check our GET values, particularly do for the value upload. This tells us the form was submitted and we can start checking it for values, and creating our re-sized image.

Here is the PHP which will appear below the HTML form to show our re-sized image once the form is submitted:

<?php

if(isset($_GET['do']) && $_GET['do'] == "upload")
{

$upload_and_resize = generate_resized_image();
echo '<div id="upload">'. $upload_and_resize. '</div>';

}

?>

Filed under: Featured, PHP, Scripts, Tutorials, Web ProgrammingTagged with: , , , , , ,

PHP Favicon Generator Script

I decided to make a free easy to use favicon generating script that uses PHP. The script has a few features and requirements when uploading your image to be converted to a favicon. First the script checks for a few things such as file type, file size, if the directory is writable, and the specified dimensions. You can create a 16×16 icon or a 32×32 icon.

This PHP Favicon Generator Script first checks the form submitted for the following:

  • Valid file size. (Default: Max 75kb)
  • Valid file types. (Valid extensions are defaulted to image files)
  • Writable directory for favicons.

Then, once verifying these variables, we create a temporary image file on our server with the new dimensions gathered from the form (16×16 or 32×32). We then copy the uploaded image file’s contents over to the temporary file, while resizing it. Once the temporary image file has the new width, height, and copied graphical output, we can transfer this temporary data to a file on our server. Finally, we rename this image file to have a favicon file extension (.ico). Wallah!

Click here to check out the demo for this script!

Just select the image file you want converted to a 16×16 or 32×32 icon and hit submit! It’s that easy.

Here’s the PHP function we want to include in our header, prior to loading the HTML:

[codesyntax lang=”php” title=”Favicon Generator Script”]
<?php
// bgallz.org – Web coding and design tutorials, scripts, resources and more.
// favicon Generator Script
function generate_favicon(){
// Create favicon.
$postvars = array(“image” => trim($_FILES[“image”][“name”]),
“image_tmp”        => $_FILES[“image”][“tmp_name”],
“image_size”    => (int)$_FILES[“image”][“size”],
“image_dimensions”    => (int)$_POST[“image_dimensions”]);
$valid_exts = array(“jpg”,”jpeg”,”gif”,”png”);
$ext = end(explode(“.”,strtolower(trim($_FILES[“image”][“name”]))));
$directory = “./favicon/”; // Directory to save favicons. Include trailing slash.
$rand = rand(1000,9999);
$filename = $rand.$postvars[“image”];

// Check not larger than 175kb.
if($postvars[“image_size”] <= 179200){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == “jpg” || $ext == “jpeg”){
$image = imagecreatefromjpeg($postvars[“image_tmp”]);
}
else if($ext == “gif”){
$image = imagecreatefromgif($postvars[“image_tmp”]);
}
else if($ext == “png”){
$image = imagecreatefrompng($postvars[“image_tmp”]);
}
if($image){
list($width,$height) = getimagesize($postvars[“image_tmp”]);
$newwidth = $postvars[“image_dimensions”];
$newheight = $postvars[“image_dimensions”];
$tmp = imagecreatetruecolor($newwidth,$newheight);

// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

// Create image file with 100% quality.
if(is_dir($directory)){
if(is_writable($directory)){
imagejpeg($tmp,$directory.$filename,100) or die(‘Could not make image file’);
if(file_exists($directory.$filename)){
// Image created, now rename it to its
$ext_pos = strpos($rand.$postvars[“image”],”.” . $ext);
$strip_ext = substr($rand.$postvars[“image”],0,$ext_pos);
// Rename image to .ico file
rename($directory.$filename,$directory.$strip_ext.”.ico”);
return ‘<strong>Icon Preview:</strong><br/>
<img src=”‘.$directory.$strip_ext.’.ico” border=”0″ title=”Favicon  Image Preview” style=”padding: 4px 0px 4px 0px;background-color:#e0e0e0″ /><br/>
Favicon successfully generated. <a href=”‘.$directory.$strip_ext.’.ico” target=”_blank” name=”Download favicon.ico now!”>Click here to download your favicon.</a>’;
} else {
“File was not created.”;
}
} else {
return ‘The directory: “‘.$directory.'” is not writable.’;
}
} else {
return ‘The directory: “‘.$directory.'” is not valid.’;
}

imagedestroy($image);
imagedestroy($tmp);
} else {
return “Could not create image file.”;
}
} else {
return “File size too large. Max allowed file size is 175kb.”;
}
} else {
return “Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).”;
}
}
?>
[/codesyntax]

Then we must include the HTML form that will submit the image and dimensions to PHP:

[codesyntax lang=”html4strict” title=”HTML Form”]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>PHP Favicon Generator Script from bgallz.org</title>
</head>

<body>
<h2>Create Your Favicon</h2>
<form action=”index.php?do=create”  enctype=”multipart/form-data” method=”post”>Image Dimensions:<br />
<select style=”width: 170px;” name=”image_dimensions”>
<option selected=”selected” value=”16″>16px x 16px</option>
<option value=”32″>32px x 32px</option>
</select>
<p><span style=”font-size: 14pt;”>Favicon Image:</span></p>
<input name=”image” size=”40″ type=”file” />
<input style=”font: 14pt verdana;” name=”submit” type=”submit” value=”Submit!” />
</form>
<p><a href=”https://bgallz.dev/488/php-favicon-generator-script/” target=”_blank”>PHP Favicon Generator Script</a> from <strong>bgallz.org</strong></p>
[/codesyntax]

So we have the function included in our header, and the form is presented by the code above. Now we just need to call the function when our GET value for “do” is set to “create” – which the form does for us.

Here is the PHP to include after the HTML form:

[codesyntax lang=”php” title=”After Form PHP”]
<?php
if(isset($_GET[“do”])){
if($_GET[“do”] == “create”){
if(isset($_POST[“submit”])){

// Call the generate favicon function and echo its returned value
$gen_favicon = generate_favicon();
echo $gen_favicon;
echo “Place your download instructions and anything else you want here to follow the download link after upload.”;

}
}
}
?>
[/codesyntax]

 

Also be sure to follow this PHP with the closing tags for HTML:

[code lang=”html4strict”]
</body>
</html>
[/code]

Be sure to include the HTML head tags in your HTML pages that use the favicon. These are given on the script’s index and on the demo.

This header HTML looks like this:

<link rel="shortcut icon" type="image/x-icon" href="./favicon.ico">

 

How to Force Download for .ico Files

Open a new text document in notepad. Paste the following code within the document:

[code lang=”htaccess”]
<FilesMatch “\.(?i:ico|gif|png|jpg|jpeg)$”>
Header set Content-Disposition attachment
</FilesMatch>
[/code]

Now save this file as “.htaccess” exactly like that. Place this file in the directory you are saving your .ico files to for download. This will force the access of these files (ico, png, gif, jpeg) to be a download only type.

Thanks for reading!

Click here to download the script!

Filed under: PHP, Scripts, TutorialsTagged with: , , , , , , ,

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:

[code lang=”php”]
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 . “th”;
break;
}
}
}
[/code]

Basically the function first checks if the number is in the range of 11-13, and if so it returns the number with “th” attached (11th, 12th, 13th). If it is not in this range it checks the remainder of the number divided by 10.

So let’s say our number was 34. 10 goes into 34 three times, with a remainder of 4. Now the function runs this value against three cases, those being 1, 2, and 3. Since it is not one of them, the default value is used, which is “th.” Thus returning “4th.”

Example input:

[code]
3
10
999
[/code]

Example output:

[code]
3rd
10th
999th
[/code]

Enjoy!

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , ,

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 for displaying rows returned from a mysql query as <option>’s in a HTML <select> or drop down.

Before we can grab anything from a mysql database we have to connect to it. Find how to connect to a mysql database here.

Let’s make our mysql query first:

[code lang=”php”]

[/code]

This will grab all the rows in “table1” ordered by the value of “id” descending. You can make this query whatever you want whether you want it ordered differently or with other requirements, etc. Now we will just make a <select> inside of a HTML form that holds each of these values as a option.

[code lang=”html”]


Categories

Category:



[/code]

If you have rows returned in you mysql query you will have a HTML drop down that looks like this:

Categories:

This HTML form is being submitted to “index.php?do=nav.” Of course you can point this where ever you want to do whatever you want with it, but let’s say we want to have it direct you to a category with PHP. So we are going to run a function on index.php?do=nav that will redirect the viewer to the category.

[code lang=”php”]

[/code]

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , , ,

PHP error_reporting() Function

The error_reporting() function determines what errors are reported from the current script.

Here is the syntax for this function:

Syntax

[code lang=”php”]
error_reporting(report_level);
[/code]

The report_level parameter is optional and specifies what report level to report for the current script. This can be set by its numeric value or its constant name, however for future versions of PHP it is recommended you use the constant name rather than the numeric value.

Report Levels

Value Constant Description
1 E_ERROR Fatal run-time errors. Errors that can not be recovered
from. Execution of the script is halted
2 E_WARNING Non-fatal run-time errors. Execution of the script is not
halted
4 E_PARSE Compile-time parse errors. Parse errors should only be
generated by the parser
8 E_NOTICE Run-time notices. The script found something that might be
an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors at PHP startup. This is like an E_ERROR in the
PHP core
32 E_CORE_WARNING Non-fatal errors at PHP startup. This is like an E_WARNING
in the PHP core
64 E_COMPILE_ERROR Fatal compile-time errors. This is like an E_ERROR
generated by the Zend Scripting Engine
128 E_COMPILE_WARNING Non-fatal compile-time errors. This is like an E_WARNING
generated by the Zend Scripting Engine
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by
the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING
set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the
programmer using the PHP function trigger_error()
2048 E_STRICT Run-time notices. PHP suggest changes to your code to help
interoperability and compatibility of the code
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be
caught by a user defined handle (see also set_error_handler())
8191 E_ALL All errors and warnings, except level E_STRICT (E_STRICT
will be part of E_ALL as of PHP 6.0)

Here is an example of the error_reporting function in PHP:

[code lang=”php”]

[/code]

Filed under: PHP, Web ProgrammingTagged with: , ,

CSS Drop-Down Menu

Drop down menus are very useful for navigation on websites and for holding many links to pages on your site without taking up much space on your web pages. Using just CSS and Javascript we can make a nice simple drop down menu you can put on your web page. To do this we’ll have three files: dropdown.css, dropdown.js, and index.html. Our index.html page will display the drop down menu and our stylesheet – dropdown.css – will hold the styles for the drop down menu.

Here is a preview of what our drop down menu will look like:

Dropdown.css

[codesyntax lang=”css” title=”Dropdown Stylesheet”]
@charset “utf-8”;
#dropdown {
margin: 0px;
padding: 0px;
list-style-type: none;
text-align: left;
font: 11px Arial, Helvetica, sans-serif;
}
#dropdown li {
float: left;
margin: 0px;
padding: 5px;
list-style-type: none;
border-bottom: 2px solid #cccccc;
border-right: 1px solid #eeeeee;
}
#dropdown li a {
display: block;
margin: 0;
text-decoration: none;
}

#dropdown div {
position: absolute;
visibility: hidden;
margin: 7px 0px 0px 0px;
padding: 0;
background-color: #e8e8e8;
border-right: 1px solid #c5c5c5;
border-bottom: 1px solid #c5c5c5;
}
#dropdown div a {
position: relative;
display: block;
margin: 0;
padding: 3px 6px;
width: auto;
white-space: nowrap;
text-align: left;
text-decoration: none;
font: 11px Verdana, Arial, Helvetica, sans-serif;
color: #333;
border-top: 1px solid #f1f1f1;
border-bottom: 1px solid #e2e2e2;
}
#dropdown div a:hover {
background-color: #f1f1f1;
color: #000;
}
[/codesyntax]

Dropdown.js

[codesyntax lang=”html4strict” title=”Javascript Source Code”]
// Dropdown menu javascript
var timeout    = 500;
var closetimer    = 0;
var ddmenuitem    = 0;

// open hidden layer
function mopen(id)
{
// cancel close timer
mcancelclosetime();

// close old layer
if(ddmenuitem) ddmenuitem.style.visibility = ‘hidden’;

// get new layer and show it
ddmenuitem = document.getElementById(id);
ddmenuitem.style.visibility = ‘visible’;

}
// close showed layer
function mclose()
{
if(ddmenuitem) ddmenuitem.style.visibility = ‘hidden’;
}

// go close timer
function mclosetime()
{
closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
if(closetimer)
{
window.clearTimeout(closetimer);
closetimer = null;
}
}

// close layer when click-out
document.onclick = mclose;
[/codesyntax]

Index.html

[codesyntax lang=”html4strict” title=”Index HTML”]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<link href=”./dropdown.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”./dropdown.js”></script>
</head>

<body>
<ul id=”dropdown”>
<li><a href=”#”>Homepage</a></li>
<li><a href=”#” onmouseover=”mopen(‘list1’)” onmouseout=”mclosetime()”>Drop Down Menu #1</a>
<div id=”list1″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
<li><a href=”#” onmouseover=”mopen(‘list2’)” onmouseout=”mclosetime()”>Drop Down Menu #2</a>
<div id=”list2″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
<li><a href=”#”>Link #1</a></li>
<li><a href=”#”>Link #2</a></li>
<li><a href=”#” onmouseover=”mopen(‘list3’)” onmouseout=”mclosetime()”>Drop Down Menu #3</a>
<div id=”list3″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
</ul>
</body>
</html>
[/codesyntax]

Click here to view the live demo!

Click here to download this drop down menu script!

Filed under: CSS, Scripts, TutorialsTagged with: ,

PHP Global Variables with Functions

Here we’re going to take a look at variables inside and outside of PHP functions. It is very easy to make one error with a function and have it return the wrong value. We’re going to make a simple function named “func1” which we’ll tweak a bit to show how variables are used inside and out of functions in PHP.

Here is our function, take a look and see if you can tell what the output of the function will be:

[code lang=”php”]

[/code]

The output is nothing right now. haha So if you thought it was anything but nothing, that’s wrong. The function has no code in it whatsoever right now, and the variable “$var” is set to equal the returned value of the function “func1().” Since the function does nothing, $var is equal to nothing. Easy enough. Now let’s work with some variables.

[code lang=”php”]

[/code]

Now, we have a parameter to our PHP function. A parameter is any variable that is defined within the function’s parentheses. You can have many parameters in your functions – seperated by commas – but it is  a good idea to keep your functions organized and simple to their purpose. So we have $var as a parameter to func1(). You can use these to submit values through a function and work with them inside the function to return a different or desired value. Right now, the function func1() is just returning $var. So whatever is put through as $var initially, will be returned back as it was.

So this code will result in the output:

hello

Let’s say we want to have a global variable we can use in several functions. Global variables are defined as such within a function as previously defined variables outside the function. In the following code we’ll define $name as “Brian” and use it in two functions.

[code lang=”php”]
“;
echo $goodbye;

?>
[/code]

The output of this code will be:

Hello Brian!
Goodbye Brian!

You can use global variables with PHP pages you include on other PHP pages. For example if you have variables defined on one PHP page, in this example “variables.php,” you can call that page to another and globally define variables in your functions to work with them and return what you like.

Variables.php:
[code lang=”php”]

[/code]

Now on our page we are working on, we’ll call it “display.php,” we’ll call the page variables.php and use these variables in functions.

Display.php:
[code lang=”php”]

[/code]

The output of this code will be:

Hello Brian! Thanks for visiting bgallz.org.

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , , ,

Making a CSS Linkbar with Photoshop

In this post I am going to show you how to make a cool link bar for your website with Photoshop. Link bars are very important in websites as they provide a clear and easy way for viewers to reach other pages on your site. Basically we’ll make a background image which will be set to repeat along the x axis as the background of the cells in our table. Then the links in the link bar will be placed on top of the background image blending in nicely as the buttons on your link bar.

In CSS we make a few styles for the table that will serve as the link bar holder:

style.css

[code lang=”css”]
.link-bar {
padding:0;
border: 2px solid #d8d8d8;
font: 11px tahoma;
}
.link-bar td {
background: #eee url(./images/bg.gif) repeat-x top left;
padding:0;
height: 32px;
}
[/code]

On our server we have to be sure to be placing the images all in the “images” folder in the “public_html” folder – your website’s default directory.

I made a new document with the dimensions 110×32 pixels as the size of a button. You can make them as big as you want for different words or a standard size, whatever you prefer.

1. Make a new layer and fill the layer with the color #EEEEEE as the background color of the link bar.

2. Make another layer and select a lower smaller half portion of the button to shade in with #E5E5E5.

3. Then make your text layer by typing whatever you want onto the canvas with the text tool on the tool panel. I started with “Home” as the first button. You want to apply the following settings to the text layer’s blending options. You can do this by double clicking on the text layer in the layers panel.

Double click the “Home” text layer and apply the settings below or something similar. You can change these grayscale colors to whatever you would like to fit your website’s color theme.

Our first button turns out something as so:

Now, you can make a rollover image if you would like. This will just make the image change when the viewer puts their cursor over it. We’ll add a slight glow on a new layer to make a rollover layer.

Optional Rollover Image Changes

4. Make a new layer. Select the brush tool in the tools panel. Select the 5th brush type, a glowy brush. Set the size to something like 32px and turn the opacity to 50%. Select the color white (#FFFFFF). Here are the settings:

5. At the top of the image drag half of the brush over the very top of the image and the other half off the canvas to make a slight white glow at the top of the image. Here is the result magnified:

Now as the background all we have to do is make a new document with dimensions 1×32. This will be a single line that will repeat as the background. Now do the same you did to the background of the buttons. Make a layer filled in as #EEEEEE and then another layer with 11 pixels or however much you selected along the bottom as #E5E5E5. Here is the background image close up:

Now as the seperator to the images we need to make a seperator image. This will be a very thin image that will sit between the buttons to give them some distinction.

6. Make a new document 2×32 pixels. On one layer copy the background image we made before and stretch it to be 2 pixels wide. (Press Ctrl+T to transform the layer). Make a new layer and fill the middle left half of the image with the color #DADADA and the middle right half with #F6F6F6. Here is the fill-in’s up close:

Now we have our links and our background image. You will need to make a rollover image for every button you want to have one. Now we just have to apply the CSS styles above to our table that will have the link bar images in it. We do this by setting the link tag in the <head> HTML tags.

Insert the following code in the <head> tags:

[code lang=”html”] [/code]

Here is our HTML:

[code lang=”html”]

[/code]

This will produce something like so:

Filed under: CSS, Graphics, TutorialsTagged with: , ,