php

Using fgetscsv function

Using fgetscsv function

Reading the data saved in CSV file format

fgetcsv -- Gets line from file pointer and parse for CSV fields

Fgetcsv() example - Read and print entire contents of a CSV file
The field delimiter is assumed to be a comma, unless you specify another delimiter with the optional third parameter.

PHP Code

<?php
 
$fp = fopen('csvfile.txt','r');
while($line = fgetcsv($fp,'1024',',')){
echo "<pre>";
print_r($line);
echo "</pre>";
 
/*
foreach($line as $value){
echo "$value<br>";
}
echo "<hr>";
*/
}
?>

The Image Creation Capabilities of PHP

The Image Creation Capabilities of PHP

PHP image functions will allow you to create images dynamically

"The format of images you are able to manipulate depend on the version of GD you install, and any other libraries GD might need to access those image formats. Versions of GD older than gd-1.6 support gif format images, and do not support png, where versions greater than gd-1.6 support png, not gif." -- From the PHP Manual: Image Functions

PHP Sessions

A session allows you to preserve data across page requests (going from one page to another on a website) by saving the data to variables that you register as being part of a session. Sessions are tracked through cookies or through the URL. I prefer to track sessions through the URL because cookies may not be enabled on the client computer.

If PHP is compiled with --enable-trans-sid then URLs will contain the session id automatically via the constant PHPSESSID.

Function session_start(void):

Uploading Files with PHP

You can use a browser to upload files with PHP. The files can be either text and binary files. PHP has several file manipulation functions, that allow you to control what you do with a file once it has been uploaded, how large the file to be uploaded should be, where it should go after being uploaded etc.

To upload files, create a file upload form:

Example:

<FORM ENCTYPE="multipart/form-data" ACTION="somephpscript.php3" METHOD=POST>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000">
Send this file: <INPUT NAME="userfile" TYPE="file">

Flash Guestbook V2

Creating a guestbook in Flash using PHP script and a MySQL database

Setting up the database

You will need to have access to a server running PHP and MySQL. If you use another database, replace the MySQL database functions that I use in the PHP script with the database functions for the other database i.e. PostgresSQL, MSSQL.

First create two tables in the database:

# This table will hold the guestbook entries.
# Table structure for table `flashgb`
#

CREATE TABLE flashgb (
id int(4) NOT NULL auto_increment,
name varchar(20) NOT NULL default '',

Fetching and saving the contents of a remote file

Fetching and saving the contents of a remote file

Submit a query to a search engine and save the results to a local file

1. Create a form that submits a query to a search engine.

2. Script should automatically save the query results to a file. Each query should be saved to separate files.

Figuring out the url of the remote file/search engine that you will submit your query to:

Working with regular expressions: HREF URL Extractor

HREF URL Extractor

Working with regular expressions

To extract the value (URL) of an HREF attribute from a string, use the preg_match or preg_match_all function. This example use preg_match_all to find all the matches.

preg_match_all - Perform a global regular expression match

preg_match_all (pattern (string), target (string), matches (array), optional flags)

Searches target for all matches to the regular expression given in pattern and puts them in matches in the order specified by the flags.

The regular expression: <\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]

Downloading files with PHP

Downloading Files

Using the header function to force file downloads

If you have files that you would like users to be able to download, it can be done simply by providing a link to the file. When the link is clicked, the browser will prompt the user to download the file or it will display it if the file type is known to the browser i.e. text files and gif/jpeg images. If you do not want the files displayed in the browser you can use the header() in PHP with the appropriate content type to force a file download.

PHP Quiz Script

PHP Quiz Script

Hard coding a graded quiz script in PHP

Quizzes are generally used to test knowledge of a subject.
This is a simple script that will allow you to create a quiz and grade the answers given.

PHP Code:

<?php
$let=array(
"a","b","c","d","e","f","g","h","i",
"j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z"
);
 
//Set a counter to 1
$count = 1;
 
/* What is a perfect score? Usually 100. */
 
$psco=100;
 
/* Deduction for each wrong answer. May be the perfect score divided by the number of questions

Simple MySQL Guestbook

Creating the guestbook has two parts, the table to hold the guestbook records and the script to insert and select records from the database
table.

The structure of the table should look similar to this:

CREATE TABLE guests (
id int(10) unsigned NOT NULL auto_increment,
guest varchar(50) NOT NULL default '',
message varchar(200) NOT NULL default '',
date datetime default NULL,
PRIMARY KEY (id)
) ;

The guestbook script has three sections - the code for the form, the code for saving the form data, and the code for displaying the data.

Syndicate content