Coding_Harry
Coding_Harry

Reputation: 45

PHP Redirect to custom url

Good day!

I'm new to PHP and I just wanted to know, I'm creating a list of customers from the database directly and showing them on the website. Each customer name is an anchor tag that should lead to a specific url for this customer only, in order to display their expenses, for example expenses.php/johndoe...or something like that...

I'm able to show the customers with anchor tags on the page with the code below, but how can I do something like this with PHP?

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            margin:0 auto;
        }
    </style>
</head>
<body>
    
</body>
</html>

<?php 

class Users extends SQLite3 {
    function __construct() {
        $this->open("customers.db");
    }
}

$db = new Users();

if (!$db) {
    echo $db->lastErrorMsg();
} else {
    echo "";
}


$sql = "SELECT * FROM CUSTOMERS";

$result = $db->query($sql);
echo "<table>
<tr>
<th>Name</th>

";
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    echo "<tr>";
    echo "<td><a href='customerExpenses.php'>" . $row['NAME'] . "</a></td>";
    echo "<tr>";
}
echo "</table>";
$db->close();
?>

Upvotes: 0

Views: 112

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

I think you are missing the parameter on the href, I have used id assuming you have one of those on the table.

echo "<td><a href='customerExpenses.php?id=$row[id]'>$row[NAME]</a></td>";

Also you are missing </tr> after echo "<table><tr><th>Name</th> so amend to

echo "<table><tr><th>Name</th></tr>";
//                            ^^^^^

Also you need to move these line

</body>
</html>

to the end of the page as you are now outputting things in the PHP section that belong inside the <body>

Upvotes: 1

Related Questions