The simplest way to learn about classes in PHP:
Well let's start by defining what is a class:
A generalized category in object-oriented programming that describes a group of more specific items called objects.
A class provides a template for defining the behavior of a particular type of object. Objects of a given class are identicalto each other in form and behavior.
A class is a descriptive tool used in a program to define a set of attributes or servicesthat characterize any member (object) of the class.
Now let's define a class in PHP and use some of the objects function. Let's assume that our object is an associative array having its indexes named after a product table. having fields: ID, NAME, DESCRIPTION.
So we should start by creating the table called products:
CREATE TABLE `products` (
`id` TINYINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 20 ) NOT NULL ,
`description` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` )
)
CODE
//let's define our class, and a set of functions inside this class.
class productdb
{
//this function create an empty instance of an associative array
function EmptyObject()
{
$product=array('ID'=>NULL,'NAME'=>NULL,'DESC'=>NULL);
return $product;
}
//this function takes a primary key as an input and returns the complete set of values corresponding to that primary key
function GetRow($id)
{
$result=mysql_query("SELECT id,name,description from products WHERE id='$id'");
while($row=mysql_fetch_assoc($result))
{
$product['ID']=$row['id'];
$product['NAME']=$row['name'];
$product['DESC']=$row['description'];
return $product;
}
}
}
//Now let's learn how to use those classes....
//first let's create an instance of that class....
$myobj= new productdb;
//now let's create an empty object, that has the specification of the object described above...
$myproduct=$myobj->EmptyObject();
//Suppose our product table is filled with values....
$myproduct=$myobj->GetRow($id)
echo $myproduct['ID'] . $myproduct['NAME'] . $myproduct['DESC'];
?>
And basicaly that is the simplest way to understanding classes and using them.
Sunday, November 4, 2007
PHP and Classes
Posted by guru at 8:15 PM 0 comments
Labels: PHP
Validity of username and passwords
We have assumed that you have created a table called "users", and that table contain as much fields as you want, you can insert recordsinto that table using a form in html. But be sure to include two fields:
name= it is the username of the user
password= it is the password of each user
Simply create a form, using html, and set the action of that forum to a php file where you will include this small piece of code:
- //First lets get the username and password from the user
- $username=$_POST["username"];
- $password=$_POST["password"];
- //Second let's check if that username and password are correct and found in our database
- $sql1=mysql_query("SELECT name, password FROM users WHERE name='$username' AND password='$password'")
- if (mysql_num_rows($sql1)==0 mysql_num_rows($sql1)>1)
- {
- echo "Sorry, the username and password you submitted are not present in our database";
- }
- //if there are found in our database, and there is only one occurence of that username and password
- //thus making them valid, so inside, you can include the webpage you want to open
- if(mysql_num_rows($sql1)==1){include("the webpage");
- //open up the secure page
- //instead of "the webpage" type in the path your secure website is located in
- }
- ?>
Posted by guru at 4:51 PM 2 comments
Labels: PHP
File-upload-with-PHP
Here are the steps :
1. Set up an html page with a form.
2. upload the file to the server.
3. Move the file to it's destination.
4. Let the user know if the upload was successful or not.
Posted by guru at 4:03 PM 0 comments
Labels: PHP