Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Echo variable inside of textbox

 

 


zip222
User

Sep 16, 2010, 8:43 AM

Post #1 of 14 (3332 views)
Shortcut
Echo variable inside of textbox Can't Post

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..


Code
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:


Code
<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.


Jason
Staff / Moderator


Sep 16, 2010, 9:09 AM

Post #2 of 14 (3329 views)
Shortcut
Re: [zip222] Echo variable inside of textbox [In reply to] Can't Post

Hi,

The best way to go about this, I think, would be to put a placeholder in your text that you can then replace with your variable. It's important that it be a string that you won't find anywhere else in the text. So, for example, your content in the text box could look like this:

Code
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 the string is stored in a field called content.)


Code
<?php echo str_replace('*school_name*',$schoolRecord['title'],$record['content']);?>


In this code, the sub string *school_name* is replaced in the string $record['content'] with the variable $schoolRecord['title']

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


Chris
Staff


Sep 16, 2010, 9:24 AM

Post #3 of 14 (3328 views)
Shortcut
Re: [zip222] Echo variable inside of textbox [In reply to] Can't Post

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:


Code
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:


Code
<?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.
Chris


zip222
User

Sep 16, 2010, 9:40 AM

Post #4 of 14 (3324 views)
Shortcut
Re: [chris] Echo variable inside of textbox [In reply to] Can't Post

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


Chris
Staff


Sep 16, 2010, 9:43 AM

Post #5 of 14 (3322 views)
Shortcut
Re: [zip222] Echo variable inside of textbox [In reply to] Can't Post

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.
Chris


(This post was edited by chris on Sep 16, 2010, 9:44 AM)


zip222
User

Sep 16, 2010, 9:50 AM

Post #6 of 14 (3318 views)
Shortcut
Re: [chris] Echo variable inside of textbox [In reply to] Can't Post

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


gkornbluth
Veteran

Oct 12, 2011, 3:19 PM

Post #7 of 14 (2942 views)
Shortcut
Re: [Jason] Echo variable inside of textbox [In reply to] Can't Post

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:

Code
<a href="target_page.php"><img src="*variable*"></a>

And in the viewer:

Code
 <?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:

Code
  
<?php $upgradeImage = $common_informationRecords[0]['upgrade_now_button'][0]; ?>


And added

Code
<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!
http://www.thecmsbcookbook.com


Jason
Staff / Moderator


Oct 13, 2011, 10:33 AM

Post #8 of 14 (2934 views)
Shortcut
Re: [gkornbluth] Echo variable inside of textbox [In reply to] Can't Post

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:


Code
<?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 - Programmer 
interactivetools.com

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


gkornbluth
Veteran

Oct 13, 2011, 12:05 PM

Post #9 of 14 (2933 views)
Shortcut
Re: [Jason] Echo variable inside of textbox [In reply to] Can't Post

Thanks Jason!!!


It certainly does clarify my understanding a bit more

Jerry
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com

(This post was edited by gkornbluth on Oct 13, 2011, 12:07 PM)


gkornbluth
Veteran

Nov 17, 2011, 10:03 AM

Post #10 of 14 (2757 views)
Shortcut
Re: [Jason] Echo variable inside of textbox [In reply to] Can't Post

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

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

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.



Jason
Staff / Moderator


Nov 17, 2011, 10:40 AM

Post #11 of 14 (2756 views)
Shortcut
Re: [gkornbluth] Echo variable inside of textbox [In reply to] Can't Post

Hi Jerry,

I took a look at your code. It seems that var6, 7, and 8 are all dependent on each other. If there is no special location name, there probably won't be an address.

In that case, we can replace 6, 7, and 8 with a single placeholder and combine those 2 fields into a single string.

So the text box would look like this:


Code
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.

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

"*var5*"

We look forward to seeing you there.


and the PHP would look like this:


Code
<?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".$learning_centerRecord['special_event_location_address'];
}

$placeHolders = array("*var1*", "*var2*", "*var3*", "*var4*", "*var5*", "*var6*", "*var7*", "*var8*" );
$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']);
?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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

(This post was edited by Jason on Nov 17, 2011, 11:17 PM)


gkornbluth
Veteran

Nov 17, 2011, 7:30 PM

Post #12 of 14 (2749 views)
Shortcut
Re: [Jason] Echo variable inside of textbox [In reply to] Can't Post

See, that's the difference between us tinkerers and someone who really knows how to write elegant code.

Thank you Jason.

The solution you suggested worked perfectly.

It may not be possible, but do you have any ideas for how to eliminate the blank line that's left in the viewer when there's no information in the $special locations variable?

Again, thanks...

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com

(This post was edited by gkornbluth on Nov 18, 2011, 4:27 AM)


Jason
Staff / Moderator


Nov 17, 2011, 11:21 PM

Post #13 of 14 (2731 views)
Shortcut
Re: [gkornbluth] Echo variable inside of textbox [In reply to] Can't Post

Hi Jerry,

Good catch, I removed the extra ; in the previous post.

One thing you could do is remove the blank space between *var6* and the next line. Then, in the $special string, you can add an extra "\n" to put a blank line after *var6* if a value is there.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


gkornbluth
Veteran

Nov 18, 2011, 4:32 AM

Post #14 of 14 (2724 views)
Shortcut
Re: [Jason] Echo variable inside of textbox [In reply to] Can't Post

Great idea.

And I've learned a bit about arrays as well.

We are lucky to have you around.

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com