PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
PHP and html together problem (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: PHP and html together problem


#1447
fazmag (User)
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
PHP and html together problem 3 Years, 4 Months ago Karma: 0  
Hello,
first, let me thank the author for the nice plugin, it really saved my day!

Second, I have a couple of questions:

Take a look at this php code:

Code:
<?php
  $value=1; 
  if ($value==1)
  {
     // perform some php code
?>
    <!-- this is html, output only if $value=1 -->
    <strong>the value is 1</strong>
<?php
      // again some php code, runs only if $value=1
  }
  else
  {
	// perform other code, runs if $value!=1
?>
	<!-- this is html again, output only if $value!=1 -->
	<strong>the value is not 1</strong>
<?php
   }
?>   
works fine when it runs directly (outside from Joomla I mean), but it does not when run into Joomla content via directphp. In this case the output is "the value is 1 the value is not 1" After some tests I found that directphp does not work if php code is output together with HTML AND you enclose it inside {} brackets to run only when some conditions are met. To have it run correctly, I must use
Code:
<?php
  $value=1;
  if ($value==1)
  {
     // perform some php code
     printf ("%s","<strong>the value is 1</strong>");
     // again some php code, runs only if $value=1
  }
  else
  {
    // perform other code, runs if $value!=1
    printf ("%s","<strong>the value is not 1</strong>");
  }
?>
This works perfectly. This would be more than acceptable, but the second problem arises then. If I have to output everything via PHP output functions, it seems directphp is unable to output a line feed+carriage return: this php code works if used directly:
Code:
<?php
	printf("%s", "this is first line<br><br>this is second line");
?>
It outputs, as expected, 2 text lines separated by 2 empty lines. But it does not inside Joomla content via directphp; it outputs the following: "this is first line this is second line". I tried different methods to provide line feed+cr:
Code:
echo JText::_('<br>'); or, printf("<br>"
and whatever I could think of, but none of these methods work. Of course, being unable to output carriage returns is a real problem when dealing with complex data output. Any suggestions are welcome! Ciao
 
 
Last Edit: 2009/01/12 00:19 By fazmag. Reason: text changes
  The administrator has disabled public write access.

#1450
kksou (Admin)
Admin
Posts: 1599
graph
User Online Now Click here to see the profile of this user
Re:PHP and html together problem 3 Years, 4 Months ago Karma: 23  
Hi,

1) As noted in another post similar to yours, DirectPHP does not process a mix of PHP and HTML statement, such as the one you've given above.

DirectPHP uses simple direct eval(). Try running your code through a eval(). It won't work. That's why DirectPHP doesn't work. (Note: yes, running the entire .php file works. But reading the entire .php file content and run the whole thing through eval(). It won't work. eval() only processes plain PHP statement.)

2) Try saving an article and take a look at how it's stored in the mysql table.

And try think about how DirectPHP is able to process your PHP statements in your article.

As noted in (1), eval() only processes plain PHP statement. However, the Joomla editor adds a lot of <br> and <p> in the PHP code. In DirectPHP, I have to strip away all these HTML tags before pumping it through eval().

This explains why all your <br> and <p> are gone.

There are two ways if you want to output carriage returns.

a) Use "no editor", or "source code" option. I always use this, because I can then add any HTML tags freely in my PHP codes.

b) Use "&lt;br /&gt;" or "&lt;p&gt;". That is, you need to replace all "<" with "&lt;" and ">" with "&gt;" I know it's troublesome. But it works.

Hope this helps.

Regards,
/kksou
 
  The administrator has disabled public write access.

#1462
fazmag (User)
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
Re:PHP and html together problem 3 Years, 4 Months ago Karma: 0  
Thanks kksou for the clear answers, sorry if I asked something which was already answered, I searched the forum but did not find anything related to my problem.

As for 1): it's clear now, I took a look at directPHP code and guessed the reason was that.

For issue 2): I always use plain editor, never used Tiny MCE or other GUI editors. In MySql DB the content is just the code I wrote: there are no additions from GUI editors.

So the only < br > are those I add to code. If I try to output carriage return printing < br > via printf, it is stripped away anyway: this happens, I guess, since the function fix_str2 in directPHP.php replaces all occurrences of < br > with \n. So, if I'm correct, < br > are stripped from php also when you use plain text editor.

As a matter of fact, I found that it is enough to comment lines 69-70 in directPHP.php: this avoids that fix_str2 functions strips <br> and <br />. It is feasible if you always use a plain text editor to write yout content, what I do.
Anyway, since I do not want to modify your code, I'll use your suggestion which works fine.

Many thanks for your time, ciao
 
  The administrator has disabled public write access.



Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved