1

Hi I have a sql query in php

SELECT * FROM catalogue order by RAND() LIMIT 5

How would I limit it to displaying that same record for an entire week without manually executing the query each week?

It currently randomizes a different record with every page refresh.

I have searched the forum for similar solutions, but they involve cron jobs which I do not want to do.

4
  • 1
    Store the random data once a week in another table + read data from that table during the week Commented Oct 19, 2017 at 10:22
  • Is there a method without having to recreate the tables? I understand the logic behind your soluton, but not really what I am looking for thanks. Commented Oct 19, 2017 at 10:25
  • @B001 which would probably involve either a cron job or a MySQL scheduled event - so you would need to explain that to the OP Commented Oct 19, 2017 at 10:26
  • @JediMasterOrion it is possible to do this without a scheduled job (cron or other type), but from a performance and complexity point of view, scheduled jobs are a lot simpler. Btw, you do not need to recreate the table. You can just add a field to the existing one, which would indicate if a record is selected in the current week. Your scheduled job would simply update this particular field. Commented Oct 19, 2017 at 10:30

3 Answers 3

1

Here are some possible solutions:

Seed your rand with the week number

In php, do

$seed = intval(date('oW')); # o will return the "Week year", which is the year the current ISO week mostly corresponds to. W will return the ISO week number

And use the sql statement

SELECT * FROM catalogue order by RAND(?) LIMIT 5

And bind this value to $seed, resulting in something like

SELECT * FROM catalogue order by RAND(201751) LIMIT 5

This will seed your rand, meaning all RAND() calls with the same seed (which is your week number and week-year) will be the same.

Seed your rand with a week number entirely in SQL

SELECT * FROM catalogue ORDER BY RAND(WEEK(NOW())) LIMIT 5

Cache your data in a static file for a week

function getDataOncePerWeek(
    $static_cache_file = 'your/cache/dir/your_cache_file.json';
    $start_of_week = strtotime('this week 00:00:00');

    if (!file_exists($static_cache_file) ||  filemtime($static_cache_file) < $start_of_week) {
        touch($static_cache_file); // Just update the last changed time, so we only try to re-cache the file once
        $tmpFileName = tempnam(pathinfo($static_cache_file, PATHINFO_DIRNAME), 'tmp_');
        $data_from_your_sql = queryAndFetchYourSqlData(); //implement this yourself for your query
        file_put_contents($tmpFileName, json_encode($data_from_your_sql), LOCK_EX); // Save your new cache to a temporary file
        rename($tmpFileName, $static_cache_file);
    }
    return json_decode(file_get_contents($static_cache_file), true);
}

This solution can also be used to generate html from you sql data, and save this to the cache instead. That is, if your html block doesn't need to change during this week.

Cache your data in a database

Set up a caching table which has the same definition as catalogue, but also has a timestamp column which you can look at instead of filemtime() to determine if you need to generate your cache.

Sign up to request clarification or add additional context in comments.

Comments

0

You could store the week number in another table in the database together with the record, and then every time it runs the file, it makes sure that Date(W) is not the same as whats in database, if it is, then it just gets the data in there already, if it is not, then it updates with the new data from the other function

Comments

0

You can do this using only SQL.

Get the current week number (using WEEK(CURRENT_TIMESTAMP())) and use it as a seed to your random function

ORDER BY RAND(WEEK(CURRENT_TIMESTAMP()))

Documentation: WEEK, CURRENT_TIMESTAMP(), RAND

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.