PDA

View Full Version : Sorting order with a link


mangofreak
05-08-2011, 06:43 PM
hi to all, it's been very long while...

I am working on a page that displays properties for sale and I would like to have an option for the user to see the properties by price: high to low or vice versa.

How can this be accomplish using a link or is there another method?

any samples?
thanks.

edbr
05-09-2011, 01:42 AM
this is one i wrote fora similar seacr using forms for search values

if (array_key_exists('area', $_POST)) {
$area = $_POST['area'];
$where ="where area = '$area' ";
}

if (array_key_exists('beds', $_POST)) {
$beds = $_POST['beds'];
if($_POST['beds'] !=' '){$where ="where beds = $beds ";}


elseif($_POST['beds'] ==' '){ $where =" ";}
else $where =" ";
}




and the select statement"SELECT * FROM table $where

mangofreak
05-09-2011, 01:53 PM
Thanks for the reply I will try it out.

Cheers.

davidj
05-09-2011, 02:07 PM
I would use the ORDER BY clause

$var = ($_POST['checkbox']=='D')? 'DESC' : 'ASC';

SELECT * FROM TABLE WHERE ........ ORDER BY price $var

mangofreak
05-09-2011, 06:49 PM
Somehow I cannot figure it out. This is what I have and even though doesn't work I think I may be close(hopeful thinking). BTW, I am using Dreamweaver to figure all this out. I work mostly on ASP but I have used PHP before, however, not advanced programming.


here is my query:

mysql_select_db($database_connSort, $connSort);
$query_rsAll = "SELECT * FROM numbers ORDER BY price $var";
$rsAll = mysql_query($query_rsAll, $connSort) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);




Here I use a link to send to the same page a param with value ASC or DESC which I would like to (somehow) replace on the query.

<table border="0" align="center" cellpadding="4" cellspacing="1">
<tr>
<td><a href="sort.php?param=ASC">ASC</a></td>
<td><a href="sort.php?param=DESC">DESC</a></td>
</tr>

<?php do { ?>
<tr>
<td colspan="2" align="center"><?php echo $row_rsAll['price']; ?></td>
</tr>
<?php } while ($row_rsAll = mysql_fetch_assoc($rsAll)); ?>

</table>


I also tried creating 3 queries. One with "select * from" for the landing page. Then a Select * from ORDER BY price ASC and the third query with ORDER BY price DESC

So when I click the link, depending on the value of the parameter, it should use the appropriate query. I failed trying that.

EDBR, I couldn't modify your script and Davidj I was wondering what the 'D' stands for and how would that work without a submit button? Also, where would I put this:

$var = ($_POST['checkbox']=='D')? 'DESC' : 'ASC';

I'm surprised that it seems so simple, but it is so complicated. At least for now ;)

mangofreak
05-09-2011, 07:52 PM
:D hehehe; I'm all teeth right now because I figured it out.

I have my links as described before. The code I used was and ElseIF statement.

$var = $_GET['param'];
if ($var == ""){

mysql_select_db($database_connSort, $connSort);
$query_rsAll = "SELECT * FROM numbers";
$rsAll = mysql_query($query_rsAll, $connSort) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);
}elseif ($var == 'ASC'){
mysql_select_db($database_connSort, $connSort);
$query_rsAll = "SELECT * FROM numbers ORDER BY price ASC";
$rsAll = mysql_query($query_rsAll, $connSort) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);
} elseif($var=='DESC'){
mysql_select_db($database_connSort, $connSort);
$query_rsAll = "SELECT * FROM numbers ORDER BY price DESC";
$rsAll = mysql_query($query_rsAll, $connSort) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);
}else{
mysql_select_db($database_connSort, $connSort);
$query_rsAll = "SELECT * FROM numbers";
$rsAll = mysql_query($query_rsAll, $connSort) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);
}


if one of you wizards can help me polish this to something more elegant, would be much appreciated.

Javier "El Mangofreak" :D