Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Need help making a drop dawn menu for sorting rows...

    I am fairly new to MySQL and don't much about it.

    What I am trying to do is make a code that will sort the rows based on what is in the drop down menu. For example:

    I have 3 columns in my database (generation, name, and gender), what I would like to do is have 3 drop down boxes and have them work like this.
    The first one will have those 3 column names in there, and will be sorted first.
    The second one will also have those 3 column names, and will be sorted second (so that if any data sorted by the first drop down box is the same, then those will be sorted by this drop down).
    And the third one will have ASC and DESC in it.
    (Then would come the submit button, I think I can manage that )

    Now I know how to connect a link to sort the MySQL rows, but I find that after coding it that isn't what I want. I have looked for tutorials on how to do this, but can't seem to find anything. It took me a while just to find a good enough tutorial on how to sort useing links...

    If anyone could help me, I would greatly appreciate it! I can't seem to find out how to...
    Last edited by Mr Travis L; 04-01-2012 at 07:01 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    And what happens if the user selects "name" in both of the first two dropdowns?

    And how would the user specify "ORDER BY generation DESC, name ASC"? If you only have a single dropdown for ASC/DESC?

    Wouldn't it make more sense to have
    By generation ASC
    By generation DESC
    By name ASC
    By name DESC
    By gender ASC
    By gender DESC

    in each of only two drop downs?

    You can't solve the problem of the user selecting the same field twice other than by using JavaScript (you could just ignore the second chioice if it same as the first). But the conflicting ASC/DESC is real.

    Anyway, the basic answer is trivial.
    Code:
    <select name="order1">
    <option>Generation</option>
    <option>Name</option>
    <option>Gender</option>
    </select>
    <select name="order2">
    <option>Generation</option>
    <option>Name</option>
    <option>Gender</option>
    </select>
    <select name="order3">
    <option value=" ASC">A to Z</option>
    <option value="DESC">Z to A</option>
    </option>
    ...
    and then
    Code:
    <?php 
    $sql = "SELECT * FROM yourtable ORDER BY " 
         . $_POST["order1"] . $_POST["order3"] . ","
         . $_POST["order2"] . $_POST["order3"];
    $result = mysql_query( $sql ) or die( mysql_error() );
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I have been thinking of both those problems. I think it would be better to just have 2 drop down menu's like you said.
    That solves 1 problem, now onto the other.
    Is there somehow to make an error message so that if they pick 2 of the same column name, then it will just not sort? They will have to both be differnt (in name, so that if someone picks name-asc for one and name-desc for two, it will still give the error message).

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    I think it would be easier to simply ignore the second selection if both are the same field.

    Of course you could give an error message, though. This is programming, you know. Programs can and do give error messages all the time. Have you never used an if in programming?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Yes... Unfortionatly I have kept busy for a few and havn't had time for coding in a long time. Now that I am getting back into it I have forgot some of the basic stuff...

    I spent a few hours trying to get a code to work a few days ago, and after finaly posting it on here I found out I was just missing { and }... It was an IF statement to :P

    I have axualy never used an IF statement for stoping a code and giving an error message... The error message I assume would be easy but I have no idea how to stop the code with it.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    In PHP, you can do:

    Code:
    if ( ... )
    {
        echo "Disatrous problem.  Page quitting now!";
        exit();
    }
    But most of the time you don't really want to exit from the page. You just want to give an error message and then recover best you can.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I have been working with the first code you gave me, and it's not working.
    I have done everything and it gives me an error. What I think is happening is, the php code you gave me above doesn't tell what to do if there is nothing selected...

    This is my code so far:
    PHP Code:
        <select name="boxlist1">
          <option>Generation</option>
          <option>Number</option>
          <option>Name</option>
          <option>From</option>
          <option>Stage</option>
          <option>Gender</option>
          <option>Height</option>
          <option>Weight</option>
          <option>Type 1</option>
          <option>Type 2</option>
          <option>Species</option>
        </select>
        <select name="boxsort1">
          <option>Ascending</option>
          <option>Descending</option>
        </select>
        <select name="boxlist2">
          <option>Generation</option>
          <option>Number</option>
          <option>Name</option>
          <option>From</option>
          <option>Stage</option>
          <option>Gender</option>
          <option>Height</option>
          <option>Weight</option>
          <option>Type 1</option>
          <option>Type 2</option>
          <option>Species</option>
        </select>
        <select name="boxsort2">
          <option>Ascending</option>
          <option>Descending</option>
        </select>

        <?php
          $connect 
    mysql_connect("localhost""*****""*****");

          
    mysql_select_db("pokedex_database"$connect);

          
    $table mysql_query("select * from pokedex");

          if (
    $_POST["boxlist1"]==$_POST["boxlist2"])
            {
              
    $table mysql_query("select * from pokedex);
            }
          else
            {
              $table = mysql_query("
    select from pokedex order by ".$_POST["boxlist1"]." ".$_POST["boxsort1"]."".$_POST["boxlist2"]." ".$_POST["boxsort2"]);
            }
        ?>
    I am not sure if a php if/isset statement would work on this...
    Last edited by Mr Travis L; 04-01-2012 at 02:18 AM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    It is impoossible for there to be nothing selected, given the way you created those <select>s in HTML.

    If the user makes no selection at all, then the PHP code will see "Generation","Ascending","Generation","Ascending".

    But...

    But if that is your actual code, then I don't think you understand the basics of PHP.

    You can't put the PHP code on the same page as the HTML <form> unless you also have some way of detecting that the form has already been submitted. I think you need to go back and study the fundamentals of client/server programming with PHP.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Old Pedant View Post
    You can't put the PHP code on the same page as the HTML <form> unless you also have some way of detecting that the form has already been submitted.
    You can check if a form has been submitted by giving the submit button a name and then checking for its existance and value in the $_POST or $_GET array depending on the form's method value.

  • #10
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thank you Mishu. That how I used to do it! Looking back in my old posts I seen that I did something like that here.
    I have used HTML forms with PHP in the same file for multiple things, and they all work perfectly fine for me. I don't think you understand how flexible PHP really is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    PHP is no more flexible than ASP or ASP.NET or JSP. And yes, I put form code in with form processing code all the time. (In fact, with ASP.NET, the *default* is same-page postback and you have to work harder to do otherwise.)

    But because you left out the check for a postback, I assumed you didn't understand why you are having problems.

    If you put in the code to check for a postback and *still* have the problem, then we can pursue the topic further.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Mr Travis L View Post
    Thank you Mishu. That how I used to do it!
    Happy to have helped

  • #13
    New Coder
    Join Date
    May 2010
    Location
    Michigan, USA
    Posts
    56
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I seem to have got it working good now, thank you both for the help. Really appreciate it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •