PCHomepage
Programmer
I am trying to create a function that has the input of a block of text with a code [VisitDate] it in to be replaced by certain text but can't get it to work. The query itself is pulling up the proper results so currently there is one where Message is 2 (the rest are 0) but there may be times with more than one match and I want to replace [VisitDate] with all of them. I understand the general issue that it is matching only the first result but I'm unsure how to fix it. Any ideas?
I use a few custom functions to connect to the database and fetch data for various things and DBConnect() used here has:
PHP:
function visitDate($PageContent) {
global $VisitDates;
global $Replacements;
global $VisitText;
$sqlDates = "SELECT ID, Location, StartDate, EndDate,
(
CASE
WHEN NOW() BETWEEN FROM_UNIXTIME(StartDate) AND FROM_UNIXTIME(EndDate) THEN 1
WHEN NOW() BETWEEN DATE_SUB(FROM_UNIXTIME(StartDate), INTERVAL 1 WEEK) AND FROM_UNIXTIME(StartDate) THEN 2
WHEN FROM_UNIXTIME(EndDate) > NOW() THEN 3
ELSE 0
END) AS Message
FROM locations";
$rowDates = DBConnect($sqlDates, "Multiple", "db_name");
for ($i=0;$i<=count($rowDates)-1;$i++) :
$ID = $rowDates[$i]['ID'];
$VisitLocation = $rowDates[$i]['Location'];
$StartDate = date("F j", $rowDates[$i]['StartDate']);
$EndDate = date("F j, Y", $rowDates[$i]['EndDate']);
$Message = $rowDates[$i]['Message'];
if ($Message == 1) :
$Replacements[] = "[VisitText]";
$VisitText = sprintf("Note: I am currently in %s from %s to %s and unable to do local work until I return.",
$VisitLocation,
$StartDate,
$EndDate);
elseif ($Message == 2) :
$Replacements[] = "[VisitText]";
$VisitText = sprintf("Note: I will be unavailable for local work from %s to %s when I will be in %s.",
$StartDate,
$EndDate,
$VisitLocation);
else :
$Replacements[] = "[VisitText]";
$VisitText = "";
endif;
$VisitDates[] = $VisitText;
endfor;
echo str_replace($Replacements, $VisitDates, $PageContent);
}
I use a few custom functions to connect to the database and fetch data for various things and DBConnect() used here has:
PHP:
case "Multiple":
if ($result = $mysqli->query($Query)) :
$numrowsCat = $result->num_rows;;
if ($numrowsCat >= 1) :
$result = $mysqli->query($Query);
while($row = $result->fetch_array()) :
$results_array[] = $row;
endwhile;
return $results_array;
endif;
$mysqli->close();
endif;
break;