Sergio Angel
Sergio Angel

Reputation: 37

How to dynamically open a new page using this php code?

I have this php code which basically retrieves information from a MySQL database and displays it in a table.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <title>Document</title>
</head>
<body>
<?php
    if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
        $output="Connection failed: " . $conn->connect_error;      
    } else {
        $sql = "SELECT id, subject, requester FROM escalationreview";
        if ( $result = $conn->query($sql) ){
            if ($result->num_rows > 0) {
                $output="<table> 
                <tr align='left'>
                <th>ID</th>
                <th>Subject</th>
                <th>Requester</th></tr>";
                while($row = $result->fetch_assoc()) {
                    $output.= "<tr><td>". $row["id"]. "</td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td></tr>";
                }
                $output.="</table>";
            } else { 
                $output= "0 results"; 
            }
        } else {
                $output="Error en la consulta: ".$conn->error;
        }
        $conn->close();
    }
    echo $output;
?>
</body>
</html> 

This is what my index page looks like:

enter image description here

Now, what I want to achieve is that, when I click in the ticket ID, it opens up that ticket page. So, for example, if I click in ticket with ID 1234 it opens 1234.php and if I click in ticket with ID 1235 it opens 1235.php and so on.

I'm new to php and honestly I have no clue how to achieve this.

Thanks to anyone willing to assist and sorry if this seems like a dumb question. Thanks again.

Upvotes: 2

Views: 83

Answers (1)

Harshana
Harshana

Reputation: 5476

You can use a common page to pass the ticket ID and retrieve the details.

First, you have to wrap the ticket ID as an anchor link to the page you want to redirect. The link also should have the Ticket ID as a parameter.

$output.= "<tr><td><a href='ticketpage.php?tid=".$row["id"]."'>". $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td></tr>";

Then, create a new page with the name ticketpage.php (You can change this if you need to) and retrieve the ID we passed via the link earlier by using the code example below:

<?php 
    $tid = $_GET['tid'];
    // Remaning code

Upvotes: 3

Related Questions