Jeremy Püringer Jeremy Püringer - 5 days ago 5
PHP Question

Order table by default

I am currently working on an event calendar which allows users to sort between date & place and date & place.

This is the code I use for the order:

$order = isset($_GET['sort'])?$_GET['sort']:'date_added';
$result = mysqli_query($con,"SELECT * FROM calendar ORDER BY $order");
$sql = mysql_query($query);


Now when you click on a button it sets the variable that will be used for the sorting like this

onclick="location.href=' index.php?sort=date&place'"


Now this works great, but when I start my event calendar, it's blank... how can I give my calendar a default order?

Here is the code for the output of the calendar:

echo "<table class='table table-striped' >
<thead>
<tr>
<th>Artist</th>
<th>Place</th>
<th>Date</th>
<th>Genre</th>
</tr>
</thead>";


while($row = mysqli_fetch_array($result))
{
$date = $row['date'];
$convdate = date('d-m-Y', strtotime($row['date']));

echo "<tr>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['place'] . "</td>";
echo "<td>" . $convdate . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";

}
echo "</table>";

mysqli_close($con);



?>

Answer

how can I give my calendar a default order?

What do you mean with "default order"?

$order = isset($_GET['sort'])?$_GET['sort']:'date_added'; $result = mysqli_query($con,"SELECT * FROM calendar ORDER BY $order"); $sql = mysql_query($query);

Never pass an untested value to a sql statement.

onclick="location.href=' index.php?sort=date&place'"

This will not result in $_GET['sort']=='date&place', the & in the url marks a new variable.

Comments