Echo variable inside of textbox

14 posts by 4 authors in: Forums > CMS Builder
Last Post: November 18, 2011   (RSS)

I have a block of text that appears on several pages of my site that has a variable in the middle of it that changes based on the current page, ie..

Welcome to the Online Student Log-in for the INSERTSCHOOLNAMEHERE. This section of our website provides links to all the information online students and families need. Remember to review the important announcements noted below to stay up-to-date on course requirements.

The paragraph itself needs to manageable at a global level, via a textbox field in one section editor. But the variable needs to change on each page.

Right now I just have this:

<p>Welcome to the Online Student Log-in for the <?php echo $schoolRecord['title'] ?>. This section of our website provides links to all the information online students and families need. Remember to review the important announcements noted below to stay up-to-date on course requirements.</p>

But this doesn't allow me to manage the paragraph itself through the CMS.

I was thinking this would be done with a echo that looks for a specific string of characters and replaces it with a variable. But I don't know how to do this.

I tried including the php echo in the textbox but that didn't work - not surprisingly.

Re: [zip222] Echo variable inside of textbox

By Chris - September 16, 2010

Hi zip222,

I think the simplest and safest way to do this would be to use a custom placeholder and have a little PHP on your page to replace the placeholder.

For example, let's say this text is in your Global section, in a field called Welcome:

Welcome to the Online Student Log-in for the INSERTSCHOOLNAMEHERE. This section of our website provides links to all the information online students and families need. Remember to review the important announcements noted below to stay up-to-date on course requirements.

On your viewer pages, you'd load both your $globalRecord and your $schoolRecord, then when you're ready to output the welcome paragraph:

<?php
$welcome = $globalRecord['welcome'];
$welcome = str_replace('INSERTSCHOOLNAMEHERE', $schoolRecord['title'], $welcome);
echo $welcome;
?>


You can, of course, use a different placeholder. You'll probably want to include a note about the placeholder in your Field Description, so that if your users ever accidently delete it, they'll know how to get it back!

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Echo variable inside of textbox

Just curious, what is it about Jason's approach that makes it either complicated or not safe?

Re: [zip222] Echo variable inside of textbox

By Chris - September 16, 2010 - edited: September 16, 2010

Hi zip222,

Our approaches are identical — I just forgot to refresh to make sure he hadn't already posted!

The alternative approach I was thinking of which was unsafe was evalling your content so that <?php ?> tags would execute. The problem there is that any user you allow to modify that field would be able to hack your site.
All the best,
Chris

Re: [chris] Echo variable inside of textbox

Thanks. Both approaches worked for me. And now I know a little more php than I knew before... and knowing is half the battle!

Re: [Jason] Echo variable inside of textbox

Hi Jason and everyone,

I've modified the code slightly to insert an image in a link that is inserted into the text field as follows:

In the text box field:
<a href="target_page.php"><img src="*variable*"></a>
And in the viewer:
<?php $subscribeImage = $common_informationRecords[0]['subscribe_now_button'][0]; ?>
<?php echo str_replace('*variable*',$subscribeImage['thumbUrlPath'],$pricingRecord['features_benefits']);?>


I've added another variable to the viewer with:
<?php $upgradeImage = $common_informationRecords[0]['upgrade_now_button'][0]; ?>

And added <a href="target_page2.php"><img src="*variable2*"></a> to the text box field.

Any thoughts on how I can modify the str_replace viewer code to insert the second link and image, and/or another text variable?

Anything that I've tried made a bit of a mess of things.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Echo variable inside of textbox

By Jason - October 13, 2011

Hi Jerry,

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 (*var*, *var1*, *var2*, 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("*var*", "*var1*", "*var2*");
$replaceWith = array($link1, $image1, $link2);

echo str_replace($placeHolders, $replaceWith, $record['content']);
?>


In this example, str_replace will look int $record['content'] and replace all instances of *var* with $link1, all instances of *var1* with $image1, and all instances of *var2* with $link2.

Hope this helps clarify
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Echo variable inside of textbox

By gkornbluth - October 13, 2011 - edited: October 13, 2011

Thanks Jason!!!


It certainly does clarify my understanding a bit more

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Echo variable inside of textbox

Hi Jason,

I’m trying to incorporate if statements in the array you described above, in order to to test for the existence of information in some “special location” fields.

The code below works but I have a feeling that it could be cleaned up some (probably a lot). I’m just not sure how.

Also, I can’t figure out how to eliminate the blank lines in the viewer that are created by empty variables (when there’s no information in the “special location” fields). The Text Box contents is also below.

Any ideas?

Thanks,

Jerry Kornbluth

VIEWER CODE
<?php
$placeHolders = array("*var1*", "*var2*", "*var3*", "*var4*", "*var5*", "*var6*", "*var7*", "*var8*" );
$event_start_date = date("l, M jS, Y", strtotime($learning_centerRecord['event_start_date'])) ;
if (@$learning_centerRecord['special_event_location_name']) {$venue_name = $learning_centerRecord['special_event_location_name'];}
if (@!$learning_centerRecord['special_event_location_name']) {$venue_name = "";}
if (@$learning_centerRecord['special_event_location_address']) {$venue_address = $learning_centerRecord['special_event_location_address'];}
if (@!$learning_centerRecord['special_event_location_address']) {$venue_address = "";}
if (@$learning_centerRecord['special_event_location_name']) {$venue_warning = "Special Location: " ;}
if (@!$learning_centerRecord['special_event_location_name']) {$venue_warning = "";}
$replaceWith = array($learning_centerRecord['event_title'], $learning_centerRecord['event_fee'], $event_start_date, $learning_centerRecord['event_times'], $learning_centerRecord['event_description'], $venue_name, $venue_address ,$venue_warning);

echo str_replace($placeHolders, $replaceWith, $common_informationRecord['learning_center_thank_you_1']);
?>

TEXT BOX CODE
You're all signed up for <b>"*var1*"</b>
on <b>*var3*, from *var4*</b>
<b>*var8* *var6*</b>
*var7*

We suggest that you arrive at least 15 minutes early so that you can get to know some of the other attendees.

As a reminder, here's a copy of the description of this event.

"*var5*"

We look forward to seeing you there.
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php