PCHomepage
Programmer
I have a function I wrote some time ago that replaces a bit of text in a paragraph and it's working well but I am trying to rework it to be able to make multiple replacements and cannot seem to get it to work. The replaced text is something like [VisitDate1], [VisitDate2] etc. and here is the original function with the $Location variable being an integer that references the ID in the database table:
Here is the new version which is no longer getting the $Location variable and instead it is being built dynamically from the entries. It is building an array of all the possible [VisitDateN] values along with an array of the text to replace it. It is not producing any errors but neither is the text being replaced so clearly I've missed something basic here. Can anyone advise?
PHP:
function visitDates($Location, $PageContent) {
// Get Oregon visit dates
$sqlDates = "SELECT StartDate, EndDate FROM locations WHERE ID = $Location";
$rowDates = DBConnect($sqlDates, "Select", "geoip");
$StartDate = date("F j", $rowDates['StartDate']);
$EndDate = date("F j, Y", $rowDates['EndDate']);
$ReplaceText = "[VisitDate$Location]";
if ($rowDates['StartDate'] > time()) :
$ReplaceDate = "<em>The next visit date is from $StartDate to $EndDate</em>";
else:
$ReplaceDate = "No future dates have been scheduled yet but be sure to check back soon!";
endif;
echo str_replace($ReplaceText, $ReplaceDate, $PageContent);
}
Here is the new version which is no longer getting the $Location variable and instead it is being built dynamically from the entries. It is building an array of all the possible [VisitDateN] values along with an array of the text to replace it. It is not producing any errors but neither is the text being replaced so clearly I've missed something basic here. Can anyone advise?
PHP:
function visitDates($PageContent) {
global $VisitDates;
global $Replacements;
$sqlDates = "SELECT ID, Location, StartDate, EndDate FROM locations";
$rowDates = DBConnect($sqlDates, "Multiple", "geoip");
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']);
if ($rowDates[$i]['StartDate'] > time()) :
$VisitText = sprintf('"<em>The next visit date in %s is from %s to %s</em>"',
$VisitLocation,
$StartDate,
$EndDate);
else:
$VisitText = sprintf('"No future dates to %s have been scheduled yet but be sure to check back soon!"',
$VisitLocation);
endif;
$Replacements .= "\"[VisitDate$ID]\",";
$VisitDates .= "$VisitText,";
endfor;
$VisitDates = rtrim($VisitDates,",");
$VisitDates = array($VisitDates);
$Replacements = rtrim($Replacements,",");
$Replacements = array($Replacements);
echo str_replace($Replacements, $VisitDates, $PageContent);
}