douni9620 2016-03-21 23:46
浏览 33
已采纳

如何通过单击列标题添加“年龄”来对列进行排序?

I am having an add row "age" with sorting data in a table by clicking on column name. I tried many solutions for this, but nothing works. I need the new column "age": TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag) from members

<?php
// first creating database connection
$servername = "localhost";
$username = "user";
$password = "password";
$database = "celebrates"; //this will contain name of a database
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

mysqli_set_charset($conn, "utf8");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$action = '';
$sql = "SELECT * from members";
$where = '';
if(isset($_GET["id"]))
{
    $id = $_GET["id"]; //geting id value which we are passing from table headers
    $action = $_GET["action"]; // geting action value which we are passing from table headers

    //we are checking condition if $action value is ASC then $action will set to DESC otherwise it will remain ASC
    if($action == 'ASC')
    {
        $action='DESC';
    } else {
        $action='ASC';
    }

    if($_GET['id']=='id') {
        $id = "id";
    } elseif($_GET['id']=='name') {
        $id = "name";
    } elseif($_GET['id']=='vorname') {
        $id="vorname";
    } elseif($_GET['id']=='geburtstag') {
        $id="geburtstag";
    } elseif($_GET['id']=='age') {
        $id = "age";
    } elseif($_GET['id']=='ort') {
        $id = "ort";
    }
    $where = " ORDER BY $id $action ";
    $sql = "SELECT * FROM members " . $where;
}
?>



<!DOCTYPE html>
<html lang="de">
<table>
  <tr>
    <th><a href="site.php?id=<?php echo 'name';?>&action=<?php echo $action;?>">NAME</a></th>
    <th><a href="site.php?id=<?php echo 'vorname';?>&action=<?php echo $action;?>">VORNAME</a></th>
    <th><a href="site.php?id=<?php echo 'geburtstag';?>&action=<?php echo $action;?>">GEBURTSTAG</a></th>
    <th><a href="site.php?id=<?php echo 'ort';?>&action=<?php echo $action;?>">ORT</a></th>
    <th><a href="site.php?id=<?php echo 'age';?>&action=<?php echo $action;?>">AGE</a></th>
 </tr>

<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Fetch a result row as an associative array
    while($row = $result->fetch_assoc()) {
?>

  <tr>
   <td><?php echo $row["name"];?></td>
   <td><?php echo $row["vorname"];?></td>
   <td><?php echo date("j. F Y", strtotime($row["geburtstag"]));?></td>
   <td><?php echo $row['ort'];?></td>
   <td><?php echo $row['age'];?></td>
 </tr>

<?php
    }
    echo '</table>';
    echo '</div>';
} else {
    echo "0 results";
}

$conn->close();

Notice: Undefined index: age in line : "php echo $row['age']"

Can you help me? Thanks.

  • 写回答

2条回答 默认 最新

  • doulin2025 2016-03-21 23:58
    关注

    You probably do not have an column called age in your members table. Just enhance your query a little bit:

    $sql = "SELECT members.*, TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag) as age FROM members " . $where
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