Drupal 6 Rules Execute custom php output not in html body - php

I'm using drupal-6.22, rules-6.x-1.4, and some modules.
With the help of rules module, I've set a triggered rule, when the condition happens, it execute custom php code.
The custom PHP print some html like this:
<?php echo '<div><span>whatever</span></div>'; ?>
BUT, the html just appears before the !DOCTYPE html tag, is there any method to place the code wherever I want?
(e.g. <div id="header">HERE!</div>)
I think in this case JS is in need, so I use drupal_add_js($output,'inline'), and called an outside function to place the html, still nothing happens.
I think there must be some method to let PHP insert the output into the current DOM tree, is there any known function or variables for this?
Please help, thanks!!

drupal_set_message() seems to be the right thing for you.

Related

custom php function to call in header

I have a Wordpress website. I am asking for help as I am not good with codes, but know a bit about the flow of functions and all.
I want to add a new PHP function that will be defined in a separate file. The function to be defined is required to serve a 728x90 banner ad on top of the header area of the site (so I guess the php function will also have CSS styling to align the ad in the center). I don't know what I am saying will be possible the way I am saying.
Alternatively, there might be a way to call a js function defined in separate file, then called in the php header to serve the banner (again center-aligned).
I am seeking help here as Stackoverflow also helped me solve problems previously.
Is there anyone who can help me get this thing done?
Once you have your PHP handler written in a separate file call it using require_once like this:
<?php
require_once('myfile.php');
// Wordpress Stuff
?>
I guess PHP have no relation with CSS/styling, you can do it with Javascript.
like this or using JQuery

Execute PHP code when making a post in a cms

Sorry for the vague title but it's hard to describe what I mean in a few words.
I made my own cms and use it for all my personal projects. On some pages I want to include a php script in the content area. I load the content simply by echoing the variable that holds the content.
The template file looks like this:
<div id="content">
echo $content;
</div>
In my CRUD I make a post containing a php snippet.
<?php echo "My name is ".$var.""; ?>;
Then I save it and load the page and this is what happens:
<div id="content">
echo <?php echo "My name is ".$var.""; ?>;
</div>
But what I want is that the php code get's executed instead of getting echoed.
Something like the Wordpress plugin Exec-PHP. Can anybody explain to me how to achieve this?
Thanks in advance!
You could use the PHP eval() function to execute PHP code. Be aware though, if you ever allow users to insert text that may at some point be run through eval(), you could end up with some serious problems.
The php website says:
The eval() language construct is very dangerous because it allows
execution of arbitrary PHP code. Its use thus is discouraged. If you
have carefully verified that there is no other option than to use this
construct, pay special attention not to pass any user provided data
into it without properly validating it beforehand.
You can try php eval() func. But it is considered evil.
Rather than use PHP instructions, why don't you output to HTML, and use Javascript to execute what is displayed? There is no danger to your server that way. Others have already warned about the dangers of eval() in PHP.

PHP + Smarty: Parse PHP+HTML into a String?

I am using PHP in combination with Smarty Templates to generate pages serverside. Currently, I am loading a page as follows:
$smarty->assign('app', file_get_contents("some_content.php"));
Where some content contains HTML with PHP tags and code inside those tags.
I would like the PHP content inside this file within the current scope (That of the script reading the file), so that a particular function I've defined is available. How would I go about doing so? All the information I can find is regarding the eval(...) function, which doesn't seem to be able to cope with the HTML/PHP mixture: would I need to perform a find/eval/replace operation to achieve the desired result, or is there a more elegant way of doing this?
From my opinion, this short snippet of the code you posted shows that something is generally wrong there :)
But nevertheless you can achieve whatever you are trying to achieve by doing the following:
ob_start();
include("some_content.php");
$result = ob_get_clean();
$smarty->assign('app', $result);
Ich, I'm such a dummkopf. There is an answer right on the PHP manual for eval, right under my nose. Here is the answer I neglected to notice.
You can use {literal}...{/literal} smarty tags to display any content in smarty templates as is. It used to transfer java scripts and other specific content.

PHP placement problem

I am calling this function halfway down the page:
<div id="leftArea">
<?php
if (isset($id)){
single($id);
}
?>
</div>
The problem is i want use some of the output in the meta tags in the <head>, whats the best way to approach this?
EDIT:
The single function, takes the $id and echo's the data straight to the page, exactly where it is. In the DIV leftArea. But i want to take one of the rows from the DB and insert it into the META tags at the top
Copy the code into the <head> section.
Redesign your System
The best method is to create a class that manages your html page for you, example:
$Page = new HTMLPage("My Page",HTMLPage::Strict);
$Page->addScript("....");
$Page->addScript("....");
$Page->addScript("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->SetBody($MyTemplate);
$Page->send();
this way though out your functions you can do
function myfunc()
{
global $Page;
$Page->addScript("....");
}
the main point here is you should build your document up before sending it to the browser, this way you still have control over the content no matter where your code is executing from.
on the final send method you build your content up, and then push the content via echo, and then exit directly. (all processing should be done prior to output to manage errors)

How can I include PHP in a vBulletin style?

I'd like to use PHP in a vBulletin style/skin, yet when I do so, the PHP doesn't work.
By 'not work', I mean it's treated just as plain text. And if you look in the code you can see the PHP code (hidden, by Firefox - which is the behaviour you get if you put PHP code on a plain html page)
http://vapurl.com/h0kp3fqr8c - view source, and you'll see what I mean.
You can't stick random PHP into the vBulletin templating system; it gives you some simple branching options in the form of
<if condition="$somevariable === $someothervariable">
some text or HTML to be displayed
</else>
some other text or HTML to be displayed
</if>
A good explanation of how the template conditional system works can be found in the vBulletin manual here.
Your short url appears to have expired, but if the code you wish to include is more complicated than that, you need to start looking into vBulletin hooks and plugins.
Assuming you're entering the correct PHP syntax, this may be occurring if you're trying to use any of the standard output methods.
If you are trying to use echo, print or any other similar output function, try wrapping all of your included PHP within ob functions.
Example:
ob_start();
//PHP code I want to run goes here
echo "Test this works?";
ob_end_clean();
hooks /plugins are the best way of manipulating data before displaying it in the template. You can add as much php as you want in a plugin and then modify the template to output the results.

Categories