AceOfJames Posted January 25, 2009 Share Posted January 25, 2009 I have been looking all over for a script that would load 1 of 4 different DISCOUNTS on our site but want them all loaded in a specific order. Meaning that if I go to the site, DISCOUNT 1 would load but if someone comes to the site right after, then they would see DISCOUNT 2 then the next person would see DISCOUNT 3. I keep finding RANDOM code but do not want it randomly shown. I want it in a specified order. Can this code be altered to do that? <?php // this function displays random text in a sentence function random_discount() { $drivers = array( 0 => 'PR0AMA you will receive 10', 1 => 'PR14MA you will receive 20', 2 => 'PR1EMA you will receive 30', 3 => 'PR28MA you will receive 40', 4 => 'PR32MA you will receive 50'); srand ((double) microtime() * 1000000); $random_number = rand(0,count($drivers)-1); $driver = ($drivers[$random_number]); return $driver; } ?> Can anyone help? Thank you in advance! James Quote Link to comment Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 You need to use a database or a flatfile to keep track of which one was used last. The mechanics/coding is pretty much the same as a page hit counter (so google page hit counter tutorials for coding), except for that you will want to include a condition to check whether the last one was reached or not, and if so, reset the counter. You would just use the current number in the file as the array position for setting $driver. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 26, 2009 Share Posted January 26, 2009 1 MySQL table 1 Row 1 Field called "LastAd" Everypage load do this <?php $q = "SELECT LastAd from `table`"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $ad = $row['LastAd']+1; if($ad >4){ $ad = 0; } $q = "UPDATE `table` Set LastAd = '".$ad."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); ?> Making sense Quote Link to comment Share on other sites More sharing options...
AceOfJames Posted January 26, 2009 Author Share Posted January 26, 2009 Hey and thanks again!!!! I need 4 fields though with a different discount in each field. 1 of the fields must be selected and shown on a page then at the next page load it should show the next discount. Does that make sense? How does that work in the code you provided? AceOfJames Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 26, 2009 Share Posted January 26, 2009 Hey and thanks again!!!! I need 4 fields though with a different discount in each field. 1 of the fields must be selected and shown on a page then at the next page load it should show the next discount. Does that make sense? How does that work in the code you provided? AceOfJames You don't You have an array like you did <?php $drivers = array( 0 => 'PR0AMA you will receive 10', 1 => 'PR14MA you will receive 20', 2 => 'PR1EMA you will receive 30', 3 => 'PR28MA you will receive 40', 4 => 'PR32MA you will receive 50'); ?> and integrating with mine you do <?php $drivers = array( 0 => 'PR0AMA you will receive 10', 1 => 'PR14MA you will receive 20', 2 => 'PR1EMA you will receive 30', 3 => 'PR28MA you will receive 40', 4 => 'PR32MA you will receive 50'); $q = "SELECT LastAd from `table`"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $ad = $row['LastAd']+1; if($ad >count($drivers)){ $ad = 0; } $q = "UPDATE `table` Set LastAd = '".$ad."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); echo $driver[$ad]; ?> Quote Link to comment Share on other sites More sharing options...
AceOfJames Posted January 26, 2009 Author Share Posted January 26, 2009 Ok, so I have the database setup as you described: 1 MySQL table 1 Row 1 Field called "LastAd" but (ok, it has been a while and I was not to far advanced to begin with in php/mysql) Where do i put the array in your new snippet? <?php $q = "SELECT LastAd from `table`"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $ad = $row['LastAd']+1; if($ad >4){ $ad = 0; } $q = "UPDATE `table` Set LastAd = '".$ad."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); ?> <?php // this function displays random text in a sentence function random_discount() { $drivers = array( 0 => 'PR0AMA you will receive 10', 1 => 'PR14MA you will receive 20', 2 => 'PR1EMA you will receive 30', 3 => 'PR28MA you will receive 40', 4 => 'PR32MA you will receive 50'); srand ((double) microtime() * 1000000); $random_number = rand(0,count($drivers)-1); $driver = ($drivers[$random_number]); return $driver; } ?> Quote Link to comment Share on other sites More sharing options...
AceOfJames Posted January 26, 2009 Author Share Posted January 26, 2009 In my haste I only ready half your post sorry. I am working on the inetgration! Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 Using the database: <?php $drivers = array( 0 => 'PR0AMA you will receive 10', 1 => 'PR14MA you will receive 20', 2 => 'PR1EMA you will receive 30', 3 => 'PR28MA you will receive 40', 4 => 'PR32MA you will receive 50' ); $query = "SELECT LastAd from `table`"; $result = mysql_query($query) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($result); $NewAd = ($row['LastAd']+1) % count($drivers); $query = "UPDATE `table` Set LastAd = '$NewAd'"; $result = mysql_query($query) or die(mysql_error()."<br /><br />".$q); echo "Curent Ad: {$drivers[$NewAd]}"; ?> Or using a flat file: (drivers.txt) PR0AMA you will receive 10 PR14MA you will receive 20 PR1EMA you will receive 30 PR28MA you will receive 40 PR32MA you will receive 50 PHP Code <?php $drivers_file = 'drivers.txt'; $drivers_list = file($drivers_file); $current_discount = array_shift($drivers_list); $drivers_list[] = $current_discount; $drivers_text = trim(implode("", $drivers_list)); $fileObj = fopen($drivers_file, 'w') or die("can't open file"); foreach($drivers_list as $driver) { fwrite($fileObj, $driver); } echo "Current discount: $current_discount"; Quote Link to comment Share on other sites More sharing options...
AceOfJames Posted January 26, 2009 Author Share Posted January 26, 2009 I got it working with the flatfile version but would really like to know how to do the other way as well using mysql (cooldude). Thanks mjdamato for the flatfile version! Works great! Back to CoolDude... After a few little fixes to your code example it worked but is getting stuck at just showing the 30% discount line and does not show any others. I suspect it to be the way I setup the mysql database? Or is the code below wrong? I had to edited it a bit. Here is how I have it setup. The connection is successful and proper databse is selected. database name = NextDiscount Table = LastAd Field = LastAd Row? Here is the code on the page: <?php function random_discount() { $drivers = array( 0 => 'PR0AMA you will receive 20', 1 => 'PR14MA you will receive 30', 2 => 'PR1EMA you will receive 40', 3 => 'PR32MA you will receive 50'); $q = "SELECT LastAd from `LastAd`"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $ad = $row['LastAd']+1; if($ad >count($drivers)){ $ad = 0; } $q = "UPDATE `LastAd` Set LastAd = '".$ad."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); echo $drivers[$ad]; } ?> Thank you in advance! AceOfJames Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 After you run a page 1 time go into phpmyadmin and verify its updating the value. Also did you import 1 row into the table with some initial value for it? Odds are its pulling 0 rows and thus $ad = 0 0 +1 = 0 the update is updating 0 rows resulting in this loop Quote Link to comment Share on other sites More sharing options...
AceOfJames Posted January 27, 2009 Author Share Posted January 27, 2009 Boom! That solved it! Thanks for all the help! Problem Solved! AceOfJames Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 27, 2009 Share Posted January 27, 2009 I think you are going to need to change this line if($ad >count($drivers)){ $ad = 0; } The drivers array has elements with indexes from 0 to 3, but the count() of the drivers array will be 4. So, when the value is equual to 4 there is no corresponding array value. But, it is also not greater than the count of 4. I think that should be this: if($ad >= count($drivers)){ $ad = 0; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.