Wednesday, August 20, 2008

Simple Mini Poll class library

This easy-to-use class library enables you to set up your own survey system in just few minutes.

Main features include:
  • Dynamically generated form (with number of total votes)
  • Detailed result page
  • View of old polls
  • Poll administration interface
  • Check for repeated votes
1 . Execute these queries on your database:

# Table structure for table `poll_check`

CREATE TABLE `poll_check` (
`
pollid` int(11) NOT NULL default '0',
`
ip` varchar(20) NOT NULL default '',
`
time` varchar(14) NOT NULL default ''
) TYPE=MyISAM COMMENT='';

# -----------------------------------------

# Table structure for table `poll_data`

CREATE TABLE `poll_data` (
`
pollid` int(11) NOT NULL default '0',
`
polltext` varchar(50) NOT NULL default '',
`
votecount` int(11) NOT NULL default '0',
`
voteid` int(11) NOT NULL default '0',
`
status` varchar(6) default NULL
) TYPE=MyISAM COMMENT='';

# -----------------------------------------

# Table structure for table `poll_desc`

CREATE TABLE `poll_desc` (
`
pollid` int(11) NOT NULL default '0',
`
polltitle` varchar(100) NOT NULL default '',
`
timestamp` datetime NOT NULL default '0000-00-00 00:00:00',
`
votecount` mediumint(9) NOT NULL default '0',
`
STATUS` varchar(6) default NULL,
PRIMARY KEY (`pollid`)
)
TYPE=MyISAM COMMENT='';

# -----------------------------------------
?>

2. Set up database connection parameters.

3. Paste this code on page where you wish poll to appear:


include_once ("includes/miniPoll.class.php");

$test = new miniPoll;

$test->pollForm();

?>

4. Paste this code on page where you wish poll results to appear:


include_once ("includes/miniPoll.class.php");

$test = new miniPoll;

if (isset(
$_GET['poll']) && is_numeric($_GET['pollid'])) {
$pollid = $_GET['pollid'];

if (isset(
$_GET['voteid']) && is_numeric($_GET['voteid'])) {
$voteid = $_GET['voteid'];
$test->processPoll($pollid, $voteid);
}

}
if (isset(
$_GET['pollid'])) {
$pollid = $_GET['pollid'];
$test->pollResults($pollid);
}

?>

5. Paste this code on poll admin page:


include_once ("includes/miniPollAdmin.class.php");

$test = new miniPollAdmin;

$test->newPollForm();

if (isset(
$_GET['opt'])) {
$opt = $_GET['opt'];
$pollid = $_GET['pollid'];
if (
$opt == 'activate') {
$test->activatePoll($pollid);
}
if (
$opt == 'delete') {
$test->deletePoll($pollid);
}

}

echo
"
"
;
if (isset(
$_GET['q'])) {
$pollname = $_GET['pollname'];
$q = $_GET['q'];
$test->createPoll($pollname, $q);
}
$test->listPolls();

?>


6. Set up these parameters in miniPoll.class.php:

->results_page = "test_poll_results.php"; ?>
page where you display results

7. Set up these parameters in miniPollAdmin.class.php:

->results_page = "test_poll_admin.php"; ?>
name of admin page file

8. Optionally you can change layout of form and results page
9. Create new poll by starting admin page
10. Activate your poll by clicking ACTIVATE
11. Enjoy and send me your comments

How to create and use Favicon.ico file

What is Favicon.ico?


Favicon is a small icon which appears next to your URL in browser address bar.


Why would I use it?


If you look around the Internet, you will notice that all professional web sites use it. It gives more customized appearance of your site in visitor browser and it also gives more distinct look of your site in Bookmarks menu by replacing generic browser icon.


What browsers support it?


Currently all modern browsers support it. Including: Internet Explorer, Mozilla Firefox, Opera, Netscape 7.0 and Mozilla.


How do I create Favicon?


Best solution would be to get some of popular icon editors. If you don't have icon editor, you may use some of the standard image editors like Photoshop, PaintShop or even Windows paint. Simply create 16x16 pixels bitmap image with standard 16 or 256 Windows icon colors. You can't put much in such a small space, but with wright color combinations you may achieve very effective solutions. When you're done editing, simply rename file to favicon.ico.


Where do I put Favicon.ico file?


