2016-03-21 40 views
-1

我有一個添加行「年齡」,通過單擊列名稱排序表中的數據。我嘗試了很多解決方案,但沒有任何效果。我需要新列 「時代」:從成員如何通過點擊列標題添加「年齡」來對列進行排序?

<?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(); 

通知 TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag):未定義指數:年齡線:「PHP的echo $行[」年齡']「

你能幫幫我嗎?謝謝。

+2

while while add'print_r($ row);'併發布結果 –

+0

謝謝,但是n ot work .... name,vorname,geburtstag,ort都來自SQL Table,我想讓臨時的「年齡」....如何以及在哪裏? 謝謝 –

+0

年齡不在表中,因此問題 –

回答

2

您的members表中可能沒有名爲age的列。只是提高你的查詢一點點:

$sql = "SELECT members.*, TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag) as age FROM members " . $where 
+0

這看起來像一個很好的計劃。 –

+0

沒有一列沒有列「年齡」 $ where =「ORDER BY $ id $ action」; $ sql =「SELECT members。*,TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())as age FROM members」; $ where =''; 這已經顯示年齡,但不排序通過點擊標題 –

+0

謝謝:) 謝謝。這是OK :) '$ sql =「SELECT members。*,TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())as age FROM members」。 $ where;' –

2

你不能計算年齡減去年,因爲年齡也計算也考慮天。

使用此查詢:

SELECT *, 
      YEAR(CURRENT_TIMESTAMP) - YEAR(geburtstag) - (SUBSTR(CURRENT_TIMESTAMP,6,5) < SUBSTR(geburtstag,6, 5)) as age 
     FROM members 

sqlFiddle demo

通過這個查詢,你從多年差減去1,如果當前月/日(SUBSTR(CURRENT_TIMESTAMP,6,5))比生日月/日小( (SUBSTR(geburtstag,6, 5)

+0

注意:未定義的索引:年齡在線:「php echo $ row ['age']」 謝謝,但不行。 你能幫我嗎?究竟在哪些行中。我是PHP新手, 同樣,我不會說英語。我是德國人。 –

+0

列「geburtstag」格式爲「日期」 –

+0

然後呢?它會在日期正常工作。你有沒有試過,它不適合你? – fusion3k

相關問題