All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create Dynamic Hyperlink In PHP

Last Updated : Mar 11, 2024

How To Create Dynamic Hyperlink In PHP

In this tutorial we will show you the solution of how to create dynamic hyperlink in PHP, dynamic links like the link which changes its value whenever clicked.

For example, we create a webpage for an institution in which there are many students.

And we have to edit the details like marks of students using an edit button that is given in front of particular student.

So, when we click on edit button then records of that particular student must be open.

Hence, today we see how to done this using dynamic hyperlink.

Step By Step Guide On How To Create Dynamic Hyperlink In PHP :-

Here, let us assume that we get some data from database, database used here is MySQL.

Here, we see that we fetch data from database and show this in a table.

Inside that table we are going to create a static column in which we create a hyperlink which edits and deletes the data of its particular row in next webpage.

Here, we are only going to show the concept of dynamic hyperlinks in php not updating and deletion of data.

<table border="1px solid black" style="width: 100%;text-align: center; border-collapse: collapse; font-family: Arial, Helvetica, sans-serif;font-size: 13px;">
                            <tr class="header_row">
                                <th>
                                    Id
                                </th>
                                <th>
                                    Page Name
                                </th>
                                <th>
                                    Page content
                                </th>
                                <th>
                                    Status
                                </th>
                                <th>
                                    Edit
                                </th>
                                <th>
                                    Delete
                                </th>
                            </tr>
                    // first create a database connection using mysqli_connect() and store it inside $connect.
                 $query = " select * from page";
               $result = mysqli_query($connect, $query);
                            while ($row = mysqli_fetch_assoc($result)) {
                            ?>
                                <tr>
                                    <td>
                                        <?php echo $row['id'] ?>
                                    </td>
                                    <td>
                                        <?php echo $row['name'] ?>
                                    </td>
                                    <td>
                           <?php echo $row['content'] ?>
                                    </td>
                                    <td class="red_color">
                           <?php echo $row['status'] ?>
                                    </td>
                                    <td>
       <a href="addpage.php?eid=<?php echo $row['id'] ?>">
                                            edit
                                        </a>
                                    </td>
                                    <td>
                                          <a href="pagesummary.php?did=<?php echo $row['id'] ?>">
                                        Delete
                                    </a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </table>
  1. First, here we create a table with some CSS.
  2. Then we add some headings using th tag inside tr to give the table a better look.
  3. Now, as we know that the data is dynamic used here from database and number of rows inserted in table depends upon the data inside database. So, we use only single td inside while loop.
  4. We create a database connection and fetch data from database using queries. At last data is fetch using mysqli_fetch_assoc(). And displayed in table using while loop.
  5. As, we see we create edit and delete button dynamic using hyperlinks. This is done using anchor tag. inside the href attribute of anchor tag, we ive the reference link of that page which we want to open with one additional key.
  6. This key give the id of that record occupied from database. And in next page we use database and queries to retrieve that record of particular id.
  7. Hence, this all is done using href attribute whereas a unique id of particular record is assigned to hyperlink which helps to create a dynamic hyperlink. You can also see the above example for better understanding.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create a dynamic hyperlink in php.

I hope this tutorial on how to create dynamic hyperlink in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