简体   繁体   中英

Get the value of column from the selected row in php

I'm a beginner in php and my instructor gave us a task. How can I get the value of the column in the selected row?

<?php
  include 'connection.php';

    $sqlsearch = "SELECT `Student_ID`,`First_Name`,`Last_Name`,`Year_Level`,`Enrollment_Date`,`Status` FROM `student_info`";
    $sqlresult = $connection->query($sqlsearch);
    $searchInput = "";

    if($sqlresult->num_rows <= 0){
        echo "No found Result";
    }
    if(!empty($_GET["search"])){
        $searchInput = trim_input($_GET["search"]);

        $sqlsearch = "SELECT `Student_ID`,`First_Name`,`Last_Name`,`Year_Level`,`Enrollment_Date`,`Status` FROM `student_info`
                      WHERE `Student_ID` = '". $searchInput ."' OR `First_Name` LIKE '%". $searchInput ."%' OR `Last_Name` LIKE '%". $searchInput."%'";
        $sqlresult = $connection->query($sqlsearch);
            if($sqlresult->num_rows > 0){
              while($row = $sqlresult->fetch_assoc()){
                    generateResult($row);
              }
            }else{

            }
    }
    else{
      while($row = $sqlresult->fetch_assoc()){
         generateResult($row);
      }
    }


  function generateResult($row){
      echo "<tr>";
      echo '<td style="color:#33F0FF"> <a href="#">'. $row["Student_ID"] .'</a></td>'; //Plz get the student ID of the selected ID.
      echo '<td>'. $row["First_Name"] .'&nbsp'. $row["Last_Name"] .'</td>';
      echo '<td>'. $row["Year_Level"].'</td>';
      echo '<td>'. $row["Enrollment_Date"].'</td>';
        if($row["Status"] == "Active"){
          echo '<td style="color:green">'. $row["Status"].'</td>';
        }else if($row["Status"] == "Dropped"){
          echo '<td style="color:orange">'. $row["Status"].'</td>';
        }else{
          echo '<td style="color:red">'. $row["Status"].'</td>';
        }
      echo "</tr>";
  }
 ?>

In the function generateResult($row) how can I get the value of Student_ID when I click the link?

If you need student_id with anchor tag , probably you want to go to other page on clicking the link . In that case , replace :

echo '<td style="color:#33F0FF"> <a href="#">'. $row["Student_ID"] .'</a></td>';

to

echo '<td style="color:#33F0FF"> <a href="xyz.php?student_id=">'. $row["Student_ID"] .'</a></td>';

Then on the xyz.php page , you will get student_id using $_GET["student_id"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM