Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Portland, Oregon
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Subimbedded statement for relative path

    I'm using a statement to write repetitive code in an include

    <?php $repeat="table blah blah blah">

    Within this table is a td needing a background image, while the page writing the include contains a statement for the image path. f.ex.:

    <td style=\"background-image: url (images/button.jpg)\"> would need to be ../images/button.jpg if it is in a subdir, so index.php would have something like:

    $imgpath="../";

    While the include would normally contain something like:

    (<?=$imgpath;?>images/button.jpg)

    However:

    <?php $repeat="<td style=\"background-image: url(<?=$imgpath?>images/button.jpg)\";?>

    isn't going to work in this case, so what am I neglecting to do? It's gotta be something really simple.

    Thanks in advance.
    Llamasery Enlightened Internet Strategies
    "Psychics will lead dogs to your body." --Alleged fortune cookie message

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    So let me get this straight. You have an image in an include file and you want to use a relative path for the image no matter what page/directory the include get put into?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Originally posted by Spookster
    So let me get this straight. You have an image in an include file and you want to use a relative path for the image no matter what page/directory the include get put into?
    ..which is, as I am assuming Spookster means, not really a good idea (& certainly not worth the hassle ?)

    just define the image path ina general include or header page, the speed difference is probably not measurable

    define('IMG_PATH','/home/www/path/images/')

    and use (<?=IMG_PATH;?>images/button.jpg)

    or plain ol $_SERVER[DOCUMENT_ROOT].'/images/'; (wont work with script-aliased directories!)


    I have done similar things before and set the relative $ROOT in a variable on several calling pages but it gets messy!
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Portland, Oregon
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by firepages


    ..which is, as I am assuming Spookster means, not really a good idea (& certainly not worth the hassle ?)
    I see what you're saying, though in this case I have little choice since I'm developing the site on one server to install to another, and the only way to make it match is by using relative paths. On top of this, even though I have root access to the second server, this is a facelift for an existing site being built in a clean directory and will be launched all at once by re-pointing IPs. Otherwise I'd just use an absolute path in my css for the background image and define it as a class (which requires far less code).

    I hadn't thought of using a literal server path for the images though (never needed to do that before). That could be the ticket. Thanks for pointing it out.
    Llamasery Enlightened Internet Strategies
    "Psychics will lead dogs to your body." --Alleged fortune cookie message

  • #5
    New Coder
    Join Date
    Jun 2002
    Location
    Portland, Oregon
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This doesn't work, because the solution you suggest repeats my original problem. Here's why:

    I start with a table that acts as a button:
    Code:
    <table border="0" cellpadding="0" cellspacing="2">
     <tr height="26">
      <td style="background:url(path/to/button.jpg)" height="26" width="130">
       <a href="index.php" class="menu">Homepage</a>
      </td>
     </tr>
    </table>
    This table is in an include such as menu.inc, and occupying this include are maybe 15 of these buttons. To eliminate the redundant html, I do something like:
    Code:
    $repeat="<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">
    and the same for <tr> and <td> etc. until I reach my href, then conlude the statement with another string:
    Code:
    $end="</a></td></tr></table>";
    In this manner the code for every button is written as:
    Code:
    <?=$repeat?>index.php">Homepage<?=$end?>
    Isn't this much prettier?

    So the question is, what do I use to replace:
    Code:
    background:url(path/to/button.jpg)
    so it won't conflict with my $repeat string and is still recognized? Sorry if I'm explaining this incorrectly.
    Llamasery Enlightened Internet Strategies
    "Psychics will lead dogs to your body." --Alleged fortune cookie message


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •