Welcome to WebmasterWorld Guest from 54.227.157.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

     
5:54 pm on Sep 1, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2008
posts:9
votes: 0


Thanks for the awesome forum! It has helped me quite a bit!

I need some help!

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.

Im not sure about using one of the php random functions since I want to display the code based on the number of users visiting the apge daily: If I get 100 users coming to see this page, and the field is set to the value of 50, I want only 50 of those users to see this code.

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!

9:31 pm on Sept 1, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


Something like this would do the trick, but it's random so you'll get about x percentage, not exactly.

$stored_percentage = 50;
if(rand(1,100) <= $stored_percentage)
{
// echo the code
}

All it does it generate a random number between 1 and 100. Roughly %50 of the time this will be below or equal to 50, 20% of the time it'll be below or equal to 20 etc.

11:17 pm on Sept 1, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Welcome to Webmaster World, isoripper.
How about this:
weight based selection [webmasterworld.com]
11:19 am on Sept 2, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2008
posts:9
votes: 0


The thing is I would like to show the code not using a random function, but actually based on the number of visits recorded, like for every 100 visitors, the code will be displayed 50 times, or dependent on the value set.

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).

Is this explanation clear?

1:30 pm on Sept 2, 2008 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
posts:3416
votes: 0


actually based on the number of visits recorded, like for every 100 visitors, the code will be displayed 50 times, or dependent on the value set

Your explanation is quite clear. We have to deal with probability here because there's no way for PHP to know ahead of time how many times the particular page will be viewed (and no way for PHP to go back in time and say "we got viewed 1,218 times so these 609 pages should have seen frame_1 and these 203 should have seen frame_2, etc.").

You could set up a short weighted list (of, for example, 100 page views), shuffle it up and loop through that list, but even in that situation at some point you'll have 250 page views and those last 50 might not exactly match your proportions. As long as you are dealing with thousands of page views this shouldn't be much of an issue.

1:47 pm on Sept 2, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2008
posts:9
votes: 0


Thanks for the reply!

So, in any case, do I have to deal with probability? Since Im keeping track of the number of visits to a particular page, isnt there a way to do it based on the number of visits already recorded on the db?

My system is the following: I have a list of auctions in a database and each auction has its own id. So when a someone visits auction.php?auction_id=123 a page will be rendered with a hidden iframe in it. The field "ctr" on the database (stands for click trough ratio) records the percentage this iframe should show. This is what the table looks like:

auction_id ¦ auction_content ¦ auction_ctr
123 this is content1 50
1234 this is content2 60
12345 this is content3 10

so when the page renders, I record a hit to the record called in the querystring (ie auction_Id=123). Since Im recording the number of hits to that record, shouldnt I be able to display the iframe 50% of the time? Sorry guys for being redundant, I need this so bad ! The thing is, I can do so much with php but I cant really figure out how to put what I want together!