1

I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..

echo '<a href="./customer-files/';
        echo $customerID;
        echo '/';
        echo $filename->getFilename();
        echo '">';
              echo $filename->getFilename();
    echo '</a>';

5 Answers 5

1

Try with

echo "<a href=\"./customer-files/{$customerID}/{$filename->getFilename()}\">{$filename->getFilename()}</a>";

Here there is the documentation with a lot of examples of how to concatenate output.

Sign up to request clarification or add additional context in comments.

4 Comments

I'd not recommend that approach, since it doesn't highlight HTML in an IDE, and may require horizontal scrolling when editing. I think lines should be shorter, where possible.
Ah, and your variables won't get expanded, since you're inside single quotes, not double quotes.
I've corrected the single quotes issue. Regarding your first comment, I have not thought about IDEs. About the line length, it's just a matter of personal tastes I think. Thanks anyway!
No worries, the swap of ideas is good :), +1. There used to be a recommendation for 80 characters, though with wider screens these days that is perhaps a little defunct; I work to 100. It's worth noting though that some template code doesn't allow for it (e.g. a long <a> plus its content - breaking the content into a new indent introduces unwanted space). More here if you're interested.
0

I'd approach it like this:

$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "<a href=\"./customer-files/$safe_customer_id/$safe_filename\">$safe_label</a>";

2 Comments

Thanks :) , will this remove any spaces etc from the filename too ?
It won't remove them, it will replace them with URL safe alternatives … but that's a good point. It shouldn't be done on the visible filename. (Answer edited)
0

I would go with this:

$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo '<a href="'.$link.'">'.$fn.'</a>';

Comments

0

If you're using a template layer, it is even better to break out into PHP only when you need to:

<a href="./customer-files/<?php
    echo $customerID . '/' . $filename->getFilename()
?>">
    <?php echo $filename->getFilename() ?>
</a>

This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).

Comments

0

Concatenation is your friend. Use a . to combine multiple string expression into one.

echo '<a href="./customer-files/'.$customerID.'/'.$filename->getFilename().'">'.$filename->getFilename()/'</a>';

Even better way would be

$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
  // ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.

3 Comments

Problem with your <a> is that you're using single quotes for attributes; that'll not produce standards-compliant HTML, afaik.
Just looked, and apologies - you're quite right. I was quite positive HTML5 and XHTML both require speech marks rather than apostrophes for attributes, and it turns out neither of them do. Even XML! :-o I had no idea.
@halfer, Great, I am removing my comment then !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.