Onbekend |
|
Telt kliks op url
Niveau: Beginner
PHP versie: 4-5??
Categorie: Beginners
Door Jeroen op 29.04.2005
Toelichting:
admin.php
Links toevoegen, wijzigen, verwijderen.
index.php
zet de naam van link achter index.php. bijvoorbeeld index.php?name=phphulp.
Dan word bekeken of er een link in de database staat met deze naam, zo ja dan word daarnaartoe doorgeschakelt met header().
Als je dus een link in de database hebt gestopt, en je wilt daarheen verwijzen, en het aantal kliks tellen, dan link je dus naar index.php?name=naamvanlink.
sql:
CREATE TABLE 'links' (
'id' INT NOT NULL AUTO_INCREMENT ,
'name' VARCHAR( 100 ) NOT NULL ,
'link' VARCHAR( 100 ) NOT NULL ,
'kliks' INT NOT NULL ,
PRIMARY KEY ( 'id' )
);
CREATE TABLE 'links' ( 'id' INT NOT NULL AUTO_INCREMENT , 'name' VARCHAR( 100 ) NOT NULL , 'link' VARCHAR( 100 ) NOT NULL , 'kliks' INT NOT NULL , );
index.php:
<?php
// Kliks on links?
$error_no_link="<b>Error:</b> No such name with link in database, Please make sure you entered the right name!"; // make this your own error.
$error_no_get_data="Nothing in Url"; // make this also your own error.
$error_no_kliks_update="There is some problem with counting the kliks."; // make this also your own error.
if(isset($_GET['name'])){
$name=$_GET['name'];
// connection with database
$db=mysql_connect("localhost","root","");
mysql_select_db("test",$db);
// sql
$sql="SELECT name,link,kliks FROM links WHERE name='$name' LIMIT 0,1";
$res=mysql_query($sql);
// is there any link with this name?
$num_rows=mysql_num_rows($res);
if($num_rows==1){
// yes there is.
$link=mysql_fetch_array($res);
$new_number_of_kliks=$link['kliks']+1;
$sql="UPDATE links SET kliks='$new_number_of_kliks' WHERE name='$name'";
$res=mysql_query($sql);
if(!$res) die($error_no_kliks_update);
header("Location: " . $link['link']);
}
else{
// no there isn't.
echo $error_no_link;
}
mysql_close();
}
else{
echo $error_no_get_data;
}
?>
<?php // Kliks on links? $error_no_link="<b>Error:</b> No such name with link in database, Please make sure you entered the right name!"; // make this your own error. $error_no_get_data="Nothing in Url"; // make this also your own error. $error_no_kliks_update="There is some problem with counting the kliks."; // make this also your own error. if(isset($_GET['name'])){ $name=$_GET['name']; // connection with database // sql $sql="SELECT name,link,kliks FROM links WHERE name='$name' LIMIT 0,1"; // is there any link with this name? if($num_rows==1){ // yes there is. $new_number_of_kliks=$link['kliks']+1; $sql="UPDATE links SET kliks='$new_number_of_kliks' WHERE name='$name'"; if(!$res) die($error_no_kliks_update); header("Location: " . $link['link']); } else{ // no there isn't. } } else{ } ?>
Admin.php:
<?php
// Kliks on links?
// errors
$error_no_link_in_database="There are no links in the database"; // make this your own error.
$self=$_SERVER['PHP_SELF'];
// connection with database
$db=mysql_connect("localhost","root","");
mysql_select_db("test",$db);
// some functions
Function DeleteLink($id){
$sql="DELETE FROM links WHERE id='$id'";
$res=mysql_query($sql);
if($res) echo "Your link is deleted.";
if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}
Function NewLink($name,$link){
$sql="INSERT INTO links (name,link) VALUES ('$name','$link')";
$res=mysql_query($sql);
if($res) echo "New link submitted.";
if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}
Function UpdateLink($id,$name,$link){
$sql="UPDATE links SET id='$id',name='$name',link='$link' WHERE id='$id'";
$res=mysql_query($sql);
if($res) echo "Link is updated";
if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}
Function ShowUpdateForm($id){
global $self;
$sql="SELECT * FROM links WHERE id='$id' LIMIT 0,1";
$res=mysql_query($sql);
$num_rows=mysql_num_rows($res);
if($num_rows==1){
$link=mysql_fetch_array($res);
$name=$link['name'];
$link=$link['link'];
echo<<<UPDATEFORM
<form action=$self method=POST>
<input type=hidden name=id value=$id />
<table>
<tr><td>Name</td><td><input type=text name=name value="$name" /></td></tr>
<tr><td>Link</td><td><input type=text name=url value="$link" /></td></tr>
<tr><td colspan=2><input type=submit value=Change name=link /></td></tr>
</table>
</form>
UPDATEFORM;
}
else{
echo "This link isn't in the database, or there is a problem.";
}
}
// nu kijken wat er moet gebeuren.
if(isset($_POST['link']) and $_POST['link']=="Insert"){
NewLink(addslashes($_POST['name']),$_POST['url']);
}
elseif(isset($_POST['link']) and $_POST['link']=="Update"){
ShowUpdateForm($_POST['id']);
}
elseif(isset($_POST['link']) and $_POST['link']=="Delete"){
DeleteLink($_POST['id']);
}
elseif(isset($_POST['link']) and $_POST['link']=="Change"){
UpdateLink($_POST['id'],addslashes($_POST['name']),$_POST['url']);
}
$sql="SELECT * FROM links ORDER BY name";
$res=mysql_query($sql);
$num_rows=mysql_num_rows($res);
if($num_rows>=1){
// table
echo "<table>";
echo "<tr><td><b>Name:</b></td><td><b>Link:</b></td><td><b>Number of kliks:</b></td><td></td></tr>";
// row(s)
while($link=mysql_fetch_array($res)){
echo "<tr><td>" . $link['name'] . "</td><td>" . $link['link'] . "</td><td>" . $link['kliks'] . "</td><td><form action=" . $_SERVER['PHP_SELF'] . " method=POST><input type=hidden name=id value=" . $link['id'] . " /><input type=submit value=Update name=link /> <input type=submit value=Delete name=link /></form></td></tr>";
}
echo "</table><hr size=1 />";
}
else{
echo $error_no_link_in_database;
}
echo <<<NEWFORM
<form action=$self method=POST />
<table>
<tr><td>Name:</td><td><input type=text name=name /></td></tr>
<tr><td>Link:</td><td><input type=text name=url /></td></tr>
<tr><td colspan=2><input type=submit name=link value=Insert /></td></tr>
</tr>
</table>
</form>
NEWFORM;
mysql_close();
?>
<?php // Kliks on links? // errors $error_no_link_in_database="There are no links in the database"; // make this your own error. $self=$_SERVER['PHP_SELF']; // connection with database // some functions Function DeleteLink($id){ $sql="DELETE FROM links WHERE id='$id'"; if($res) echo "Your link is deleted."; if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem."; } Function NewLink($name,$link){ $sql="INSERT INTO links (name,link) VALUES ('$name','$link')"; if($res) echo "New link submitted."; if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem."; } Function UpdateLink($id,$name,$link){ $sql="UPDATE links SET id='$id',name='$name',link='$link' WHERE id='$id'"; if($res) echo "Link is updated"; if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem."; } Function ShowUpdateForm($id){ $sql="SELECT * FROM links WHERE id='$id' LIMIT 0,1"; if($num_rows==1){ $name=$link['name']; $link=$link['link']; <form action=$self method=POST> <input type=hidden name=id value=$id /> <table> <tr><td>Name</td><td><input type=text name=name value="$name" /></td></tr> <tr><td>Link</td><td><input type=text name=url value="$link" /></td></tr> <tr><td colspan=2><input type=submit value=Change name=link /></td></tr> </table> </form> UPDATEFORM; } else{ echo "This link isn't in the database, or there is a problem."; } } // nu kijken wat er moet gebeuren. if(isset($_POST['link']) and $_POST['link']=="Insert"){ NewLink (addslashes($_POST['name']),$_POST['url']); } elseif(isset($_POST['link']) and $_POST['link']=="Update"){ ShowUpdateForm($_POST['id']); } elseif(isset($_POST['link']) and $_POST['link']=="Delete"){ DeleteLink($_POST['id']); } elseif(isset($_POST['link']) and $_POST['link']=="Change"){ UpdateLink ($_POST['id'],addslashes($_POST['name']),$_POST['url']); } $sql="SELECT * FROM links ORDER BY name"; if($num_rows>=1){ // table echo "<tr><td><b>Name:</b></td><td><b>Link:</b></td><td><b>Number of kliks:</b></td><td></td></tr>"; // row(s) echo "<tr><td>" . $link['name'] . "</td><td>" . $link['link'] . "</td><td>" . $link['kliks'] . "</td><td><form action=" . $_SERVER['PHP_SELF'] . " method=POST><input type=hidden name=id value=" . $link['id'] . " /><input type=submit value=Update name=link /> <input type=submit value=Delete name=link /></form></td></tr>"; } echo "</table><hr size=1 />"; } else{ echo $error_no_link_in_database; } <form action=$self method=POST /> <table> <tr><td>Name:</td><td><input type=text name=name /></td></tr> <tr><td>Link:</td><td><input type=text name=url /></td></tr> <tr><td colspan=2><input type=submit name=link value=Insert /></td></tr> </tr> </table> </form> NEWFORM; ?>
Ben je hier iets mee? |