Oke Tega
Oke Tega

Reputation: 883

Make text a link to row in database using php

I have uploaded and displayed information to and from my database. Now am looking for a way to make a title of my output a link to display data in a particular row of my database. Is this possible. If yes, how do i do this.

<?php
require_once("db.php");

$db = new MyDb();

        $sql =<<<EOF
SELECT * FROM addcategory ORDER BY catID DESC;
EOF;

    $ret = $db->query($sql);

    while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
        $catname = $row['catname'];
        $catdes = $row['catbrief'];
        $catimage = $row['catpic'];

        echo "<div class=\"catDescription\">
<div class=\"catname\"><p>$catname</p></div> //I need this to be a link to display particular row in database
    <div class=\"catImage\"><img src='".$catimage."'></div>
    <div class=\"catprof\"><p>$catdes</p></div>
    </div>";
    }

?>

This is more or less like showing summary on a page and then on clicking on the title, it shows full content on another page.

Please is this possible and how do i achieve this?

Upvotes: 1

Views: 39

Answers (1)

David
David

Reputation: 218960

Making a link is simply using an <a> element. Something like:

"<p><a href=\"details.php?id=$catid\">$catname</a></p>"

I've used a currently nonexistent variable called $catid, of course. Essentially what you'd need is some unique identifier for the record being "clicked on". Maybe $catname is that identifier instead? That's up to you.

(Though if you use a text string as an identifier then you may want to URL-encode it first.)

Once you have that, you can use it on the details.php page that you'd need to build. The value would be in:

$_GET['id']

With that value you can query the database for the identified record and display it on the page.

Upvotes: 1

Related Questions