Jump to content

[SOLVED] not random


AceOfJames

Recommended Posts

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

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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];
?>

Link to comment
Share on other sites

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;

} ?>

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.