In Drupal's page.tpl.php
<?php foreach($node->og_groups as $test) {dpm($test);} ?>
Gives me
alt text http://img.skitch.com/20091229-ekf6xqg5dxq6cgjsgfty74umfx.jpg
But when I do
<?php foreach($node->og_groups as $test) {print($test);} ?>
The value doesn't show up. this is kinda frustating..
Any help is appreciated.
Cheers!
As Steve Michel suggested: Try doing a view source on the rendered page; it may be going to the top of the HTML and may not be visible in the browser output.
Drupal first executes all code, collecting output into a variable. At the very end, this variable is print out. If you print or var_dump something in between, this will be at the very top of the output (since that's done before any of the regular content is printed).
You even figured out the answer: use drupal_set_message (for which dpm is an abbreviation I guess?) to insert text in a nicely formatted way, somewhere in the content part of the page rather than before the tag.
If you need to print out arrays, you can use dpm(print_r($array, 1)) -- the 1 argument makes print_r return the formatted output (and pass it to dpm) rather than printing it out directly.
Try doing a view source on the rendered page; it may be going to the top of the HTML and may not be visible in the browser output.
You can use
<?php foreach($node->og_groups as $test) print_r($test) ?>
You can do this:
$node = $variables['node'];
and then use it like a normal node.
Related
How would one go about showing PHP code on user end. Sort of like w3School does?
Having lets say a grey area div, and then showing the code in there without activating it?
You can use html entities <?php in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
Use <pre> or <code> tags to wrap your code.
Take a look at http://php.net/manual/en/function.highlight-string.php to further see how you can make the code look pretty.
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
Something like:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
Simply you can use following code to display php code on webpage.
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
<?php print('This is php code.'); ?>
The first step is to not wrap that code in PHP tags. So instead of this:
<?
var sample = "code";
?>
You would have this:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre tags to get rid of HTML-ish formatting:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre tags (or any other tags you want to use for displaying code, such as div) as you see fit.
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettify often comes highly recommended.
Typically this is done by showing code within <pre> or <code> tags.
You can use this template........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
It will show the source code and output following.
use the header function of php, this will rea
<?php
header("content-type: text/plain");
?>
The PHP code will just be a string that you can echo or print onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.
Ex:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";
I have a problem with eval() function. Please do not comment something like "Don't use eval" or anything of this kind of thing, as this is not helpful. I have a very good reason to use eval().
Basically I am getting a value from a text field in html on my web page as input code to be executed, like so:
$code = $_POST['code'];
Then, am passing that value to eval function in the html body, like so:
eval($code);
the results are displayed like this:
<h1>test</h1>
the above is displayed string. I want this to execute the html part of it is well. Funny thing is if I try this in a different file like this:
<?php
$code = "echo '<h1><b>TEST</b></h1>';";
eval($code);
?>
I get the desired result, which is a proper processed html element h1 with "TEST" in it.
Any ideas?
Thanks in advance
$_POST['code'] apparently contains HTML entity codes, e.g.
"echo '<h1>test</h1>';"
You need to decode it before calling eval.
eval(html_entity_decode($_POST['code']));
I have a php script that does a query in my database and returns a string ( like "2" ). I print it using
print strip_tags('2');
but in the output of my browser I get :
<body><html>2</html></body>
Is there any way to prevent the tags from beiing printed? Is it maybe that the browser auto adds them?
For all those answering about strip_tags (" 2 ");
THIS IS WRONG:
I want a siple version.php
with
echo '2';
and nothing else. It prints the tags too. I don't have the tags and then try to print.
More explanation to those who try to get easy rep
my code is:
$str = '2';
print strip_tags($str);
and it prints
<html><head></head><body>2</body></html>
It is not possible. The browser creates these elements automatically, without it there would not be any text flow(means nothing of this could be made visible). You can just use this variable for any script, it won't include the HTML tags. This is only made by the browser to make it visible for you.
You can use
header("Content-Type: text/plain");
at the beginning of your script, in order to tell the browsers you're only gonna send plain text, not html. This will prevent your browser from automatically adding those html tags.
Then, check what you print (or echo). Here, the body tag should be in html tag.
I'm quite new here. I'm trying to make a blog/journal site that allows users to post their own journal. I'm still quite reluctant on making it because I am really afraid of malicious code injections.
So here's a sample code:
<?php
$test = "<b>blah</b>"; //User input from SQL
echo "$test";
?>
What will come out is just the word "blah" in bold right? What I was trying to achieve was to echo "<b>blah</b>" instead. I don't want people to put some PHP codes that can actually mess up my whole web page. Please keep in mind that the variable $test is actually a MYSQL query, so that variable will be needed as an example. I know you can do echo '$test'; but it just comes out as "$test" instead. I feel like pulling my hair out I can't figure it out yet.
The second solution I know of is the htmlspecialchars(); function, but I want the strings to display as what I typed, not the converted ones...
Is there any way I can do that?
I think the OP wants the HTML itself to be output to the page, and not have the tags stripped. To achieve this, you can run the string first through htmlentities()
$test = '<b>blah</b>';
echo htmlentities($test);
This will output:
<b>blah</b>
Which will render in the page as
<b>blah</b>
Echo don't execute PHP code from string. This is impossible and this is not security hole in your code.
You can use a template engine like Twig for exemple.
If htmlspecialchars(); is not the one you are looking for, try the header() option.
header('Content-type: text/plain');
When you are gonna give <b>Hi</b> to a browser, it will be displayed in Bold and not the text be returned. But you can try this way, outputting it inside a <textarea></textarea>.
Or the other way is to use htmlentities():
<?php
$test = "<b>blah</b>"; //User input from SQL
echo htmlentities("$test");
?>
How can I print the array in a Tree-like format--making it easier to read?
Try:
<pre><?php print_r($var); ?></pre>
It will give the proper tree structure that HTML's whitespace policy trims out.
Are you wrapping the output in <pre> tags? That should get you pretty decent output, because it will show the spaces. Another option would be to install the xdebug extension, which then can replace var_dump so that it generates more-readable HTML output.
function pr($var)
{
print '<pre>';
print_r(htmlspecialchars($var));
print '</pre>';
}
pr($myArray);
I found it's a good idea to print_r as follows
printf("<pre>%s</pre>", print_r($array, true));
It may not be ideal, but it's easier to read.
Try taking a look at Zend_Debug, a relatively plug-and-play module from the Zend Framework which does an excellent job at effectively dumping complex variables.
Usage:
$my_var = new StdObject(); // or whatever
Zend_Debug::dump($my_var);
die; // optional, prevents routing, forwarding away, etc.
You could print it into the error log:
error_log(print_r($myarray,1));
Note that you will see \n instead of carriage returns because it has to be collapsed in a single line.
Mabe the output looks like junk in the webpage. Try looking at the source of the page and it will be in tree-like format I suppose.
As many people previously mention, make sure to wrap it around a <pre> tag.
I would take an extra precautions to make sure nothing is wrapping that <pre> as well, such as <p> or <div> with a CSS class that can override the Pre's Style
May I suggest using var_export($array)?
It formats values with parsable php syntax
And even when you forget to output <pre> and </pre> tags,
while not very easy on the eye,
its output still makes more sense then print_r informal bunch of data.