Click to See Complete Forum and Search --> : Image Count ....
systech44
Jan 5th, 2007, 12:18 AM
Hi,
I have a problem in PHP. I have three images with three separate hyperlinks. These are coming randomly one at a time. I have done this part.
Now I would like to count how many times an individual image is clicked by the user with the following restrictions,
1. I use only PHP.
2. I will not use any scripting language and HTML.
3. I will store the values in a table with three separate rows.
Please help.
Thank you so much in advance.
dclamp
Jan 5th, 2007, 12:42 AM
You will need a seperate page for this.
The img link should look like this:
yoursite.com/image_click.php?img=1
on the seperate page:
$Img = $_GET['img'];
$sql = "SELECT * FROM `img_clicks` WHERE imgid = '".$Img."' LIMIT 1";
$result = mysql_query($sql);
$Array = mysql_fetch_array($result);
$Clicks = $Array['total_clicks'] + 1;
$sql = "UPDATE `img_clicks` SET total_clicks = '".$Clicks."' WHERE imgid = '".$Img."'";
$result = mysql_query($sql);
kows
Jan 5th, 2007, 01:48 AM
well, if you're using PHP you've already broken the second restriction you made. PHP is a scripting language.
anyway, the simplest way to do this on a small scale would be to use a text file to keep track of how many times something was clicked. on a larger scale, you would want to use a database because of the ease of use and the speed you'll get from it.
in the following example, I will use text files to store the number of clicks received. it's all contained in one script. I'm sorry if my file handling is not "optimal," but I don't usually work with opening/reading from files at the same time @_@. feel free to correct my method of opening with read, then reopening with write, if there is a better way of doing both with one fopen() call.
<?php
//the array of images/links
$this[] = array("image" => "image001.gif",
"link" => "http://domain.com/page001.htm");
$this[] = array("image" => "image002.gif",
"link" => "http://domain.com/page002.htm");
$this[] = array("image" => "image003.gif",
"link" => "http://domain.com/page003.htm");
$this[] = array("image" => "image004.gif",
"link" => "http://domain.com/page004.htm");
$this[] = array("image" => "image005.gif",
"link" => "http://domain.com/page005.htm");
if(isset($_GET['random'])){
//find a random entry
$i = rand(0, count($this) - 1);
//display that entry
?>
<a href="./?redirect=<?php echo $i + 1; ?>"><img src="<?php echo $this[$i]['image']; ?>" /></a><br />
<?php
}elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect']) && $_GET['redirect'] > 0){
$r = $_GET['redirect'] - 1;
if(!isset($this[$r])) exit;
//something was clicked, so first let's record it
$fp = fopen("record.txt", "r");
$fr = fread($fp, filesize("record.txt"));
$records = explode("|", $fr);
fclose($fp);
if(count($records) == 0){
//no records exist. we need to create them
$newrecords = "";
for($j = 0; $j < count($this); $j++){
if($j == $r)
$newrecords .= "1|";
else
$newrecords .= "0|";
}
}else{
//records do exist, so let's rewrite them
$records[$r]++;
$newrecords = "";
for($j = 0; $j < count($this); $j++){
if(!isset($records[$j])) $records[$j] = 0;
$newrecords .= $records[$j] . "|";
}
}
$fp = fopen("record.txt", "w");
fwrite($fp, $newrecords);
fclose($fp);
//now, we redirect them
header("Location: " . $this[$r]['link']);
}elseif(isset($_GET['display'])){
//display the amount of clicks
?>
<table width="95%" border="1">
<tr>
<th width="50%">link</th><th width="40%">image</th><th width="10%">clicks</th>
</tr>
<?php
$fp = fopen("record.txt", "r");
$fr = fread($fp, filesize("record.txt"));
fclose($fp);
$records = explode("|", $fr);
for($i = 0; $i < count($this); $i++){
$link = (isset($this[$i])) ? $this[$i]['link'] : 'not set';
$image = (isset($this[$i])) ? $this[$i]['image'] : 'not_set.gif';
$clicks = (isset($records[$i])) ? $records[$i] : 0;
?>
<tr>
<td><?php echo $link; ?></td><td><img src="<?php echo $image; ?>" /></td><td><?php echo $clicks; ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
this is probably not the best way to do this, I'd much rather use a database. but, if you're just looking for simple and don't want to expand your knowledge of PHP (or you might not have access to a database), then this would probably work fine for you. for my purposes, I named this script index.php and to pick a random image you would call index.php?random. if you clicked it, it would quickly call index.php?redirect=(the_number_you_picked), and then redirect you after finishing rewriting the file. to display the stuff, you just need to call index.php?display and it will give you a table with all the details.
to show you this script does work, I have a working demo for you:
this is the randomly generated image/link (the image will be broken because i don't have any images, and if you click it will you be redirected to a website that does not exist, be forewarned!) (http://www.cowarama.com/clicks/index.php?random)
this is the display page, which just displays a table of all the information you were keeping track of (http://www.cowarama.com/clicks/index.php?display)
also, please realize that if you edit the order of the links, your clicks and stuff will be wrong. if you need to change the links and stuff a LOT, I suggest using a database instead.
remember, this is just the basics. if you're expecting to deploy this somewhere, you'll have to do a bunch of editing. but, everything you asked for is right here.
dclamp
Jan 5th, 2007, 01:35 PM
What was wrong with my code?
kows
Jan 5th, 2007, 03:21 PM
did you read my post or even look at the block of code I had? it has nothing to do with databases and uses just text files. Like I mentioned in the post, it's very simple and would be useful if you:
don't have access to any type of database
don't want to invest into learning how to manipulate databases with PHP
just want a simple, quick method
It's not the ideal way, but everything in this world is not ideal.
now, what's wrong with your code? for one, you're querying the database twice when you could just query it once. for example, this is much more efficient if you're not actually going to SHOW the user the number of clicks:
UPDATE table_name SET clicks=clicks+1 WHERE id=23
otherwise, I guess you could say selecting all elements from a table (using SELECT *) is bad when you're only manipulating one of them. this makes your query faster, so instead of SELECT * you should use SELECT clicks instead. If you want to get nit-picky, you're using mysql_fetch_array() when you're trying to grab just one record instead of its sister function, mysql_fetch_assoc(). When grabbing just one record from the database, it's faster than mysql_fetch_array(). Then, if you want to get even more nit-picky, you're opening yourself up for some type of SQL injection by not validating your $_GET and inserting it directly into your query string.
and besides, I hit reply when I first read it (around 11:30 my time, his post was at 11:18) and had a call to do some things. when I eventually got back, I wrote some stuff and posted, so I didn't even know that you had posted before me in the first place.
dclamp
Jan 6th, 2007, 01:21 AM
:( im sorry. lol. Hope i didnt make you mad.
If any of these things helped, please mark this post as RESOLVER and dont forget to give Rep. ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.