Jeremy
Jeremy

Reputation: 350

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



?>

Upvotes: 0

Views: 190

Answers (3)

testo
testo

Reputation: 1260

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.

Upvotes: 0

Eduardo Ramos
Eduardo Ramos

Reputation: 416

Is the name of the field "date&place"?

Beware that the ampersand in the URL should be escaped because if not the $_GET will take "place" as another and diferent parameter.

Upvotes: 1

MatejG
MatejG

Reputation: 1423

It's correct setting order like this. But there is mysql_query which doesn't make sense. Just drop it out. For security reasons (SQL Injection) I sugest you to escape string before using in query.

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

Upvotes: 1

Related Questions