2

I have read a lot of submissions, I haven't been able to find an answer to my exact question.

I am filling out an HTML form that is sent into mysql database - I am good there.

The results are displayed using php in a html table. I would like the website URL to show as a hyperlink. Everything I do results in an error.

Does anyone know how I can update my code to allow for the website url to show as a hyperlink?

[$dbname = "seller";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT website, url, price, name, email FROM listing";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>Website</th><th>URL</th><th>Price</th><th>Contact      Name</th><th>Email</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. "  </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " .    $row["email"]. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

$conn->close();
?>]

Thanks

1
  • Wrap the url within an anchor. '<a href="' . $row['url'] . '">Caption</a>' Commented Jul 16, 2016 at 6:23

7 Answers 7

3

Have you tried this...

echo "<tr><td></td><td><a href='" . $row['url']. "'>" . $row['website']. "</a>  </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " .    $row["email"]. "</td></tr>";

You could also just change 'Link to website' to " . $row["website"]. " for it to display the link

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

Comments

1

If you use an a tag it should work.

<a href="' . $row['url'] . '">URL</a>

Comments

1
echo "<tr><td><a href=\"{$row['url']}>{$row['website']}</a></td>...etc

You should also consider closing the tags to get a cleaner code:

<?php if ($condition) { ?>
    <div>HTML code!</a>
<?php } ?>

3 Comments

I am new to php, where in my code would I put this? Thanks again for your help.
You have to surround the text you want to be a link with <a> , like this: <a href=\"{$row['url']}>{$row['url']}</a>. I guess you are new to HTML as well, as this a problem related to HTML and not PHP.
Yes and no. I have been working with HTML for a while. I am just really getting back into it and trying to improve my skills. So, sometimes I over think answers thinking I am missing something. I closed all my tags. Thanks for replying back.
1

Add an anchor tag <a href=""> inside the table data tag <td> inside your while loop.

Change this line:

echo "<tr><td>".$row["website"]."</td><td>".$row["url"]."  </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";

Into this:

echo "<tr><td><a href='".$row["website"]."'>".$row["website"]."</a></td><td>".$row["url"]."  </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";

Comments

1

That will be great if you can show me the error otherwise Try this:

while($row = $result->fetch_assoc()) {
     echo "<tr><td>" . $row["website"]. "</td><td><a href='".$row["url"]."'>Link</a> </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " .    $row["email"]. "</td></tr>";
 }

2 Comments

It actually made the url an extension of my website. Example: google.com/url so it didn't actually make it a url to the clients page.
Disregard, figured it out.
0

use this

echo '<tr><td>' . $row["website"]. '</td><td><a href=" ' . $row["url"]. ' ">'; echo $row["name"];    echo'  </a> </td><td>' . $row["price"]. '</td><td>' . '</td><td> ' .    $row["email"]. '</td></tr>';

instead of

echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. "  </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " .    $row["email"]. "</td></tr>";

Comments

0

use <a> tag, please see this link.

'<a href="' . $row['url'] . '">' . $row['url'] . '</a>'

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.