1

I'm trying to add a view button to my table but I'm getting a syntax error, unexpected href. Seems like I'm wrong with the formatting. Still trying to learn PHP but is it possible to add href to the table?

Here's my code:

while($row = mysqli_fetch_array($result))
{
    $output .= '
        <tr>
            <td>'.$row["name"].'</td>
            <td>'.$row["temperature"].'</td>
            <td>'.$row["phoneno"].'</td>
            <td> '<a href='read.php?id='. $row['id'] .'' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;''></i></a>';' </td>
        </tr>
    ';
}
echo $output;

And here's the image for the color coding that seems wrong.

sublime

2
  • 3
    It is because you're using single quotes to delimit your strings while using single quotes in the strings to denote values. Commented Jul 30, 2020 at 14:38
  • Does this answer your question? PHP parse/syntax errors; and how to solve them Commented Jul 30, 2020 at 14:43

3 Answers 3

3

It is because you're using single quotes to delimit your strings while using single quotes in the strings to denote values.

This -

<td> '<a href='read.php?id='. $row['id'] .'' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;''></i></a>';' </td>

Should be this -

<td><a href="read.php?id='. $row['id'] .'" title="View Record" data-toggle="tooltip"><i class="fa fa-eye" style="font-size:30px; color: black;"></i></a></td>';
Sign up to request clarification or add additional context in comments.

1 Comment

I was able to understand after reading this article PHP parse/syntax errors; and how to solve them but the answer is a good guide that would help. Thanks so much for the help!!
3

Just mismatched quotes.

There're many ways to mix PHP and HTML and you've chosen a hard syntax. Please compare with e.g.:

<?php foreach($foo as $row) { ?>
    <tr>
        <td><?= $row["name"] ?></td>
        <td><?= $row["temperature"] ?></td>
        <td><?= $row["phoneno"] ?></td>
        <td>
            <a href='read.php?id=<?= $row['id'] ?>' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;'></i></a>
        </td>
    </tr>
<?php }

BTW, your are injecting raw text into HTML, beware that it can break your markup any time.

1 Comment

I flip flop between the two myself. When I can, I use the above method, however if I have to put all the html into a variable for method return, I do the concat style in the OP (because I detest using ob_funcs to capture pieces). All good examples here.
2

Your quotes are all over the place. You open a string variable with single quotes so every time you use single quotes unescaped in the string you just created will be interrupted.

It should be like this: <a href="read.php?id='.$row['id'].'" ... >

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.