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>";
                }
eduardoimm
  • 11
  • 5
Daniel
  • 311
  • 3
  • 5
  • 16

4 Answers4

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.

Kervon Ryan
  • 684
  • 1
  • 10
  • 20
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>";
}
Tpojka
  • 6,996
  • 2
  • 29
  • 39
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

Community
  • 1
  • 1
Babajide Apata
  • 671
  • 6
  • 18
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;
Sylwit
  • 1,497
  • 1
  • 11
  • 20