Forum Moderators: coopster

Message Too Old, No Replies

Select List populating a MySQL Query?

         

proteus9

3:33 am on Nov 23, 2008 (gmt 0)

10+ Year Member



Is there a way to populate the dbquery by the choices made from a select list?
I believe I'll probably need to incorporate some Javascript to do this. Possibly jQuery?
Below you'll see in my array structure the options I'd like to have in my select list.
I would like to find a way so that when a user chooses, let's say:

("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]."");

cameraman

5:53 am on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't think of a reason you'd need to incorporate javascript other than maybe if you wanted to change the page content without reloading the page.
The array will be indexed by your form element's name, either from the $_POST superglobal or the $_GET superglobal depending on your form's method. If method = post, for example, you'd use $_POST['select_name'].

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

proteus9

1:49 pm on Nov 23, 2008 (gmt 0)

10+ Year Member



I thought maybe I would confuse people with that post.
Let me try to explain what I am wanting to do again.

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.

cameraman

5:08 pm on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you read this forum's charter [webmasterworld.com]? This forum is all about learning and helping each other as opposed to writing the code for you. In other words, the entire forum is a how-to tutorial. If you're fairly new to server side scripting and want to proceed to being a veteran at server side scripting, then learning by example & adapting those examples to your own needs are vital skills to develop.

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.

proteus9

5:44 pm on Nov 23, 2008 (gmt 0)

10+ Year Member


Thank you cameraman for your patience and your guidence. And for the record, I never asked, nor did I expect, anybody to write the script for me. I'm all about the learning as well. The threads, and blogs and such that I've been pointed towards by people here, as well as from elsewhere, never 'seemed to me' to clearly explain the how's and why's of this. All I DID expect was friendly direction towards the answers in a way that 'I' could understand.
Like I've said, I'm new to Server-Side Scripting. I'm still trying to get my head wrapped around it. I'm sorry if simply being pointed to "A Forum" or "A Blog" didn't truely explain to me the how's and why's.

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.

cameraman

6:50 pm on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've never gotten mad at anyone for not learning 'quick enough', only frustrated at myself for not finding the words which enable the person to learn. I do get irritated at people who clearly don't want to learn, so I'm glad that you're not in that category - I apologize for inferring from "the tone" of your second post that you might be.

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?

proteus9

7:04 pm on Nov 23, 2008 (gmt 0)

10+ Year Member



Thank you cameraman, but I believe your snippet shall suffice. I will muddle my way through this one way or another.
Actually, your snippet is found to be quite enlightening and I believe that from it I may be able to gain the knowledge I seek.
Once again, thank you. And have a nice day!

cameraman

7:09 pm on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You too, and if you hit any snags, we're always open...