0

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();
?>
0

1 Answer 1

1

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>

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

3 Comments

Yep! I do have an id in my database. Even though the url gets changed with the clicked client's id, I should have another page were they get redirected based on that specific need shouldn't I?
Yes, you have to write a customerExpenses.php script, that expects to get passed id in the $_GET array - $GET_['id'] and in there you query the db based on knowing who the user is and show whatever other data the page was designed for
Sorry thats $_GET['id']

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.