Zdarec.
Chcel by som filtrovat/zoradit data v tabulke ale bohuzial neviem kde je chyba pretoze mi to nechce zoradovat tak ako chcem. Prve zoradenie je v pohode ale ak chcem napriklad podla ID alebo description nikdy mi to nezoraduje ako ma.
Pointa je jednoducha odoslam formular a podla toho co som vybral upravujem select.
<?php
if (isset($_GET['sortby'])) {
if (($_GET['sortby']) == "ASC" ){
$sortbys = "ASC";
}
else{
$sortbys = "DESC";
};
}
else{
$sortbys = "ASC";
};
if (isset($_GET['orderby'])) {
$orderbys = htmlspecialchars($_GET['orderby']);
try{
$query = $dbh->prepare("SELECT * FROM code ORDER BY '.$orderbys.' '.$sortbys.' ");
$query->execute();
$result = $query->fetchAll();
}
catch(PDOException $e)
{
echo $dbh . "<br>" . $e->getMessage();
}
$dbh = null;
}
else
{
try{
$query = $dbh->prepare("SELECT * FROM code ORDER BY id '.$sortbys.'");
$query->execute();
$result = $query->fetchAll();
}
catch(PDOException $e)
{
echo $dbh . "<br>" . $e->getMessage();
}
$dbh = null;
}
echo
"<table class='table table-responsive'>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Name</th>
<th>Code</th>
</tr>"
;
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['code'] . "</td>";
}
echo "</tr>";
echo "</table>";
?>