How do I have a counter for a link?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post by mabufo »

but it gives you a clue... notice the http://www.shiver7.com/out.php....


*cool website by the way
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Ok.

You will also need to use PHP with each of your links then as well if you want a mouse-over display of the 'click' count.

For each link you will need to find out how many times that link has been clicked (see the previous code that has been posted to work out how).

Then on each link you add alt="Number of clicks: <?php echo($clicks); ?>"
and that should be that.

If you created your site by yourself then you shouldn't have any problems sorting this out. If you've just done a PHP cut 'n paste job on your site then it might be a bit harder.
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post by Black Majin »

Ok hey, I have the main.php in the home directory but I have the other files in another folder. So how would I do the alt code? :)
Cause I checked the txt file and it has the number of clicks everytime I click the link so I know that part works. Just need some help on showing it when mouse over.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

For each link you will need to pull the 'click' info into PHP from the txt files.. it's pretty much the same code as you use for the 'click' count updates.

(I take it the page with the links on it is has a .php extension else this won't work)

On all of the links which you have set-up to be recorded you need to add an ALT tag which basically tells the link to show some text when the mouse is rolled over it... so... an example link would look like this...

Code: Select all

&lt;a href="links.php?sitename=name_of_site"&gt;&lt;img src="theimage.gif" width=what_ever height=what_ever ALT="&lt;?php dolink('name_of_site'); ?&gt;"&gt;&lt;/a&gt;
Notice we have PHP in the ALT tag which we can do as long as the page has the .php extension.

Now somewhere on your page (near the top is good) you need to add this code. What this code does is sort out the count info for each link.
If the .txt files are in a different folder to the webpage you will need to change the $PATH variable so that it points to the folder your .txt files are in.

Code: Select all

<?php 

function dolink($sitename)
{
$PATH = "textfiles/";

//list of links 
if($sitename=="site_name_one") { $countfile = $PATH."site_name_one.txt"; } 
if($sitename=="site_name_two") { $countfile = $PATH."site_name_two.txt"; } 

// open count file and read the current value 
$fp=fopen($countfile,"r"); 
$count=fread($fp,filesize($countfile)); 
fclose($fp); 

// now put the text we want to display in the ALT tag of the link.
echo("Link Count = ".$count);
}

?>
If everything is done correctly this should work ok.
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post by Black Majin »

ok now so ur sayin I have to have the code in the main.php and in the links.php?! Yeah i'm confused cause it says links.php in the url for the link but u also said to copy that code to the top of the page. Just making sure, do I do both?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Yep you need the links to link to the links.php page which updates the clicks and sends the user to the relevant url. The code for this is the first lot (on the previous page).

The last lot of code (above) needs to go into the page where the links are.. basically the main website page. This will sort out the stuff needed to display the click-count using mouse-overs.

Make sense?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

Black Majin, try to learn something!. you ask how to register the clics. ok, for that use Gen-ik code.

Now this:
I mean I just dont see anywhere in that code where it shows the amount of clicks when u do a mouse over
is another problem!!!

read and try those codes before posting anything here
Post Reply