Solved

PHP Algorithm

Posted on 2013-01-30
2
268 Views
Last Modified: 2013-01-30
Hello,

I have a table in mysql which all it does is hold a value, a counter, every time someone visits my website, it adds value = value + 1.

Every 51 visitors should see a popup saying that they win something:

function is_twelve_pack_winner()
{
	global $custom_conn;
	$sql = "SELECT * from code_win_count LIMIT 1";
	$rs = $custom_conn->execute($sql);
	$count = $rs->fields['code_wins'];

}

Open in new window


So if 50 people visit the website, visitor number 51 should get a popup saying that they can redeem a coupon... if 100 people visit the website, number 101 will also get a popup saying that they win something and so on and so on...

any ideas?
0
Comment
Question by:nm_mario
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 38838109
One consideration you should think about is whether you are counting "page loads", or "unique visitors". You most likely want to popup every 51 unique visitors. That way a user can't just hit refresh 50 times and win the prize. You can count unique visitors by examining their IP address. If they have already visited the page at some point today their page load shouldn't increment the counter.

What I'd suggest is have your MySQL table hold a list of IPs, the number of times they have visited the page, and the last day of the year they were on the page.

For example:

Visitors Table:
     visitorid       integer primary key
     ip                  varchar
     lastvisit        int
     visitcount    int

Every time a visitor hits your website, your code does the following:

    - Find the record in MySQL for that user's IP

   $visitorip = $_SERVER["REMOTE_ADDR"]

   SELECT * FROM Visitors where ip = '$visitorip'

    - If the day of the year is different than lastvisit field, bump the visitcount

     $dayofyear = date('z');
    if( $dayofyear != $row["lastvisit"] ) {
          // update the table
    }

    Update the table using SQL like this:

         UPDATE Visitors set visitcount = visitcount+1,  lastvisit = '$dayofyear' LIMIT 1


To determine if you should be popping up the popup, you want to sum all of the visit counts together, e.g.

SELECT SUM(visitcount) FROM Visitors

Then check if it is divisible by 51 in PHP, for example:

e.g.

   if(    $totalvisits  % 51   == 0  ) {
        // the popup should be shown
   }
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 38838334
I wouldn't use IP because if you have multiple people behind a firewall they will all have the same IP.

Rather use the session ID or put a cookie on their machine.

Also $totalvisits % 51 won't give you you the right answer.

This will give you the

51
102
153

Etc

What you actually want is

if ($totalvists % 50 == 1) {
}
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
database connection error mysql stops 7 82
How do i use the await event in php echo 5 44
Dump data from mysql to xls php 10 56
integrated two Separate file . 3 30
Creating and Managing Databases with phpMyAdmin in cPanel.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question