Hey guys, wondering if you could help me with these two bits of code.

What I'm trying to do is count how many clicks a link has been clicked, then when it has been clicked x amount of times, it echos 'test'

Now the code below does that, but I've used a session, so it currently only counts when the page is refreshed.

<?php 
session_start(); 
($_SESSION['count']) ? $_SESSION['count']++ : $_SESSION['count'] = 1;

if ($_SESSION['count'] % 5 == 0)
{ 
        echo "test";
}

?>

Is there anyway to get this to apply only when the link below has been clicked:

<a href="index.php?act=generate_quotes&cb=<?php echo time();?>" class="main_control">Generate</a></p>

So when that link has been clicked it THEN begins counting? I've not really used sessions before so count someone maybe show me how this would be done?

And this may help:

The link I want to apply it to is calling the following function:

<?php
if($_GET['act'] == "generate_quotes") {

	$con = mysql_connect("*", "*", "*");
	if (!$con)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	mysql_select_db("quote", $con);
				 
	$result = mysql_query( ' SELECT * FROM `quote`  ORDER BY RAND() LIMIT 0,1 ' );

	while($row = mysql_fetch_array($result))
  	{
 		echo '' . $row['q_quote'] . '';
 	}
	mysql_close($con);

} else { 

	echo "<img src='img/vpbgt.png' alt='hello'>";
	
}
?>

Thanks for any info.

Recommended Answers

All 2 Replies

<?php 
session_start(); 

if (isset($_GET['beginCount'])) $_SESSION['countView'] = true;
if ($_SESSION['countView']) $_SESSION['count']++;
if ($_SESSION['count'] % 5 == 0)
{ 
        echo "test";
}
?>
<a href="index.php?act=generate_quotes&beginCOunt&cb=<?php echo time();?>" class="main_control">Generate</a></p>

You can move if (isset($_GET)) $_SESSION = true; and change it to $_SESSION = true; into your function if you prefer, it just has to change the session information. That way it can begin counting.

That's fantastic, thanks a lot for that, +1 :)

But just a typo on one line encase anyone else needs to use this, the link should be:

<a href="index.php?act=generate_quotes&beginCount&cb=<?php echo time();?> " class="main_control">Generate</a> </p>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.