Forum Moderators: coopster
("SELECT title_format FROM my_table ORDER BY title_name");
then that is what is displayed. And then still have the ability to change that to:
("SELECT title_rating FROM my_table ORDER BY title_format");
that upon submitting the form data then that is what is displayed. Of course I would like to have a default query displayed on the pages initial load. Is this possible?
$myArray = array("*","title_name","title_rating","title_format");
$default = dbquery("SELECT * FROM my_table ORDER BY title_name");
$result = dbquery("SELECT ".$myArray[my_choice_goes_here]." FROM my_table ORDER BY ".$myArray[my_choice_goes_here_2]."");
This forum's library [webmasterworld.com] has a good thread on form submission, Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]. Of course you're not emailing anything but the first part should prove helpful. You can implement a default by checking whether or not the form has been posted ($_POST['your_select_name'] won't be set).
Table Structure of displayed data ordered by name:
___NAME___¦___RATING___¦___FORMAT___
____A____________9___________Z
____A____________7___________X
____B____________9___________X
____C____________7___________Z
1) My page loads. It displays vast amounts of data ordered by NAME.
2) Users then uses a select list, (dropdown list), to choose a filtering and sorting of the data.
If the user chooses format 'X' then only data is returned with the X format ordered by name by default
3) The user then chooses to ORDER it by it's 'Rating'. It would the return these results:
___NAME___¦___RATING___¦___FORMAT___
____B___________9_____________X
____A___________7_____________X
I'm fairly new to Server-Side scripting and pointing me towards another 'how-to' form validation or e-mail form tutorial is getting me nowhere because neither of these actually seem to cover what I would like to do. This seems to be some highly guarded secret script because I cannot find any source code examples anywhere. I know it can be done because I've seen it in action.
See it in action here: [themes.php-fusion.co.uk ]
And yes, before anybody asks me, I have asked the admins of the above site about this and ain't nobody there going to help out in any way what-so-ever with this script.
It's not a highly guarded secret script; I told you the answer in my first reply and referred you to the library thread for further explanation since it didn't appear from your first post that you were familiar with form submission. And, as a matter of fact, there's another thread on this very page that shows you how to do what you want to do, so you don't even need to use this site's search feature.
If you create a form on your page with method="post" and two selects, one with name="my_choice_goes_here" and another with name="my_choice_goes_here2" then:
if(!isset($_POST['my_choice_goes_here']))
$result = dbquery("SELECT * FROM my_table ORDER BY title_name");
else
$result = dbquery("SELECT ".$myArray[$_POST['my_choice_goes_here']]." FROM my_table ORDER BY ".$myArray[$_POST['my_choice_goes_here_2']]."");
I'm assuming you've already created a function called dbquery. You also need to validate the posted data as opposed to using it directly as it is above, otherwise you're vulnerable to sql injection.
I've read and re-read all these threads and blogs and still am trying to understand how this works. Please don't be mad because I'm slower than most.
If the code snippet I posted still isn't helping you, let's attack it from a different angle: post what you figure is the relevant section of script that you've got so far and we'll go from there. What names did you assign to the select elements?