Dom
Dom

Reputation: 13

Hyperlink in PHP search

Good Afternoon, I am very new to programming, and I have spent the last two days looking for answers to my problem, but I cant seem to get anything to work. I have a search box looking up first name and last name, and then it displays results based on that search. One of the items it displays is a website, but it only is displayed in text. How would I get the website to display as a hyperlink? $row["website"] Thanks for everyone's help!!

<?php

// connect to the database
include("connect.php");

$tyt = $_POST['submit'];
$txt = $_POST['submit2'];


$sql = "SELECT * FROM burials WHERE Lname like '$txt%' AND Fname like'%$tyt%' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         echo "<br> ". $row["Location"]. 
         "<br>" . $row["Fname"]. " " . $row["Lname"] .
         " <br> Date of Birth: " . $row["DateOfBirth"]. 
         " <br> Date of Death: ".$row["DateOfDeath"].
         " <br> ". $row["Website"]. 
         "<br>";
     }
 } else {
     echo "0 results";
 }
 ?>


<?php

$conn->close();
?>

Upvotes: 1

Views: 50

Answers (2)

TheSnooker
TheSnooker

Reputation: 935

This should work. You need to add an anchor tag to your echo.

  <?php

        // connect to the database
        include("connect.php");

        $tyt = $_POST['submit'];
        $txt = $_POST['submit2'];


        $sql = "SELECT * FROM burials WHERE Lname like '$txt%' AND Fname like'%$tyt%' ";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo "<br> ". $row["Location"].
                "<br>" . $row["Fname"]. " " . $row["Lname"] .
                " <br> Date of Birth: " . $row["DateOfBirth"].
                " <br> Date of Death: ".$row["DateOfDeath"].
                // add anchor tag around result from DB
                " <br> <a href=". $row["Website"].">".$row["Website"]."</a>"
                "<br>";
            }
        } else {
        echo "0 results";
        }
    ?>


    <?php

    $conn->close();
    ?>

Upvotes: 0

Companjo
Companjo

Reputation: 1792

Use this:

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

In your code:

<?php

// connect to the database
include("connect.php");

$tyt = $_POST['submit'];
$txt = $_POST['submit2'];


$sql = "SELECT * FROM burials WHERE Lname like '$txt%' AND Fname like'%$tyt%' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         echo "<br> ". $row["Location"]. 
         "<br>" . $row["Fname"]. " " . $row["Lname"] .
         " <br> Date of Birth: " . $row["DateOfBirth"]. 
         " <br> Date of Death: ".$row["DateOfDeath"].
         " <br> <a href=\"". $row["Website"].."\">". $row["Website"]. 
         "</a><br>";
     }
 } else {
     echo "0 results";
 }

$conn->close();
?>

Upvotes: 1

Related Questions