Jump to content

How to store PHP and HTML code to a variable?


djmike

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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.