Jump to content

How to show Section of Code X amount of times based on value stored in DB?


BrazilMac

Recommended Posts

I have a table field in a database that stores a value between 20 and 100 that I want to use it as a percentage of how many times a section of code will be displayed for a particular record. So at any time, I may have 10 or 20 records and each has its on field storing this value.

 

I am tracking all visits to a php page, but I want to display a piece of code based on the value stored on that table field.

 

Basically, if the the field has a value of 50, 50% of the time I want to echo a piece of html code. But I have no idea how to put this into php!!

 

Can anybody help me on that one?

 

Thanks all masters!

 

 

you would do something along the lines of

 

<?php
$percentChance = 20;

if(mt_rand(1,100) <= $percentChance)
{
   echo 'You got lucky, there was only a 20% chance this code would execute!';
}
?>

 

Except retrieve the $percentChance from a database... for info on how to do that google 'mysql data retrieval'

Thanks for the input!!!

 

However, Im puzzled by the fact that how will php keep track of the numbers of visits? I have pasted the code and ran it and it will do exactly what it says, but if I have different users visiting the page throughout the day, how will it keep track of the number of visits to display the code the right amount of times?

visits? I'm sorry, I just posted how to display something based upon a probability. I didn't read anything about visits... or I just assumed you already had it implemented based upon when you said this...

 

Basically, if the the field has a value of 50, 50% of the time I want to echo a piece of html code. But I have no idea how to put this into php!!"

is all I answered.

 

Can you describe exactly what you want, and exactly what you have implemented already?

Thanks again for your reply!!!

 

Basically, I have a page where I log every single visit to that particular page. The visitors access the page from a link such as:

 

page.php?id=123

 

page.php retrieves the record, and renders the document with a hidden iframe in it. This page also inserts each visitors info (ip, referer, etc) in another table in the database.

 

Now, I need to show this hidden iframe X% of the time based on the field where I stored the value in question.

 

so page.php?id=123 retrieves the record from info_table:

 

page_id

page_iframe

page_clicks

 

Now, if page_clicks is 50, I want the iframe to only show 50% of the time. So, nothing random. I need php to make sure that for every 100 visitors, it will display the iframe 50 times (if the page_clicks is 50).

 

Did I explain myself properly???  :) :) :) :)

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.