• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

Chunk of HTML to PHP

$head_extras = '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<script type="text/javascript">$(function(){var galleries=$('.ad-gallery').adGallery();galleries[0].settings.effect='fade';});</script>';

How to pull out this chunk of HTML via PHP?
0
SSupreme
Asked:
SSupreme
  • 6
  • 5
  • 4
  • +1
1 Solution
 
HellmarkCommented:
use the print command, such as
<?PHP
$head_extras = '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<script type="text/javascript">$(function(){var galleries=$('.ad-gallery').adGallery();galleries[0].settings.effect='fade';});</script>';
print($head_extras);
?>
0
 
HellmarkCommented:
Also, I do have to note, that if you have HTML code outside of the <?PHP and ?>, it'll be passed directly on, assuming it is a file you'll be accessing from the browser, and not just using PHP without a webserver.

Also, you can use the echo function.

<?PHP
$head_extras = '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<script type="text/javascript">$(function(){var galleries=$('.ad-gallery').adGallery();galleries[0].settings.effect='fade';});</script>';
echo $head_extras;
?>
0
 
SSupremeAuthor Commented:
the problem with size and statements like this ...... $('.ad-gallery'). ....
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
When you say, "pull out" what do you mean, exactly?
0
 
SSupremeAuthor Commented:
Take this chunk of markup from PHP and make it appear in Html via PHP.
Sorry for miss understanding.
0
 
HellmarkCommented:
As far as size goes, sometimes too long of a string causes issues, plus it is easier to break things up using multiple lines

<?PHP
echo '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>';
echo '<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>';
echo '<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>';
echo '<script type="text/javascript">';
echo '$(function(){var galleries=$(\'.ad-gallery\').adGallery();galleries[0].settings.effect=\'fade\';});</script>';
?>
0
 
Dave BaldwinFixer of ProblemsCommented:
In this demo file from another question, anything that doesn't need to be parsed by PHP is put in between the PHP tags.  That's what I do.  If you are not using $head_extras for anything but an echo, I'd just put that code where it belongs on the page and not echo it from PHP.
<?php 
// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Get Name</title>
</head>
<body>
<?php 
$nname = "mysite.com/WD/ph/th/lm.html";
echo $nname."<br>";
$result = NULL;  // or ""
$pieces = explode("/", $nname);
foreach($pieces as $vstring) {
        if(strlen($vstring) == 2) {
           $result = $vstring;
           break;
           }
        }
echo $result;
?>

</body>
</html>

Open in new window

0
 
Ray PaseurCommented:
OK, I think you want echo.  The right notation to use for things like this is called HEREDOC (or maybe NOWDOC).
http://us3.php.net/manual/en/language.types.string.php

Run this and view source to see how it works.  No worries about single/double quotes, etc.
http://www.laprbass.com/RAY_temp_ssupreme.php


<?php // RAY_temp_ssupreme.php
error_reporting(E_ALL);

$head_extras = <<<HEREDOC
<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<script type="text/javascript">$(function(){var galleries=$('.ad-gallery').adGallery();galleries[0].settings.effect='fade';});</script>
HEREDOC;

echo $head_extras;

Open in new window

0
 
HellmarkCommented:
Personally, if you're just wanting to output static HTML, its best to not have it in the area marked as being PHP code. PHP would ignore it then, and just pass it along to the browser.

For example, the following two have identical output to the browser, despite being different code on the server

<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<?PHP
echo '<script type="text/javascript">';
echo '$(function(){var galleries=$(\'.ad-gallery\').adGallery();galleries[0].settings.effect=\'fade\';});</script>';
?>

Open in new window


<?PHP
echo '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>';
echo '<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>';
echo '<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>';
echo '<script type="text/javascript">';
echo '$(function(){var galleries=$(\'.ad-gallery\').adGallery();galleries[0].settings.effect=\'fade\';});</script>';
?>

Open in new window

0
 
Ray PaseurCommented:
Over time you may find, as many developers do, that the ability to intermix HTML and PHP code becomes a terrible liability.  It usually shows up as a problem about the time you need to make a change that affects more than one page.  For that reason a lot of developers prefer to use a Model-View-Controller organization.  Just a thought... ~Ray
0
 
SSupremeAuthor Commented:
Where can I read about "Model-View-Controller "?

Suddenly, I start to think  that my question isn't logical.
I gave a variable, and asked in my opinion: Why doesn't it work? How make it work?
And Hellmark gives me wrong answer, which is useless.
If you test it, it's most likely it would not work, even with echo:
<?PHP
$head_extras = '<link rel="stylesheet" type="text/css" href="../gal/jquery.ad-gallery.css"/>
<script type="text/javascript" src="http://ajax.googaleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../gal/jquery.ad-gallery.js"></script>
<script type="text/javascript">$(function(){var galleries=$('.ad-gallery').adGallery();galleries[0].settings.effect='fade';});</script>';
print($head_extras);
?>

Open in new window

plus it is easier to break things up using multiple lines
Believe me it isn't. Try to echo whole site line by line.
DaveBaldwin, I tried to stick your code between, but it doesn't help.
Ray_Paseur, your solution is best and I would use it in future to echo whole site at once. Thanks for checking it before posting.
Hellmark, why did you think of \'? When I saw it I got an idea to replace all single quotes inside with double quotes, and it works now.
0
 
SSupremeAuthor Commented:
It works in best way. Thanks for participation.
0
 
Ray PaseurCommented:
Thanks for the points.  Using the backslash to escape quote characters was the only tool we had for a long time.  HEREDOC and NOWDOC syntax are relatively recent additions to the language.  IMHO, they are really excellent tools!  All the best, ~Ray
0
 
HellmarkCommented:
Also, other related languages still need to rely on backslash, plus I am a linux user, so use back slash with bash on a daily basis.

As far as the first example, I forgot to escape the single quote, and that's where the problem is coming in. The other examples are problem free though.
0
 
Ray PaseurCommented:
@Hellmark: I understand your solution and it makes sense to me, even if there is a small syntax error.  Syntax errors are a dime-a-dozen and they are easy to fix.

At the same time, I see many untested "solutions" posted here at EE, and I believe that we should all work together to raise the quality of our answers.  So for my own part, I test every solution before I post it (or in the case of suggestions that cannot be tested because we have no test data, I make note of that).  And I challenge my colleagues here at EE to do the same.  Let's make enough tests to prove the validity of the code that we post, and eventually our word will become law in the universe.

Not a sermon, just a thought. ~Ray
0
 
HellmarkCommented:
I did test the solutions I posted, I just had accidentally pasted the wrong bit. I had copied and pasted the original poster's code into the comment box, got the code to how I thought it should be, copied the code into a file, brought it up in a browser, saw the issue, fixed it, but forgot to recopy the code before pasting it back into the comment box. In the other examples of code I did afterwards, I made sure to do the work in a text editor and test before I even started writing the comments, so I wouldn't forget again.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now