This script is based on the creation of a column that’s repopulated each time the script runs. It would be great to randomly re-order just the mysql resource from the query. Is that even possible?
Here’s my script:
require_once "connect_to_mysql.php";
mysql_query("UPDATE preplan set random_num = RAND()") or die(mysql_error());
require_once "connect_to_mysql.php";
$act_directs = mysql_query("SELECT * FROM preplan WHERE TIMESTAMP(NOW()) BETWEEN TIMESTAMP(begin) AND TIMESTAMP(end) ORDER BY random_num ASC") or die(mysql_error());
$act_directs2 = mysql_fetch_array($act_directs);
$id = $act_directs2['id'];
echo $id;
<?php
require_once "connect_to_mysql.php";
$sql="
SELECT
*
FROM
preplan
WHERE
TIMESTAMP(NOW()) BETWEEN TIMESTAMP(begin)
AND
TIMESTAMP(end)
";
$result = mysql_query($sql) or die(mysql_error());
while ( $row = mysql_fetch_array($result) {
$act_directs[] = $row;
}
$random_entry = array_rand($act_directs,1);
echo $random_entry['id'];
?>
If you want all records then just use shuffle() instead of array_rand()
I think the script is returning the index. How do I return the data?
This is a dump of my table:
array(4) { [0]=> string(1) “1” [“id”]=> string(1) “1” [1]=> string(1) “f” [“alpha”]=> string(1) “f” }
array(4) { [0]=> string(1) “2” [“id”]=> string(1) “2” [1]=> string(1) “e” [“alpha”]=> string(1) “e” }
array(4) { [0]=> string(1) “3” [“id”]=> string(1) “3” [1]=> string(1) “d” [“alpha”]=> string(1) “d” }
array(4) { [0]=> string(1) “4” [“id”]=> string(1) “4” [1]=> string(1) “c” [“alpha”]=> string(1) “c” }
array(4) { [0]=> string(1) “5” [“id”]=> string(1) “5” [1]=> string(1) “b” [“alpha”]=> string(1) “b” }
array(4) { [0]=> string(1) “6” [“id”]=> string(1) “6” [1]=> string(1) “a” [“alpha”]=> string(1) “a” }
echo $random_entry[‘id’]: produces a blanks screen.
echo var_dump($random_entry) produces a result like this: int(5) - where the value inside the parens changes
echo $random_entry produces a single integer between 0 and 5