Wednesday, February 28, 2007

Delete Database fields...

Creating is very important, but sometimes you also need to delete. So, let's say you need to delete a certain place in the database. A field containing a user name for example. The function you are supposed to use is the mysql_query(); function in PHP.
So let's say that the table we are using is named users and this table contains the fields: ID, user_name, password and email.
In order to delete the correct value, we need to know the ID value of the user_name that we want to delete, because it's a unique value.
This is the function:

mysql_query("DELETE FROM `users` WHERE `ID`='2' ");

Right now, ID, user_name, password and email is completely deleted from the database.
This is a very easy example for beginners.
In later posts I will show you more complex examples where you can use PHP variables extracted from the database or set by you etc....
Enjoy.

Broken board :(


The white board that I used to make "art" pictures for explaining and hopefully entertaining YOU, was broken today when I accidentally stepped on it (was on the floor). I will probably make a new one but I don't have time right now as I am very busy. So Photoshop pictures containing similar "data" will have to do......
If I get enough posts that request the board back, I will make one fast. I forgot to make pictures of it. :(
Until then... happy reading
Cy21

Tuesday, February 27, 2007

Sorting (Again)- but this time, easyer


The last post was about posting too but there I presented a general, well known sorting algorithm(Bubble Sort) that sorts a string of numbers.
PHP has a simple function that does a lot of things in the sorting world but comparing to Bubble Sort, You don't need to see and understand the source code.
The function looks like this:

array_multisort();

and it can sort 1 or even 2 dimensional arrays....
you can use it by giving values like: the array, sorting order, sorting type.

array_multisort($array,sorting order, sorting type);

The array option is recommended but the other 2 are optional....
For the sorting order, you have the following options:
  1. SORT_ASC - sort in descending order (ex: from A to Z)
  2. SORT_DESC - sort in descending order (ex: from Z to A)
For the sorting Type you have other options:

  1. SORT_REGULAR - This option compares elements in a normal way
  2. SORT_NUMERIC - This option compares elements as numeric values
  3. SORT_STRING - This option compares elements as string values
You can optionally add more arrays...

That's about it... You just call the function. and set the objects. It does everything...FAST MAGIC :)

Bubble Sort - How it works

//
function BubbleSort( $items ) {
$temp = "";
$size = count( $items );
for( $i = 0; $i < $size-1; $i++ ) {
for( $j = 0; $j < $size - 1 - $i; $j++ ) {
if( $items[$j+1] < $items[$j] ) {
$temp = $items[$j];
$items[$j] = $items[$j+1];
$items[$j+1] = $temp;
}
}
}
}
//
In order to sort an array of n items, we can use this Bubble Sort Algorithm.
How it works: It's a function, that you call it and place in it, as a value.. ...the array of the items you want to sort. IT compares 2 elements. IF the first one is greater then the second one, it swaps them. And this thing is done with all of the elements, from the first 2...until the last 2. The step keeps being repeated for 1 fewer elements each time. So...basically, the step is being done until there are no more pairs to compare. As you can see, there are 2 FOR statements one inside the other. 2 Loops, one included in the other. It's a simple, but very known and used algorithm. to use it: for example you have an array of 5:
$x[0]=1;
$x[1]=6;
$x[2]=4;
$x[3]=2;
$x[4]=5;

$new_array = BubbleSort($x);

now, the content of the new array will be:
$new_array[0];//1
$new_array[1];//2
$new_array[2];//4
$new_array[3];//5
$new_array[4];//6

Monday, February 26, 2007

Making a Dynamically resizable Form - secrets


Let's say you have a form like the one in the image on the left. You've just created this form in your favorite picture editor and you wish to add content in it. But, you find that you have more than 1 problem: the picture you've created is too little or too big for the place you want to add it into your web page, and the content is going to change so if you have more content, you will be forced to make the font smaller so the picture will be just 1 and not 1 and 1/2 :).

So, what do you do?

You cut your picture into small parts. so that it will dynamically change according to the content. So..if you have 3 pages of content, your "picture" will be 3 pages in height and if you want to make the picture wider, no problem, same thing :).
In order to better understand how the above picture is supposed to be cut, I will show you another picture:
The areas marked with green are the areas that are supposed to be cut and transformed into separate pictures. You have to use tables for this procedure. each picture will be a new table. the margins(corners) on the left, right(both up and down margins) are supposed to be fixed in size.(fixed width and height) the middle left, right, up and down are supposed to be 1 pixel wide(for up and down) and 1 pixel in height (for left and right). If you do this, you will have great borders, exactly like your picture.

Why?

Because the left, right, up and down bars are made out of 1 small picture. if you set that picture the sizes mentioned above, if you add content, no matter how much, the picture will automatically resize...... the picture repeats itself.

You still haven't finished. The center must be a picture 1 pixel in width and height that is created from the middle of the original picture.

If it's still not clear, comment and I will answer your questions.
In a later post, I promise a PHP function that automatically does everything needed to be done in HTML coding.... that's mentioned above. You will still have to cut your picture :).

Cy21

Sunday, February 25, 2007

Insert into Mysql Database - SQL commands

You have a database that has a few tables in it and you want to add some values in those tables (more exactly in the fields of the tables). So what do you do?
You can:
- go to PhpMyadmin and just select the database, table and insert the values.
- you can LEARN how to write the SQL code so that the values will be entered in the proper place.

So, let's do that :).

In order to insert something in the Mysql database you need to use the INSERT INTO command. After that you write the name of the table like so: `table_name` and then you start enumerating the fields between ( ) like so: (`field1`, `field2`, `field3`) next you add the values by writing VALUES and then again the values that need to be stored in the previous fields, IN THAT EXACT ORDER!!!! like so: ('value1', 'value2', 'value3') and then you end with a ;.
This is how the full code is supposed to look like:

INSERT INTO `table_name` (`field1`, `field2`, `field3`) VALUES ('value'`, 'value2', 'value3');

you notice that the fields are in one type of brackets and values are in another type.
like this: `field1` and 'value1'. only values must be in ' ' .Everything else is in ` ` :)

Hope It helped,
Cy21

Extract data from Mysql Database

Ok, so you have some data stored in tables in your Mysql Database. How do you get that data out? I'm going to show you a very basic and easy way to get that data out of the database. Let's name teach thing we are working with. The table is named "my_table", and let's say we need to extract data from the following fields: "ID", "user_name", "password".

How do we do this?

First we make a new variable.Let's name it "$query". we assign to that variable, the PHP function mysql_query(); with the SQL request that extracts the inside the function like so:

mysql_query("SELECT * FROM `my_table`");
Right now, the variable $query contains an array with all of the values in the "my_table" table.
To get that out, and store it into other variables so we can use it we need to make a while loop work. To store all of the data into variables I will make an array of variables.I will continue the example using the commands again:
$query =mysql_query("SELECT * FROM `my_table`");
$i=0;
while($row = mysql_fetch_array($query ))
{
$ID[$i]=$row['ID'];
$user_name[$i]=$row['user_name'];
$password[$i]=$row['password'];
$i++;
}
Right now, the $ID,$user_name and $password are arrays that contain all of the values of the table "my_table".
To check a value you use the echo function:
echo $ID[0];
or
echo $user_name[5];

Enjoy.

Saturday, February 24, 2007

PHP file include file in code


A very useful function that PHP provides is the include() function. It gives you the possibility to add the content of one file in another. So, for example if you want to make a very loooong script that does something important and you don't want the page you are working on right now to be filled with all of that content, you can use a IF statement and in that IF statement, you can add:

include("file_path/file_name.extension");

Now, you have one file included in another, and you have just 1 line of code more ......
This is a basic easy function. More to come.........

Nr of days - Date Function PHP

//
function date_return($days,$start_date=NULL)
{
if ($start_date!="")
{
$start_datex = explode("-",$start_date);
$day=$start_datex[0];
$month=$start_datex[1];
$year=$start_datex[2];
}
else
{
$day=date(d);
$month=date(m);
$year=date(y);
}

$default_month=$month;
while($days>0)
{
$x = cal_days_in_month(CAL_GREGORIAN,$month,$year);
if($default_month==$month)
{
if($day+$days > $x)
{
$days=$days-($x-$day);
$day=0;
if ($month==12)
{
$year++;
$month=01;
}
else
{
$month++;
}
}
else
{
$day=$day+$days;
$days=0;
}
if ($day<10 style="color: rgb(51, 51, 255);">strlen($day)<2){$day="0".$day;} style="color: rgb(51, 51, 255);">strlen($month)<2){$month="0".$month;} style="color: rgb(51, 51, 255);">strlen($year)<2){$year="0".$year;}> $x)
{
$days=$days-($x-$day);
$day=0;
if ($month==12)
{
$year++;
$month=01;
}
else
{
$month++;
}
}
else
{
$day=$days;
$days=0;

}
if ($day<10 style="color: rgb(51, 51, 255);">strlen($day)<2){$day="0".$day;} style="color: rgb(51, 51, 255);">strlen($month)<2){$month="0".$month;} style="color: rgb(51, 51, 255);">strlen($year)<2){$year="0".$year;} date="$day." style="color: rgb(0, 153, 0);">return $date;

}
//


Returns a date after you give it a number of days and a starting date. You can leave the start date blank , and it will give the current date as a start date :).
For example, you can calculate when does a customer have to pay you if he applied for a number of days for your product or service. the function will give you the exact date.

A lot of strings are cut and calculated in order to show a good date, with a 0 in front of a number if the number is smalled than 10.
To call the function you can type:
< ?php $date=
date_return("30"); //will show a date 30 days from now
$date=date_return("30","12-03-2001"); //will show a date 30 days from 12-03-2001
? >
If you wish to sate a starting date, you need to make it exactly like this:
day-month-year, separated by a "-".
That's all!

Scrape Web Page script



//

function getRSS($xml_url)
{
$handle = @fopen($xml_url, "r");
if ($handle)
{
while (!feof($handle))
{
$buffer.= fgets($handle, 4096);
}
fclose($handle);
}
return $buffer;
}
//

With this PHP script, you can extract all of the HTML information of any web page and store it in a variable. From there, you can cut it and find certain parts of it to use how ever you like. The way you use this script for extracting web page content is your responsibility.
To call the function and store it in a variable you type this:
< ?php
$variable = getRSS("http://www.google.com");
? >

Rename a file on your hosting server

//
function rename_file_hex($name,$path)
{
@$newname = md5_file($path.$name);
$ext = @substr($path.$name,$lenght-4);
@rename($path.$name,$path.$newname.$ext);
}
//

This PHP function renames a File from your server to a "HEX_name.extension".
So this way, you will have a file with an unique name. You just need to know the name of the file(before it is changed) and the path on the server. The function it is called like this:

< ?php
rename_file_hex("file_name.jpg","file_path"); ? >

Upload files simple function

//
function
upload_files($path)
{
if ($_FILES["file"]["error"] > 0)
{

}
else
{

$name = md5_file($_FILES["file"]["tmp_name"]);
$lenght = strlen($_FILES["file"]["name"]);
$ext = substr($_FILES["file"]["name"],$lenght-4);
if (file_exists($path . $_FILES["file"]["name"]))
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$path . $name.$ext);
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$path . $name.$ext);
}
}
return $name.$ext;
}
//

In order to call this PHP function you must already have the value of the file object sent to the page where you are calling the function. So, basically you must have a web page that sends that information. the form must contain the enctype="multipart/form-data" in it's content in order to send the file information right.
The input will look like this:


<input type="file" name="file" id="file" />

A textbox and a
Browse Button will appear. You can select your file from there and send it with the form on your page where the function will take the data and upload it.

The function is called like this:

< ?php
upload_files("path_2_save_on_the_server"); ? >

The file will be automatically renamed to it's equivalent in HEX so it will have an unique name :).

Mysql connect Function

//
function connect_db($localhost,$user,$password,$database)
{
mysql_connect($localhost,$user,$password);
mysql_select_db($database);
}
//

You use the mysql_connect function and the mysql_select_db function at the same time in 1 new function created by you. Saves up a few seconds of your time. ;)
So actually, you just call 1 PHP function:
< ?php connect_db("localhost","username","password","database") ? >
and VOILA!!! you are connected to your database!

Technorati Profile

Tables inside Tables - HTML design trick


Making a web page look good and work on all Browsers is not easy. Especially if you are using javascript ;). But...how can you make a web page entirely out of tables, and make it perfect at the same time?
Creating tables one inside of each other and setting everything right can make wonders.
First you must make a table with the border of 0 (border="0") and make the with and height of the page (of the table) at 100% (width="100%" height="100%"). Also very important , you must always set the cellspacing and cellpadding to 0 (cellpadding="0" cellspacing="0"). The table will now look like this:

< border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
Right now, you have a table that is as wide and as tall as the browser's page is. now, very important, to continue, you must make the properties set to the center and aligned to the middle or the top like so:< align="center" valign="top">. Inside the <> you must place now another table just like the one in the first example, but you must get the width="100%" height="100%" out! so it will look like this:
< border="0" cellpadding="0" cellspacing="0">
Right now, you have a table including another table, and the second table is on the top center of the page. Once you add data to that table it will become wider. You can include as many tables as you want in the second one....and make as many separate <>'s as you want.
Including one table in the other is very useful. You can position any content where ever you want to. For example, if you want to make the top-center table of 400 width, you just set the width="400" of the <> that includes the table and the content of the table will now be wider.A very important thing once you set a width or a height property is....if you want the content to go down, in stead of right, you must set the nowrap="nowrap" . Try it!
Let me know if there are any questions ;).

Friday, February 23, 2007

How to add data in Mysql From Php

Adding information in the database (a mysql database) using PHP is not a hard thing at all...
You just use a function:

mysql_query(" ");
between " " you must add the mysql command that adds the information.
for example: you have a table named "user" that contains:
user name
password
How do you add information to mysql from the page you have?

Simple!

Let's say you have the information stored in the variables:
$user_name and $password
you just use the following command:

mysql_query("INSERT INTO `user` (`user_name`, `password) VALUES ('$user_name', '$password')");
user is the name of the table
you must put tables and rows and everything that is not a value in ``
you put values in ''
` is a character on the left of the 1 button on your keyboard !
' is the second one on the right of L button on your keybord
values are variables themselves!!!
you can also write the insertion like this:
mysql_query("INSERT INTO `user` (`user_name`, `password) VALUES ('".$user_name."', '".$password."')");
in order to make the variables $user_name and $password outside of the mysql_query function.

