one random number per day

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nightmaster
Forum Newbie
Posts: 5
Joined: Sun Nov 15, 2015 12:21 pm

one random number per day

Post by Nightmaster »

Hey guys.

I know it's been a while since previous posts were written, but while googling around I found it and I used it to make simple thing. I used following code:

Code: Select all

    if ($row['Date'] != date("Y-m-d")) {
         //Then it must be the next day
         $Number = 0;
         while($Number == $row['Number'] || $Number == 0) { //Keep looping through until we really get a new number
             $Number = rand(0,10); //0 is Minimum, 10 is Maximum. Adjust as necessary;
         }
         //We have a new number now, so just save it to the database and we are done
         $novisql = "Update RandomNumber set Date = 2017-11-12, Number = ".$Number."";
			if ($conn->query($novisql) === TRUE) {
				echo "Record updated successfully";
			} else {
				echo "Error updating record: " . $conn->error;
			}
    } else {
        $Number = $row['Number'];
    }
    //OK, we now have our random number `$Number` and the database is up to date
Do note that the query is updating Number at every load, while my Date field in database is always 0000-00-00. Whatever I write in sql query it's the same. Not really sure what I'm doing wrong, maybe I did something wrong in database itself, I just used type date for that field?

Any help would be appreciated!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: one random number per day

Post by requinix »

Split from one random number per day.

Why use a database? The other thread showed a really easy way to do it: mt_srand(something according to the date) then mt_rand(). If you want it to change between days then there are other similar solutions that don't need a database and definitely don't need a loop.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: one random number per day

Post by Christopher »

Your query is failing because of quotes. And your logic is flawed.

Code: Select all

    $date = date("Y-m-d");
    if ($row['Date'] != $date) {
         //Then it must be the next day
         $Number = 0;
         while($Number == $row['Number'] || $Number == 0) { //Keep looping through until we really get a new number
             $Number = rand(0,10); //0 is Minimum, 10 is Maximum. Adjust as necessary;
         }
         //We have a new number now, so just save it to the database and we are done
         $novisql = "UPDATE RandomNumber SET Date = '$date', Number = $Number";
			if ($conn->query($novisql) === true) {
				echo "Record updated successfully";
			} else {
				echo "Error updating record: " . $conn->error;
			}
    } else {
        $Number = $row['Number'];
    }
    //OK, we now have our random number `$Number` and the database is up to date
(#10850)
Nightmaster
Forum Newbie
Posts: 5
Joined: Sun Nov 15, 2015 12:21 pm

Re: one random number per day

Post by Nightmaster »

Thanks for your help, that seems to be working fine.

As I'm still learning php, can you please explain why when using first variable ($date) single quotes should be used, and when using second variable ($Number) there's no quotes? I'm just curious.

Also, is there something else wrong with the statement apart from that?

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: one random number per day

Post by Christopher »

Nightmaster wrote:can you please explain why when using first variable ($date) single quotes should be used, and when using second variable ($Number) there's no quotes? I'm just curious.
It has nothing to do with using a variable like $date. I only added $date because you want to against today's date and of it fails then save today's date. Hence the variable.

In SQL, numbers can be assigned with our without quotes, so: Number=5 or Number='5'.

But with dates and strings, you must use quotes, so: Date='2000-01-01' or Name='Joe'.

Your original code was: Date=2000-01-01 which would cause an error. You can probably see why this ambiguity is not allowed ... is that a date? or is it the value 1998 (i.e., 2000 - 1 - 1)
(#10850)
Nightmaster
Forum Newbie
Posts: 5
Joined: Sun Nov 15, 2015 12:21 pm

Re: one random number per day

Post by Nightmaster »

requinix - I thought that database would be the best choice here. I need a script which would add me random number - each 24 hours.
Christopher - I understand, thank you for explanation, I wasn't aware that I need to use single quotes for dates, will keep that in mind!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: one random number per day

Post by Christopher »

requinix wrote:Split from one random number per day.

Why use a database? The other thread showed a really easy way to do it: mt_srand(something according to the date) then mt_rand(). If you want it to change between days then there are other similar solutions that don't need a database and definitely don't need a loop.
I agree with what requinix is saying. My question is whether you need one random number per day for all users or each user. There are several possible solutions. If it is one number per day for all users, then I think a cron job that wrote a PHP script with the daily value set might be the simplest and most efficient solution.
(#10850)
Nightmaster
Forum Newbie
Posts: 5
Joined: Sun Nov 15, 2015 12:21 pm

Re: one random number per day

Post by Nightmaster »

Oh, I see. I need it for all users. However I never actually used cronjob, so I'm kinda stuck there.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: one random number per day

Post by Christopher »

Nightmaster wrote:Oh, I see. I need it for all users. However I never actually used cronjob, so I'm kinda stuck there.
It's pretty easy. If your hosting has a control panel then you can schedule a script to be run at any interval -- nightly in your case.

Run this script nightly:

Code: Select all

<?php
mt_srand(crc32(date('D d F Y')));
$rand = mt_rand(1,100);
$output = "<?php\n\$RandomNumber = $rand;\n";
file_put_contents('/path/to/random_number.php', $output);
Wherever you need to random number just:

Code: Select all

include '/path/to/random_number.php';
On Linux command line run crontab -e and enter something like:[text]# once a day at 1:00 am
00 1 * * * php /path/to/generate_random_number.php;[/text]
(#10850)
Post Reply