BrazilMac Posted September 1, 2008 Share Posted September 1, 2008 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! Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/ Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 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' Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/#findComment-631266 Share on other sites More sharing options...
BrazilMac Posted September 1, 2008 Author Share Posted September 1, 2008 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? Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/#findComment-631294 Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 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? Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/#findComment-631359 Share on other sites More sharing options...
BrazilMac Posted September 1, 2008 Author Share Posted September 1, 2008 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??? :) :) Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/#findComment-631394 Share on other sites More sharing options...
BrazilMac Posted September 2, 2008 Author Share Posted September 2, 2008 Anyone can help me with this??? Link to comment https://forums.phpfreaks.com/topic/122254-how-to-show-section-of-code-x-amount-of-times-based-on-value-stored-in-db/#findComment-631806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.