Jump to content

Archived

This topic is now archived and is closed to further replies.

djmike

How to store PHP and HTML code to a variable?

Recommended Posts

I'm writing a script that will randomly insert news into a sidebox on my website. However, the site allows users to choose their 'skin' and this is passed to other pages via the url. So a link to a page called "hr0.php" is this:

    php:<a href="hr0.php?p=<? echo $p; ?>">Introduction</a>



I have the code to randomly pick a number, then the HTML code to display that story and any assossciated images is loaded into a variable. This is all done in a separate file from the page where it will be displayed (as it will be used to fill the sideboxes of many pages.) In the sidebox I also want to include a link, so if the user is interested they can click it and be taken straight to the page. However, I am having trouble because it doesn't work having php code, within html code, being loaded into a php variable and then echoed. This is the code within my random picking file that loads the variable that I want to echo on my target page:

    php:
    $body = "<p align=\"center\"><img src=\"images/l2-019.jpg\" width=\"155\" height=\"156\" /></p>
          <p align=\"center\">      <a href=\"hr0.php?p=<? echo $p; ?>\">Introduction</a>"



This works and does show the image and word "introduction" but upon clicking it the colour reverts to the default.

Any ideas please?
Thanks

Share this post


Link to post
Share on other sites
Just use:
[code=php:0]$body = '<p align="center"><img src="images/l2-019.jpg" width="155" height="156" /></p>
          <p align="center">      <a href="hr0.php?p=' . $p . '">Introduction</a>';[/code]

Share this post


Link to post
Share on other sites
Thanks, that's sorted now. When should ' be used, and when should " be used, jsut for future reference

Share this post


Link to post
Share on other sites
They are the samething yes. However if you ahve variables or whitespace characters (\n, \t, \r, etc) in single quotes PHP will just treat them as normal text. However if you used double quotes PHP wont treat them as normal text and will parse the variables and treat \n, \t, \n etc as whitespace characters.

Also I use single quotes on html blocks to save having to type the backslash . This also helps keep the code a readable as possible.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.