PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 16, 2017 3:24 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Sun Nov 26, 2017 11:45 am 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 1:21 pm
Posts: 5
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:
Syntax: [ Download ] [ Hide ]
    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!


Top
 Profile  
 
PostPosted: Sun Nov 26, 2017 12:40 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6607
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Sun Nov 26, 2017 1:15 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13459
Location: New York, NY, US
Your query is failing because of quotes. And your logic is flawed.
Syntax: [ Download ] [ Hide ]
    $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)


Top
 Profile  
 
PostPosted: Sun Nov 26, 2017 4:19 pm 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 1:21 pm
Posts: 5
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!


Top
 Profile  
 
PostPosted: Sun Nov 26, 2017 8:55 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13459
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Mon Nov 27, 2017 6:32 am 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 1:21 pm
Posts: 5
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!


Top
 Profile  
 
PostPosted: Mon Nov 27, 2017 11:43 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13459
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Mon Nov 27, 2017 7:28 pm 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 1:21 pm
Posts: 5
Oh, I see. I need it for all users. However I never actually used cronjob, so I'm kinda stuck there.


Top
 Profile  
 
PostPosted: Mon Nov 27, 2017 11:42 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13459
Location: New York, NY, US
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:
Syntax: [ Download ] [ Hide ]
<?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:
Syntax: [ Download ] [ Hide ]
include '/path/to/random_number.php';

On Linux command line run crontab -e and enter something like:
Syntax: [ Download ] [ Hide ]
# once a day at 1:00 am
00 1 * * *     php /path/to/generate_random_number.php;

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group