Simply use your favorite FTP program and upload it in root folder of your web site. Most browser look there for it. You may also help them by inserting following code in section of your page:


<link href="favicon.ico" rel="SHORTCUT ICON">


This way you ensure that browser will find it. You may also put favicon file in different folder, like for example, in your images folder. In that case code may look like this:


<link href="images/favicon.ico" rel="SHORTCUT ICON">

User Online Class

Description:

This very simple class enables you to track number of visitors online in an easy and accurate manner.

Main features include:
  • Automatic insertion of new user
  • Automatic database cleanup
  • Check if user is coming behind proxy server (this helps in script accuracy)
  • Display of users online
  • Error reporting

DATABASE STRUCTURE
CREATE TABLE
`useronline` (
`
id` int(10) NOT NULL auto_increment,
`
ip` varchar(15) NOT NULL default '',
`
timestamp` varchar(15) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `id`(`id`)
)
TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;
?>



PHP CODE :
include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();

if (
count($visitors_online->error) == 0) {

if (
$visitors_online->count_users() == 1) {
echo
"There is " . $visitors_online->count_users() . " visitor online";
}
else {
echo
"There are " . $visitors_online->count_users() . " visitors online";
}
}
else {
echo
"Users online class errors:
    \r\n";
    for (
    $i = 0; $i < count($visitors_online->error); $i ++ ) {
    echo
    "
  • " . $visitors_online->error[$i] . "
  • \r\n"
    ;
    }
    echo
    "
\r\n"
;

}
?>

Tuesday, August 12, 2008

PHP Array

An array is a data structure that stores one or more values in a single value. For experienced programmers it is important to note that PHP's arrays are actually maps (each key is mapped to a value).

PHP - A Numerically Indexed Array

If this is your first time seeing an array, then you may not quite understand the concept of an array. Imagine that you own a business and you want to store the names of all your employees in a PHP variable. How would you go about this?

It wouldn't make much sense to have to store each name in its own variable. Instead, it would be nice to store all the employee names inside of a single variable. This can be done, and we show you how below.
PHP Code:

$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";

In the above example we made use of the key / value structure of an array. The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference. The general form for setting the key of an array equal to a value is:

* $array[key] = value;

If we wanted to reference the values that we stored into our array, the following PHP code would get the job done.

Note: As you may have noticed from the above code example, an array's keys start from 0 and not 1. This is a very common problem for many new programmers who are used to counting from 1 and lead to "off by 1" errors. This is just something that will take experience before you are fully comfortable with it.
PHP Code:

echo "Two of my employees are "
. $employee_array[0] . " & " . $employee_array[1];
echo "
Two more employees of mine are "

. $employee_array[2] . " & " . $employee_array[3];

Display:
Two of my employees are Bob & Sally
Two more employees of mine are Charlie & Clare

PHP arrays are quite useful when used in conjunction with loops, which we will talk about in a later lesson. Above we showed an example of an array that made use of integers for the keys (a numerically indexed array). However, you can also specify a string as the key, which is referred to as an associative array.
PHP - Associative Arrays

In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.
PHP Code:

$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;

echo "Bob is being paid - $" . $salaries["Bob"] . "
";

echo "Sally is being paid - $" . $salaries["Sally"] . "
";

echo "Charlie is being paid - $" . $salaries["Charlie"] . "
";

echo "Clare is being paid - $" . $salaries["Clare"];

Display:
Bob is being paid - $2000
Sally is being paid - $4000
Charlie is being paid - $600
Clare is being paid - $0

Once again, the usefulness of arrays will become more apparent once you have knowledge of for and while loops.

PHP Require

Just like the previous lesson, the require command is used to include a file into your PHP code. However there is one huge difference between the two commands, though it might not seem that big of a deal.


Require vs Include

When you include a file with the include command and PHP cannot find it you will see an error message like the following:
PHP Code:



Display:
Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2 Warning: main(): Failed opening 'noFileExistsHere.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2

Hello World!

Notice that our echo statement is still executed, this is because a Warning does not prevent our PHP script from running. On the other hand, if we did the same example but used the require statement we would get something like the following example.
PHP Code:



Display:
Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2
Fatal error: main(): Failed opening required 'noFileExistsHere.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2

The echo statement was not executed because our script execution died after the require command returned a fatal error! We recommend that you use require instead of include because your scripts should not be executing if necessary files are missing or misnamed.