Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Through Data, Replacing String

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
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?

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;
 
I knew it had to be something simple and think I finally figured it out but perhaps someone has a better idea. I had repurposed this function from another that I wrote some time ago but on this one I realized that it does not need the brackets on $Replacements[] because the value is not an array in this version while it was before in the other usage:

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 [bold][COLOR=red].=[/color][/bold] 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 [bold][COLOR=red].=[/color][/bold] sprintf("Note: I will be unable to do local work from %s to %s when I will be in %s.",
				$StartDate,
				$EndDate,
				$VisitLocation);
		else :
			$Replacements = "[VisitText]";
			$VisitText = "";
		endif;
	endfor;

	echo str_replace($Replacements, $VisitText, $PageContent);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top