Jump to content


Photo

How to store PHP and HTML code to a variable?


  • Please log in to reply
4 replies to this topic

#1 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 11:32 AM

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 September 2006 - 11:36 AM

Just use:
$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>';


#3 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 12:52 PM

Thanks, that's sorted now. When should ' be used, and when should " be used, jsut for future reference

#4 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 03 September 2006 - 12:54 PM

Aren't they the same thing -- jus that ' doesn't require the \ ?



#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 September 2006 - 04:11 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users