
Script of calculation klikov
The script of calculation klikov is very simple. All that to us need - to be added 1 on the certain line in base.
So, the field which will contain quantity{amount} klikov is called count. The type of this field in base can be " smallint (3) not null ", other field is called url with type " varchar (50) not null ", and more one field will be called id. The field ' count ' stores{keeps} quantity{amount} klikov when on corresponding ' url ' transition has been made.
<?
*EU«JAF« the initial information.
*mTª¡« to name the table.
$table = "blah";
*zhSUuSU on which there is a base
$dbhost = "localhost";
*k¼n databases on the server
$dbname = "name";
*EbU«½eh to base.
$dbpass = "**";
*k¼n a concrete database
$dbbase = "base";
// Now we are connected to base
$connect = mysql_connect ("$dbhost", "$dbname", "$dbpass");
mysql_select_db ($dbbase, $connect);
// We increase quantity{amount} klikov on certain{determined} url.
// Thus id specifies the necessary recording.
$update = " update $table set count = count + 1, url = url where id = ' $ id ' ";
$result = mysql_query ($update);
// Now we get url and the redirect is done{made} there
$query = " select * from $table where id = ' $ id ' ";
$result = mysql_query ($query);
$tab=mysql_fetch_array ($result);
$url = $ tab ["url"];
header (" location: $url ");
?>
Pay attention to this line:
$update = " update $table set count = count + 1, url = url where id = ' $ id ' ";
All values except for id in this search are renominated. Thus it turns out, that it is not necessary to insert new id, for new url. That's all. Now it is possible to address to a script so:
http: // yoururlhere.com/? id=45
(45 - varies for each link).
If you doubt, for what such script, can be useful to you three examples.
*) For a forum or for a script of news (to count up quantity{amount} klikov on the certain message.)
*) For a script of show of banners or for a script of show of links (to count up how much time users went on url.)
*) A script rehjtinga (having cluck, the person reports on the idle link and it gives him additional glasses).
This example can be connected with sriptom viewing of links
It is very simple way to display all links, without everyone hand-worked (except for creation of the script).
As a result of studying this lesson you can:
To incorporate to base mysql
To create tables for storage of the information
To take the data from the table, to add new and to change the written down information
To deduce{remove} links in a cycle
The manager can delete and edit the data on the special protected page
Users can add new links
In the first part of a lesson we:
Let's create php a file which incorporates to a database
Let's make the table in a database with the help phpmyadmin
It was my first script on php and all process of creation has borrowed{occupied} about 30 minutes from the beginning up to the end.
Part 1 - Connection with a database, creation of the table
The first, that we should make is php a file incorporating to a database. The code will be such:
Save this code in a file with a name connect.php and write down in a folder links.
<? php
$username = " database username ";
$password = " database password ";
$host = "localhost";
$database = " the database to be used ";
mysql_connect ($host, $username, $password) or die (" cannot connect to the database. <br> ". mysql_error ());
mysql_select_db ($database) or die (" cannot select the database. <br> ". mysql_error ());
?>
Now, it is necessary to make so that this code was inserted into all php files which will work with the data from base. It is the best way to make it with the help of the next lines (it is necessary to insert these lines into the beginning of everyone php a file):
<? php
include ("connect.php");
?>
Into a file connect.php these lines to insert it is not necessary, it will lead to to that the file should insert itself (an infinite cycle).
Creation of the table
In this lesson we shall use system phpmyadmin to not complicate to ourselves a life and to not force a script to be engaged in creation of tables. Go on page with phpmyadmin and click at the left on a name of a database which you will use. The list of tables which are already added for this moment on the right should appear. Under the list there is a field for input sql search, you should copy there this search:
create table links (
id int (11) not null auto_increment,
site varchar (50) not null,
url varchar (255) not null,
banner varchar (255) not null,
primary key (id)
);
Now press on the button "go" below the second field of input (it is intended for performance of the text file containing sql the text). If all of you have made correctly should the message, that the search is executed successfully will appear: " mysql query was executed successfully ".
If you scroll downwards pages and see at the list of tables, you should see the new table, under the name "links". In this table just also will be the information is stored{kept}.
Well that's all, that was required from phpmyadmin for this moment. It is possible to close a browser and to proceed{pass} to creation of our script.
Conclusion
For the beginning it is enough of it. If at you the program phpmyadmin is not established, it is possible to download I skin the version here: http://www.phpmyadmin.net/. There is an excellent{a different} documentation, and to establish the program simply enough.
Addition
One of the important features of this script consists that you can add with his help links simply and without additional efforts. Create a new file and to name it add.php. This file can be changed a little that only you (the manager of a site) could add new links, but we this lesson shall do{make} a variant when all visitors can add new links.
starting
As always, to be connected to a database, we need to write the next lines inserting a file connect.php:
<? php
include ("connect.php");
?>
Let's create the form that visitors could fill in her for addition of the new link. I shall simply write below this code, not pressing in details html:
<table border = "0" cellpadding = "0" cellspacing = "0">
<tr>
<td width = "173"> the Name of a site: </td>
<td width = "688">
<input type = "text" name = "site" size = "75">
</td>
</tr>
<tr>
<td width = "173"> url: </td>
<td width = "688">
<input type = "text" name = "url" size = "75">
</td>
</tr>
<tr>
<td width = "173"> the Address of a banner </td>
<td width = "688">
<input type = "text" name = "banner" size = "75">
</td>
</tr>
</table>
<input type = "submit" name = "To send" value = "submit">
And more, that the script remained on the same page, after processing the form, write the next lines:
<? php echo $php_self;?>
So all is simple.
Processing of the form
There is no sense in html to the form if there is no the script processing her . First, it is necessary to check up, whether all data are entered:
if (! empty ($site))
{
// Now we add spec. Symbols successfully to insert into base:
$site = addslashes ($site);
$url = addslashes ($url);
$banner = addslashes ($banner);
// We insert in base:
$sql = " insert into links set site = ' $ site ', url = ' $ url ', banner = ' $ banner ' ";
// If there was a mistake, deduce{remove} the message:
$query = mysql_query ($sql) or die (" cannot query the database. <br> ". mysql_error ());
// If a mistake was not, it is necessary to deduce{remove} the message that all ok?
echo " Recording is added. <br> <br> <a href ='index.php '> Click here to continue. </a> ";
// After all it it is necessary to close brackets and to show html the form:
} else {
?>
After this line there should be a code of the form, and after html a code we should close a bracket:
<? php}?>
If think, that all this is simple, will be farther even easier.
Removal{Distance}
It likely the shortest fragment of a code.
To remove recording, it is necessary to write:
$sql = " delete from links where id = ' $ id ' ";
$query = mysql_query ($sql) or die (" cannot delete record. <br> ". mysql_error ());
Only be convinced, that you have written the lines including a file connect.php. At removal{distance} of recording we should use " where id = " or we shall remove all recordings in the table. So, if all has passed successfully, it is necessary to deduce{remove} the message:
echo " the Link is removed! <br> <br> <a href ='index.php '> Click here to continue. </a> ";
When we cause a script of removal{distance}, he should be called so:
<a href ='delete.php? id=1 '> To remove the link </a>
Conclusion
I congratulate, you have learned:
To create tables in a database
To be connected to base
To add the data
To delete the data
In the following part we shall do{make} the most necessary file showing the links. The initial code of a today's lesson is submitted below:
--------------------------------------------------------------------------------
add.php
<? php
include ("./connect.php");
if (! empty ($site)) {
$site = addslashes ($site);
$url = addslashes ($url);
$banner = addslashes ($banner);
$sql = " insert into links set site = ' $ site ', url = ' $ url ', banner = ' $ banner ' ";
$query = mysql_query ($sql) or die (" cannot query the database. <br> ". mysql_error ());
echo " Recording is added. <br> <br> <a href ='index.php '> Click here to continue </a> ";
} else {
?> <b> To add the link </b>
<form name = "links" method = "post" action = " <? php echo $php_self;?> ">
<table width = "694" border = "0" cellpadding = "0" cellspacing = "0">
<tr>
<td width = "117"> the Name of a site: </td>
<td width = "577">
<input type = "text" name = "site" size = "100">
</td>
</tr>
<tr>
<td width = "117"> url: </td>
<td width = "577">
<input type = "text" name = "url" size = "100">
</td>
</tr>
<tr>
<td width = "117"> the Address of a banner: </td>
<td width = "577">
<input type = "text" name = "banner" size = "100">
</td>
</tr>
</table>
<p>
<input type = "submit" name = "To send" value = "submit">
</p>
</form> <? php
}
?>
delete.php
<? php
include ("./connect.php");
$sql = " delete from links where id = ' $ id ' ";
$query = mysql_query ($sql) or die (" cannot delete record. <br> ". mysql_error ());
echo " the Link is removed! <br> <br> <a href ='index.php '> Click here to continue </a> ";
?>