-1

I have the below table in my site... enter image description here and what I want is that when i click on the read more link to redirect me to the page view_announcement.php and in this page to display me the whole data for this specific row. For example, if we click on the link in the second row I want in the view_announcement.php to load all the data for this specific row.

My code in order to display this table is this...

<?php
$q = ($_GET['q']);

$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"project");
$sql="SELECT author,category,subject,content FROM announcements WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Author</th>
<th>Category</th>
<th>Subject</th>
<th>Content</th>

</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['author'] . "</td>";
    echo "<td>" . $row['category'] . "</td>";
    echo "<td>" . $row['subject'] . "</td>";
    echo "<td>" . '<a href="view_announcement.php">Read More</a>' . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

The view_announcement.php file doesn't contain any code yet because i dont know what to write.

Waaaaat
  • 634
  • 3
  • 14
  • 29

2 Answers2

1

One way to do it is to append a query variable to the "Read More" links. You'll probably need a unique identifier, such as an ID number, on your announements table. If you don't have one yet, I suggest adding one and setting it up to auto-increment.

You would want to modify your query to include the unique ID number:

$sql="SELECT id,author,category,subject,content FROM announcements WHERE category = '".$q."'";

Then you would modify the loop which prints your table out to include those unique IDs in the URL to view_announcement.php

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['author'] . "</td>";
    echo "<td>" . $row['category'] . "</td>";
    echo "<td>" . $row['subject'] . "</td>";
    echo "<td>" . '<a href="view_announcement.php?id='.$row['id'].'">Read More</a>' . "</td>";
    echo "</tr>";
}

And in your file view_announcement.php, you would make another SQL query to get the full data for a specific row, like this:

$sql="SELECT * FROM announcements WHERE ID = '".$_GET['id']."'";
Community
  • 1
  • 1
Ben Cole
  • 1,847
  • 1
  • 15
  • 18
0

If you click any button, that redirects to view_announcement.php file, where you can get the subject values.

Use that subject values in your query to get all the details which relates to that subject.

Jagadeesh
  • 734
  • 6
  • 26