Ich habe eine Zeile "Alter" hinzufügen mit Daten in einer Tabelle sortieren, indem Sie auf Spaltenname klicken. Ich habe viele Lösungen dafür versucht, aber nichts funktioniert. Ich brauche das neue Spalte "Alter": TIMESTAMPDIFF (YEAR, CURDATE(), Geburtstag) von MitgliedernWie "Alter" hinzufügen, um die Spalte zu sortieren, indem Sie auf Spaltenüberschrift klicken?
<?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: Alter in Zeile:„$ row [php echo‘ Alter '] "
Können Sie mir helfen? Vielen Dank.
nach dem während hinzufügen 'print_r ($ row);' und die Ergebnisse –
Dank posten, aber n ot arbeit .... name, vorname, geburtstag, ort sind aus SQL Table, ich möchte temporäres "alter" machen .... Wie und wo? Dank –
Alter ist in der Tabelle nicht daher das Problem –