User zip222 wanted to include a variable in a text box field so that the contents of the textbox could be customized.
Here’s what Jason Sauchuk from Interactive Tools suggested:
Welcome to the Online Student Log-in for the *school_name* This section of our website provides links...
You can then replace *school_name* when you are outputting your content.
(Note: In this example, we're assuming that the values for *school name* is in an editor called "school_record" and the text field is a field called "content" in a table called" your_table".)
For a list page:
<?php echo str_replace('*school_name*',$schoolRecord['title'],$record['content']);?>
For a detail page the code would change to:
<?php echo str_replace('*school_name*',$schoolRecord['title'],$your_tableRecord['content']);?>
Jason explained:
In this code, the sub string *school_name* is replaced in the string $record['content'] with the variable $schoolRecord['title']
Another way to look at this is:
If you place a variable called *var1* in a target text box field called target_field in a table called target_table
And your source for *var1* is in a text box field called source_field in a table called source_table
Then the code in the body of the viewer where you want to display the composite would be:
<?php echo str_replace('*var1*',$source_tableRecord['source_field'],$target_tableRecord['target_field']);?>
Another scenario:
The detail page for a series of event listings contained a PayPal payment link that redirected to a thank you page after successful payment.
In my thank you page I wanted the text field to include the name of the event that was paid for.
I inserted *var1* in my thank you pages' target text field as above and inserted the above code in the body of my thank you page.
Then, I added: NOTE:
?<?php echo $source_tableRecord['num'] ?>%23
to the return URL in my PayPal button code. The %23 (URL-encoding for #) inserts a # after the record number that separates the transaction data (if any) from the record number so it will render correctly, (of course with my own source_table name inserted)
And another scenario: I had a series of variables that I wanted to insert in the thank you page above, and some of them were dependent. IE they shouldn't show unless there was data in a specific field. For example, if there was no special venue for an event, I didn't want to show the special venue name, address, or warning in the thank you page.
With some help from Jason Sauchuk from Interactive Tools, here's what I came up with. In the viewer:
<?php $event_start_date = date("l, M jS, Y", strtotime($learning_centerRecord['event_start_date'])) ; $special_location = ""; if (@$learning_centerRecord['special_event_location_name']) { $special_location = "<b>Special Location: ".$learning_centerRecord['special_event_location_name']."</b>\n"."<br />\n".$learning_centerRecord['special_event_location_address']."<br />\n"; } $placeHolders = array("*var1*", "*var2*", "*var3*", "*var4*", "*var5*", "*var6*" ); $replaceWith = array($learning_centerRecord['event_title'], $learning_centerRecord['event_fee'], $event_start_date, $learning_centerRecord['event_times'], $learning_centerRecord['event_description'], $special_location); echo str_replace($placeHolders, $replaceWith, $common_informationRecord['learning_center_thank_you_1']); ?>
And in the Text Field ('learning_center_thank_you_1)
NOTES: The the ."<br />\n" after Special Location:puts that text on its own line on the finished page. The <br /> after the special_event_location_name is because the address is a multi line entry. The extra ."<br />\n" at the end allows you to remove the blank line after *var6* below, and have it appear only when there's a "special_event_location_name"
Congratulations, you're all signed up for <b>"*var1*"</b> on <b>*var3*, from *var4*</b> *var6* We suggest that you arrive at least 15 minutes early so that you can get to know some of the other attendees. Here's a copy of the description of this event. *var5*
See you there...
Another scenario:
I used this approach to insert a link and an image called "image1" from a single record editor called "graphics" in a text box called "content" in the table "my_table" with the following code.
In the contents of the text box where I wanted the link to appear, I inserted:
<a href="my_URL.com/my_page.php"><img src="*variable*"></a>
And in the page code for my list page (in the foreach loop)
<?php $myImage = $graphicsRecords[0]['image1'][0]; ?> <?php echo str_replace('*variable*',$myImage['thumbUrlPath'],$record['content']);?>
And for my detail page.
<?php $myImage = $graphicsRecords[0]['image1'][0]; ?> <?php echo str_replace('*variable*',$myImage['thumbUrlPath'],$my_tableRecord['content']);?>
Don't forget to include load records calls for both editors at the top of your viewers
I asked Jason how to include more than one variable (text, links or images) in the same text box and he said:
str_replace is a php function (http://ca3.php.net/str_replace) that searches for one string inside another and replaces all instances of that string.
So, if you want to be able to insert more than 1 piece of text into a string, you'll need more placeholders (*var1*, *var2*, *var3*, etc). The actual name of the placeholder is irrelevant, but you should choose something that is unlikely to appear naturally in the text.
You can also use arrays with str_replace. For example:
<?php $placeHolders = array("*var1*", "*var2*", "*var3*"); $replaceWith = array($link1, $image1, $link2); echo str_replace($placeHolders, $replaceWith, $record['content']); ?>
In this example, str_replace will look in $record['content'] and replace all instances of *var1* with $link1, all instances of *var2* with $image1, and all instances of *var3* with $link2.
|