I'm trying to remove redundancy in one of my files. I have 30 to 40 queries---of 2 different types---that run based on the date. I tried to avoid repeating the same (of 2 different) query strings by putting them in a function and calling the function based on the date. It doesn't seem to work as I expected/hoped. I'm new to PHP and maybe I've overlooked something or don't understand how PHP functions work.
Here' a sample of the code:
function queryONE() {
$stmt = $pdo->query("SELECT `rand` FROM `setGallery` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit");
$fh = fopen("galleryRand_PDO.txt", "w");
while ($row = $stmt->fetchObject()) {
fwrite($fh, $row->rand);
}
fclose($fh);
exit();
}
function queryTWO() {
$stmt = $pdo->query("(SELECT `rand` FROM `jukebox2014` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit) ORDER BY `rand` DESC";
$fh = fopen("galleryRand_PDO.txt", "w");
while ($row = $stmt->fetchObject()) {
fwrite($fh, $row->rand);
}
fclose($fh);
exit();
}
if ($date == Jun 13)
{
$category = "galleryDir";
$dayList = "Grids";
queryONE();
}
if ($date == Nov 16) {
$category = "class";
$dayList = "Grids";
queryTWO();
}
Note: If I put an --- echo "Hello world!" --- statement in the function it does display; so the if() statement is firing the function, but the function isn't running the query, but the fopen code DOES create the expected text file.
Obviously there are a lot more if() statements or I wouldn't bother. Any ideas why this won't work?
Thanks.