Fiji Web Design
Welcome, Guest
Please Login or Register.    Lost Password?
help: PHP module - mod_php using str_replace() (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: help: PHP module - mod_php using str_replace()
#344
akane (User)
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
help: PHP module - mod_php using str_replace() 3 Years, 2 Months ago Karma: 0  
hi everyone,

I need help using mod_php in my joomla 1.5 site. I want to use this module for finding and replacing some tag on the head section of joomla by using str_replace() and they vary on different pages. The problem is i have little knowledge on PHP but basically this is what i want to achieve, first I want to find the "<head>" tag and insert this "<script src="somejavascript_1.js" type="text/javascript" />" tag below the "<head>" tag.

I'm using a module for the reason i want to insert javascript to specific pages only and I have 3 different javascript that i would insert in different pages of my site.

e.x.
"HOMEPAGE" uses javascrip_1.js
"PRODUCT PAGE" uses javascrip_2.js
"ABPOUT US PAGE" uses javascrip_3.js

If you have some Idea that wouldn't use this technique but would achieve same result. I Open to it!

Thanks guys!
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#347
admin (Admin)
Admin
Posts: 475
graph
User Online Now Click here to see the profile of this user
Re:help: PHP module - mod_php using str_replace() 3 Years, 2 Months ago Karma: 10  
Hi,

All the output from Joomla1.5 is available through:

JResponse::getBody();

And can be changed and modified, then reset using:

JResponse:etBody();

For example:

$body = JResponse::getBody();
$body = str_replace('your string', 'your replacement', $body);
JResponse::setBody($body);
This is better suited for a plugin however, since the module will still display an empty module position.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1135
jacomstephens (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Re:help: PHP module - mod_php using str_replace() 2 Years, 1 Month ago Karma: 0  
OK, I'm also a PHP newbie. It makes sense that you would have to actually apply the results after str_replace changed the variable. Would you help me with the syntax of my particular situation? What I'm trying to do is to be able to type something like {username} or <<username>> in the content of a page and have it replace that with the actual username of whichever user is currently logged in. For example, I'd type "Welcome, {username}." and it would display "Welcome, Bob Wenzel." when Bob is logged in. I'm using Joomla 1.5 and here is my code so far:

$user =& JFactory::getUser();
$row->text = str_replace('{username}', $user->username, $row->text);

I assume that looking in $row->text (the content of the article?) is correct, but please feel free to correct me if that is the wrong place. So what would I do to apply the change? Something like:

JResponse:etBody($row->text);

or do I need to setContent or something else? Thanks in advance for your help.

Jacom
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1136
jacomstephens (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Re:help: PHP module - mod_php using str_replace() 2 Years, 1 Month ago Karma: 0  
Or am I making it too complicated? Would I do it just the way you explained it using the getBody, str_replace, setBody?

Nope, tried that and nothing happened. hmmm.
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/26 10:05 By jacomstephens.
  The administrator has disabled public write access.
#1137
admin (Admin)
Admin
Posts: 475
graph
User Online Now Click here to see the profile of this user
Re:help: PHP module - mod_php using str_replace() 2 Years, 1 Month ago Karma: 10  
You will want to use the content plugin: http://www.fijiwebdesign.com/products/joomla-php-plugin.html

Paste your code into the onprepare field. For Joomla1.5 use $article instead of $row. The variable $article (and $row in J1.0) only exist in the content plugins. They do not exist in a module scope.

The problem you had with accessing getBody() is that your module was loaded before the article loaded. It would work if you set the module in a position after the article. It is best however to use the plugin I linked.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#1138
jacomstephens (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Re:help: PHP module - mod_php using str_replace() 2 Years, 1 Month ago Karma: 0  
Thank you so much for your help. I'm using the botphp_content plugin you suggested and I have this code in the OnPrepare:

$user =& JFactory::getUser();
$article->text = str_replace('<<username>>', $user->username, $article->text);
$article->text = str_replace('<<user>>', $user->name, $article->text);
JResponse:etBody($article->text);

And I have the plugin order set to last, but it's still not working. What am I doing wrong? Do I need to do a getBody before the str_replace or something?
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Joomla Downloads

DownloadsDownload our free Joomla! Components, Modules and Plugins.