0

I'm trying to concatenate some href with my fetched value form my DB. But I got lost in '' . "" \

I'm sure I'm missing something small but I just can't see it.

while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo."<td>" . $row["interest"] . "</td>"
                        ."<td>"."<a href=\"http://localhost/page/files/".$row["filename"]\"/>" . $row["filename"] . " </a></td>"
                        ."<td>" . $row["reg_date"] . "</td>";
                }
2
  • Replace $row["filename"]\"/>" with $row["filename"] . "\"/>" Commented Sep 11, 2016 at 21:08
  • Btw. you have excessive dot after second echo. Commented Sep 11, 2016 at 21:13

4 Answers 4

2

I suggest you use single quotes instead of double quotes for your html values as this can confuse things.

You also had a lot of mistakes. I wont go into detail but compare your code to this and you should see them.

I fixed the code for you.

while ($row = $result->fetch_assoc()) {
                    $returnedHTMl = "<tr>";
                    $returnedHTMl .= "<td>" . $row["interest"] . "</td>"
                    $returnedHTMl .= "<td><a href='http://localhost/page/files/".$row["filename"]."'>" . $row["filename"] . " </a></td>"
                    $returnedHTMl .= "<td>" . $row["reg_date"] . "</td>"; 

                    echo $returnedHTML
}

@tpojka is also correct you should assign variables but I kept it similar to your code as I am not aware as to your reasons for doing it this way.

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

Comments

1

Until you get your mind back, check this example - assign variables to values:

while ($row = $result->fetch_assoc()) {
    $interest = $row["interest"];
    $filename = $row["filename"];
    $reg_date = $row["reg_date"];
    echo "<tr><td>$interest</td><td><a href=\"http://localhost/page/files/$filename\"/>$filename</a></td><td>$reg_date</td>";
}

Comments

0

I believe you want to do something like this,

echo "<a href='".$link_address."'>Link</a>";

check this for more insight

add-php-variable-inside-echo-statement-as-href-link-address

Comments

0

When you have a lot of concatenation like this you can use the HEREDOC syntax which is a lot more readable

http://php.net/manual/fr/language.types.string.php#language.types.string.syntax.heredoc

$out <<<EOT
<tr>
<td> {$row["interest"]} </td>
<td><a href="http://localhost/page/files/{$row["filename"]}"> {$row["filename"]} </a></td>
<td> {$row["reg_date"]} </td> 
EOT;

echo $out;

Comments

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.