Moopsish
Moopsish

Reputation: 137

How to get a select order in Mysql?

I'm trying to make a select box that changes the order of my MYSQL data. the code i got so far returns a blank page and I can't figure out why..

$host = "IP";
$user = "USER";
$pwd = "PW";
$db_name = "DBNAME";

echo "<p>Order by: <select name='order' id='order' onChange='document.getElementById('order').submit();'><option value='DESC'>Newest</option><option value='ASC'>Oldest</option></select>";

if(!isset($_POST['order']) {
    $order = "DESC";
} else {
    $order = $_POST['order'];
}

$link = mysqli_connect($host, $user, $pwd, $db_name)or die("cannot connect"); 

$sql = mysqli_query($link, "SELECT * FROM foto ORDER BY id ".$order." LIMIT 25") or die(mysqli_error($link));
//var_dump($sql);

while ($rows = mysqli_fetch_assoc($sql))
{
    echo "<img class='littleshow'"."id='foto".$rows['id']."'src='".$rows['foto']."' onclick='Bigscreen(this)'></img>";
} 

Upvotes: 0

Views: 72

Answers (2)

Anish
Anish

Reputation: 5058

Check for syntax error. I can see already one in the script

if(!isset($_POST['order']) {

to

if(!isset($_POST['order'])) {

Upvotes: 3

fire
fire

Reputation: 21531

There's an error in your code and the blank page is because you can't see the error message.

Try putting:

ini_set('display_errors', 1);
error_reporting(E_ALL);

At the top of your page.

Upvotes: 1

Related Questions