WordPress.org

Ready to get started?Download WordPress

Forums

Putting a variable in a URL (8 posts)

  1. Mark (podz)
    Support Maven
    Posted 8 years ago #

    Scenario:
    A file is needing to be referenced in several places.
    The name of this file changes often.
    The folder where this file is remains the same, so I just want to alter the filename.

    So the link would normally be:
    <a href="http://very/long/path/filename.txt">

    This makes for messy editing and increased chance of mistakes. So.... I want to include a file with the name in and then include that in the url as a constant:

    <?php include('constants.php'); ?>
    and that has
    $file1=rabbits.txt
    $file2=carrots.txt

    Then in the main code body
    <a href="http://very/long/path/<?php $file1 ?>">

    Unsuprisingly (because I'm writing it) this does not work and when I view source I see
    http://very/long/path/

    How do I do this ?

    Thanks :)

  2. x-f
    Member
    Posted 8 years ago #

    Just to make sure.
    <a href="http://very/long/path/<?=$file1?>">
    You print out that $file1 somehow, right?

  3. Mark (podz)
    Support Maven
    Posted 8 years ago #

    That's what I want to do, yes.
    I need the $variable to become part of the url so it gets the right file.

  4. McShelby
    Member
    Posted 8 years ago #

    Try:

    <a href="http://very/long/path/<?php echo $file1; ?>">

    You just forgot the echo.

  5. Mark (podz)
    Support Maven
    Posted 8 years ago #


    <?php
    $cast1=file.txt;
    ?>

    <a href="http://very/long/url/<?php echo $cast1; ?>">

    That's not working - output is as above, so I assume I'm doing something wrong. To be certain the variable is there, it is now in the header, not an include.

  6. McShelby
    Member
    Posted 8 years ago #

    Try

    <?php
    $cast1="file.txt";
    ?>

    You just forgot the quotes ;)

  7. Mark (podz)
    Support Maven
    Posted 8 years ago #

    McShelby - I had indeed :)

    The above does work perfectly, thank you !

    I then tried to embed the variable inside an Object tag - and failed. But that's a different story...

  8. McShelby
    Member
    Posted 8 years ago #

    Okay, storytellers night. Tell me the whole story...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags