2

I am trying to create an output table in PHP after extracting data from a database. The fourth column needs to be a hyperlink. I have used the following code. But I am getting error in regards to the hyperlink section. Could you please let me know how to rectify it?

Thanks!

 echo"<tr>
            <td>".$row["name"]."</td>
            <td>".$row["age"]."</td>
            <td>".$row["sex"]."</td>                                                                                                                                                                        
            <td><a href= "https://weblink.com/path1/path2/test.php?name='.urlencode($row["name"]).'&age='.urlencode($row["age"]).'">Click for next</a></td>

  </tr>";
3
  • 4
    You need to escape the quotes, \" inside the string, the quotes that surrounds the link. Commented May 9, 2017 at 20:37
  • 2
    or you can use single quotes around the link Commented May 9, 2017 at 20:41
  • And you need to end the string with the same quote you started it with. You started with double quotes, but then ended with single quotes before .urlencode Commented May 9, 2017 at 20:45

2 Answers 2

1

Looking at your forth <td> your code is attempting to end the echo after href=", hence you have to escape the quotation mark href=\". If this looks too ugly you can also use single quotes instead.

echo"<tr>
        <td>".$row["name"]."</td>
        <td>".$row["age"]."</td>
        <td>".$row["sex"]."</td>                                                                                                                                                                        
        <td><a href=\"https://weblink.com/path1/path2/test.php?name=".urlencode($row["name"])."&age=".urlencode($row["age"])."\">Click for next</a></td>
</tr>";
Sign up to request clarification or add additional context in comments.

2 Comments

A good answer contains a description what was changed, not just "try this". What did you change, and why? Although it might be obvious to some, its better to include a description of the issue.
I agree very much. Since I was short on time and there was no answer yet I figured it would help @dash to have some working code at least. I absolutely agree with you though that a good answer should include an explanation :-)
0

I started php not long ago and I realized that it can be useful to format your code differently for clarity. Try using your tags differently if you can, for exemple:

// end php and switch to html 
?> 

    <tr>
       <td><?php echo $row["name"]; ?></td>
       <td><?php echo $row["age"]; ?></td>
       <td><?php echo $row["sex"]; ?></td>                                                                                                                                                                        
       <td><a href= "https://weblink.com/path1/path2/test.php?name=<?php echo urlencode($row["name"]); ?>&age=<?php echo urlencode($row["age"]); ?>">Click for next</a></td>
    </tr>

    <?php // resume php

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.