0

I am trying to make a Q/A webpage. I fetched the data from database through PHP which looks like: This is the fetched data image.

Now, how can I make each data clickable, so that after clicking on specific question, It will open a modal or a new page where the data of clicked question goes.

My code is:

   <?php     
 //Start to show '...' after 200 words. 
         function limited_echo($x, $length){
            if(strlen($x)<=$length){
            echo $x;}   
            else{
            $y=substr($x,0,$length) . '...';
            echo $y;}}
 //End Here
 //Main Code
    include_once 'inc/connection.php';
    $record= "SELECT * FROM questions";  
    $getdata = mysqli_query($link,$record);
    if(! $getdata ) {
      die('Could not get data');
    }
    while($row = mysqli_fetch_array($getdata)){
    ?>
   <div class="section">  
         <h1><?php echo "{$row['title']}"; ?></h1>
         <p><?php limited_echo($row['description'], 200); ?></p>
   </div>
<?php
    }
 ?>

Hope, This information is enough. If anything extra needed please let me know.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Dishu
  • 29
  • 9

1 Answers1

0

Since HTML5 you can make <div> tags linkable using <a>.

<a href="[url here]">
    <div class="section">  
        <h1><?php echo "{$row['title']}"; ?></h1>
        <p><?php limited_echo($row['description'], 200); ?></p>
    </div>
</a>

For the URL that it links to, it would be useful to have an ID that each post links to, and using that ID on that page you would need to look up the full post information in the database.

www.example.com/post.php?id=5 //post with the id 5

You would then use the $_GET['post'] variable to look up the post in the database. (Warning: Ensure to escape this variable when doing the lookup or you may end up with an SQL injection attack.)

Community
  • 1
  • 1
f.w.
  • 38
  • 4