Textbox Date Validator - Javascript

I needed to validate 3 textboxes for making a date input on a web page., but I needed to verify the content that the user was entering in the webpages. So I came up with the following Javascript Function:

function text_check(id,x)
{

var text,i,value,status;
var nr = new Array()
nr[0]="0";
nr[1]="1";
nr[2]="2";
nr[3]="3";
nr[4]="4";
nr[5]="5";
nr[6]="6";
nr[7]="7";
nr[8]="8";
nr[9]="9";

text = document.getElementById(id).value;

text = text.substr(text.length-1);
i=0;

while(i<=10)
{
if(text!=nr[i])
{
value=false;
}
else
{
value=true;
break;
}
i++;
}

if(value!=true)
{
if(x=="true"){alert("You cannot imput "+text+" character! Only Numbers!");}
text = document.getElementById(id).value;
text = text.substr(0,text.length-1);
document.getElementById(id).value = text;
}

}

You just call it and enter the ID of the textbox. here is an example:

< id="birth_date_1" name="birth_date_1" onkeydown="text_check('birth_date_1','false')" onkeyup="text_check('birth_date_1','true')" onclick="text_check('birth_date_1','false')" style="width: 30px;" maxlength="2" type="text" width="30">

It does not look very pretty but it get's the job done. The long content can be placed in a dinamic PHP function that will make it more pretty.

Change Image, Change Collor

function changeImage(id,img)
{
newImage = "url("+img+")";
document.getElementById(id).style.backgroundImage = newImage;
}

function changeColor(id,color)
{
document..getElementById(id).style.backgroundImage = color;
}

2 JavaScript functions that modify the color or the image of an ID. You just need to set the ID and the path of the file or the color (Depending on the function). I guess the name of each function gives you the biggest hint .