[Solved] Displaying data from 2 tables as links


Consider this an example:

First you have to extract those values using PHP. After the extraction, you can now present it on HTML. You can use a dropdown box (here in this example), to select which category you want to see.

index.php

<?php

// use $_GET variable for your query
$category = (isset($_GET['category']) && $_GET['category'] != '') ? (int) $_GET['category'] : null;

// connect to mysql
$con = mysqli_connect("localhost","user","password","database");

// build your query
$query_statement="SELECT `listing`.`id`, `listing`.`cat_id`, `category`.`category`, `listing`.`title`, `listing`.`author` FROM `listing` LEFT JOIN `category` ON `listing`.`cat_id` = `category`.`cat_id`";
if($category != null) {
    $query_statement = $query_statement . " WHERE `category`.`cat_id` = $category";
}

$query = mysqli_query($con, $query_statement);

?>

<!-- HTML -->
<!-- Loop them inside a table -->
<form method="GET" action="index.php">
    <select name="category" onchange="this.form.submit()">
        <option disabled selected>Select Category</option>
        <option value="">All</option>
        <option value="1">Art</option>
        <option value="2">Drama</option>
        <option value="3">Music</option>
        <option value="4">Fiction</option>
        <option value="5">Computer</option>
    </select>
</form>

<table border="1" cellpadding="10">
    <thead>
        <tr>
            <th>Category</th>
            <th>Title</th>
            <th>Author</th>
        </tr>
    </thead>
    <tbody>
    <?php while($result = mysqli_fetch_assoc($query)): ?>
        <tr>
            <td><?php echo $result['category']; ?></td>
            <td><?php echo $result['title']; ?></td>
            <td><?php echo $result['author']; ?></td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>

EDIT:

<!-- LINKS (if you need links you can do something like this) -->
<a href="https://stackoverflow.com/questions/23334349/index.php?category=1">Art</a>
<a href="index.php?category=2">Drama</a>
<a href="index.php?category=3">Music</a>
<a href="index.php?category=4">Fiction</a>
<a href="index.php?category=5">Computer</a>

Note: Try to study more of these and then try to enhance some of its parts, like security, etc. Just use this as a stepping stone towards learning more since you did not posted any code related to the problem.

4

solved Displaying data from 2 tables as links