Ask a Question related to Dreamweaver AppDev, Design and Development.
-
TheRapidGroup #1
Sorting Database Results in PHP
I am working on putting a drop down menu on my page which allows the user to
change the order of which he/she wants to view the database results, for
example by date, price, etc...
I guess where I run into trouble would be the SQL query. I understand the
statement ORDER BY price ASD or DESC. But how can I rewritre my statement so
the user has a choice to change the order of things??
Any help would be greatly appreciated!
TheRapidGroup Guest
-
Database Results
Hi I have created a results page so that when a users types something in my search field on the web the results page shows a list of products.... -
Why Does Sorting Require Another Call To The Database?
I hope someone can help with this. Every example I've seen, and every example I've written, where I fill a datagrid from a recordset I get from... -
Database results error
I've originally posted this in the front page extensions page, but was told to stop by here. Here's a copy of what we said. Any ideas on how... -
Sorting a list held in a database
Hi all, I need to build a list of text items. Using asp I want to add text items to a list and then sort the list in the order I wish. The... -
scoring/sorting db search results based on score
looking for code snippets or links to examples of the following; - Have a database with multiple fields that will be searched against (happens to... -
Tom Muck #2
Re: Sorting Database Results in PHP
"TheRapidGroup" <webforumsuser@macromedia.com> wrote in message
news:cvq5lp$bju$1@forums.macromedia.com...to> I am working on putting a drop down menu on my page which allows the userthe> change the order of which he/she wants to view the database results, for
> example by date, price, etc...
>
> I guess where I run into trouble would be the SQL query. I understandso> statement ORDER BY price ASD or DESC. But how can I rewritre my statementIf you're using a drop-down menu, you could do something like this:> the user has a choice to change the order of things??
>
> Any help would be greatly appreciated!
<?php
$sortorder = "ASC";
$sortfield = "Price";
if(isset($_POST["sortorder"])) {
$sortorder = $_POST["sortorder"];
}
if(isset($_POST["sortfield"])) {
$sortfield= $_POST["sortfield"];
}
then in your SQL statement:
"SELECT blah FROM mytable
ORDER BY $sortfield $sortorder"
Your dropdowns would look something like this:
<select name="sortorder">
<option value="ASC">Ascending</option>
<option value="DESC">Descending</option>
</select>
<select name="sortfield">
<option value="Price">Price</option>
<option value="field2">Field2</option>
etc.
</select>
I also have a commercial extension for sorting tables that you may find
suits your needs, as it automates things:
[url]http://www.tom-muck.com/extensions/help/sortrepeatregion/[/url]
--
-------------------------------------------
Tom Muck
co-author Dreamweaver MX 2004: The Complete Reference
[url]http://www.tom-muck.com/[/url]
Extending Knowledge, Daily
[url]http://www.CommunityMX.com/[/url]
Tom Muck Guest
-
TheRapidGroup #3
Re: Sorting Database Results in PHP
Thanks for replying, I made a few minor changes to the last few lines of code
only because I was getting "File Not Found" error... but I want to show you
what I have done thus far:
PHP
<?php
$sortorder = "ASC";
$sortfield = "Price";
if(isset($_POST["sortorder"])) {
$sortorder = $_POST["sortorder"];
}
if(isset($_POST["sortfield"])) {
$sortfield= $_POST["sortfield"];
}
SQL QUERY:
FROM clocks ORDER BY $sortfield $sortorder
MENUS
<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<select name="sortorder">
<option value="?sortorder=ASC">Ascending</option>
<option value="?sortorder=DSC">Descending</option>
</select>
<select name="menu2" onChange="MM_jumpMenu('parent',this,0)">
<select name="sortfield">
<option value="?sortfield=price">Price</option>
<option value="?sortfield=item">Item</option>
</select>
Now when I select one of these two items, it would appear to be executing
except nothing on the page changes in terms of sorting.... can you see where I
am going wrong?
TheRapidGroup Guest
-
Michael Fesser #4
Re: Sorting Database Results in PHP
.oO(Tom Muck)
Ever heard of SQL injection?>$sortorder = "ASC";
>$sortfield = "Price";
>
>if(isset($_POST["sortorder"])) {
> $sortorder = $_POST["sortorder"];
>}
>if(isset($_POST["sortfield"])) {
> $sortfield= $_POST["sortfield"];
>}
>
>then in your SQL statement:
>
>"SELECT blah FROM mytable
>ORDER BY $sortfield $sortorder"
Micha
Michael Fesser Guest
-
Tom Muck #5
Re: Sorting Database Results in PHP
where I> Now when I select one of these two items, it would appear to be executing
> except nothing on the page changes in terms of sorting.... can you seeYou're using query strings instead of form variables, so you have to change> am going wrong?
your POSTs to GETs. Also, Michael brings up the point of SQL injection,
which you should also concern yourself with, by using something like this
instead:
if(isset($_GET["sortorder"])) {
$sortorder = (!get_magic_quotes_gpc()) ?
addslashes($_GET["sortorder"]; ) : $_GET["sortorder"];;
}
Tom
Tom Muck Guest



Reply With Quote

