Web Analytics Made Easy -
StatCounter sort by dropdown? - CodingForum

Announcement

Collapse
No announcement yet.

sort by dropdown?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sort by dropdown?

    how would i get it so i can display a dropdown, and the user can select a value in the dropdown and clicks a button, and after that it displays only values in the database that match what the user selected? Something like this..

    Code:
    Sort By: <select name="category">
      <option value="$1ClickFamily">1Click Family</option>
      <option value="$1stPage/EmailSubmits">1st Page/Email Submits</option>
      <option value="$AbsoluteRewards">Absolute Rewards</option>
      <option value="$BlazingRewards">Blazing/Nextday/WayRewards</option>
      <option value="$DailySurveys">Daily Surveys</option>
      <option value="$Downloads">Downloads</option>
      <option value="$Eversave">Eversave</option>
      <option value="$HealthOffers">Health Offers</option>
      <option value="$InsuranceQuotes">Insurance Quotes</option>
      <option value="$QuizJungles">Quiz Jungles</option>
      <option value="$Registration">Registration</option>
      <option value="$Downloads">Downloads</option>
      <option value="$SurveyPanels">Survey Panels</option>
      <option value="$ThinkFast">Think Fast</option>
      <option value="$WinningSurveys">Winning Surveys</option>
      <option value="$ZZZQuiz">ZZZ Quiz</option>
    </select> 
    
    $get_name = mysql_query("SELECT * from `offers` WHERE `category` = '$selectedvalue' & ORDER BY id DESC");
    I KNOW thats completely wrong i just didnt know how to explain it :P
    Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

  • #2
    It's not really wrong. Just missing the important step:
    Code:
    $selectedvalue = $_GET["category"];
    or
    $selectedvalue = $_POST["category"];
    depending on whether your <form> is coded as method="get" or method="post"

    Though I don't understand the point of the $ characters in front of all the category values, unless you really do have $ characters in your database in that category field.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      yeah, but what would i make the <form> action?

      wait, so on the page i want the dropdown thing, what would i put?

      and would i make another file that the form uses? XD
      Last edited by markman641; Aug 28, 2011, 08:35 PM.
      Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

      Comment


      • #4
        this is what i got so far, its not working, its searching for "category"

        Code:
        <P ALIGN="RIGHT"><select name="category">
          <option value="1Click Family">1Click Family</option>
          <option value="1st Page/Email Submits">1st Page/Email Submits</option>
          <option value="Absolute Rewards">Absolute Rewards</option>
          <option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
          <option value="Daily Surveys">Daily Surveys</option>
          <option value="Downloads">Downloads</option>
          <option value="Eversave">Eversave</option>
          <option value="Health Offers">Health Offers</option>
          <option value="Insurance Quotes">Insurance Quotes</option>
          <option value="Quiz Jungles">Quiz Jungles</option>
          <option value="Registration">Registration</option>
          <option value="Downloads">Downloads</option>
          <option value="Survey Panels">Survey Panels</option>
          <option value="Think Fast">Think Fast</option>
          <option value="Winning Surveys">Winning Surveys</option>
          <option value="ZZZ Quiz">ZZZ Quiz</option>
        </select>
                 <? 
        $selectedvalue = $_GET["category"];
        ?>
        <form name="form" action="searchcategory.php" method="get">
          <input name = "q" type="submit" name="Submit" value="Category" />
        </form></P>
        Last edited by markman641; Aug 28, 2011, 10:11 PM.
        Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

        Comment


        • #5
          The <select> *MUST* be between the <form> and the </form> !!!

          Also, you need to provide for the case when the page first appears and no category has yet been chosen.

          Code:
          <form ...>
          <select ...>
          </select>
          <input type="submit" ...>
          </form>
          <?php
          $selectedvalue = $_GET["category"];
          if ( isset($selectedvalue) )
          {
              // ** NO AMPERSAND IN THE SQL!!! **
              $sql = "SELECT * from `offers` WHERE `category` = '$selectedvalue' ORDER BY id DESC";
          
              $get_name = mysql_query($sql) ...
              ... dump out results ...
          
          }
          ?>
          Or, if you want to show *all* categories when no <option> is yet chosen, maybe
          Code:
          <form ...>
          <select ...>
          </select>
          <input type="submit" ...>
          </form>
          <?php
          $selectedvalue = $_GET["category"];
          if ( isset($selectedvalue) )
          {
              $sql = "SELECT * from offers WHERE category = '$selectedvalue' ORDER BY id DESC";
          } else {
              $sql = "SELECT * from offers ORDER BY id DESC";
          }
          $get_name = mysql_query($sql) ...
          ... dump out results ...
          
          ?>
          Up to you.

          But in any case, no <form> fields are ever posted back to server if they don't appear in the HTML between <form> and </form>. Or if they don't have a name=
          Be yourself. No one else is as qualified.

          Comment


          • #6
            i thought it would bring me to another page.. i dunno im really confused.
            Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

            Comment


            • #7
              It will take you to whatever page you have specified in the form's action attribute.

              Comment


              • #8
                but... then why is the SELECT on that page? *confused*
                Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

                Comment


                • #9
                  It depends on what you want to do.

                  Consider this scenario which is what I am assuming you have.

                  1 - You have a php page containing a <form> with a <select> inside the form.

                  2 - the user makes a selection from the <select> and clicks a submit button

                  3 - the form then sends the user's selection to the php script specified in the form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.

                  4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.

                  So the first thing you need to decide is whether the form data processing script in 3 is to be the same script containing the original <form> or a separate script all together.
                  Last edited by webdev1958; Aug 28, 2011, 11:09 PM.

                  Comment


                  • #10
                    1 - You have a php page containing a <form> with a <select> inside the form.

                    2 - the user makes a selection from the <select> and clicks a submit button

                    3 - the form then sends the user's selection to the php script specified inthe form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.

                    4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.
                    that is exactly right. i have a search script called categorysearch.php, but when it tries to search it searches for the term "Category" instead of the users selected value.
                    Last edited by markman641; Aug 28, 2011, 11:17 PM.
                    Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

                    Comment


                    • #11
                      You'll have to post the actual query being run to debug it, but in theory if the user selects the Absolute Rewards optin then your query should evaluate to

                      Code:
                      SELECT * from `offers` WHERE `category` = 'Absolute Rewards' & ORDER BY id DESC
                      To help debug your code, do the following

                      PHP Code:
                      $query "SELECT * from `offers` WHERE `category` = '$selectedvalue'  ORDER BY id DESC";

                      echo 
                      $query//this will display the actual query about to be run

                      die(); //stop further execution 
                      and post the output from echo statement.

                      btw - I removed the ampersand that was in your original code. I don't know why you had it there.

                      Comment


                      • #12
                        Okay, i give up this is more complicated then i thought. I'm just going to make a new page for each category then just have the dropdown link to it.

                        It sounds so simple. select something from dropdown then display offers that match the category on a new page.
                        Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

                        Comment


                        • #13
                          Originally posted by markman641 View Post
                          Okay, i give up this is more complicated then i thought.
                          It doesn't have to be.

                          If you put this code in a file called searchCategory.php and insert your code to connect to the db where I have commented to do so, it should work when you substitute your own table field names for col1, col2 etc
                          PHP Code:
                          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                          <html xmlns="http://www.w3.org/1999/xhtml">
                              <head>
                                  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
                                  <title></title>
                                  <style type="text/css"></style>
                              </head>
                              <body>
                                  <form action="searchCategory.php" method="post">
                                      <select name="category">
                                          <option value="1Click Family">1Click Family</option>
                                          <option value="1st Page/Email Submits">1st Page/Email Submits</option>
                                          <option value="Absolute Rewards">Absolute Rewards</option>
                                          <option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
                                          <option value="Daily Surveys">Daily Surveys</option>
                                          <option value="Downloads">Downloads</option>
                                          <option value="Eversave">Eversave</option>
                                          <option value="Health Offers">Health Offers</option>
                                          <option value="Insurance Quotes">Insurance Quotes</option>
                                          <option value="Quiz Jungles">Quiz Jungles</option>
                                          <option value="Registration">Registration</option>
                                          <option value="Downloads">Downloads</option>
                                          <option value="Survey Panels">Survey Panels</option>
                                          <option value="Think Fast">Think Fast</option>
                                          <option value="Winning Surveys">Winning Surveys</option>
                                          <option value="ZZZ Quiz">ZZZ Quiz</option>
                                      </select>
                                      <input type="submit" value="Submit" />
                                  </form>
                                  <div id="results">
                                      <?php
                                      
                          if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') { //check if submit button was clicked
                                          //insert your code to connect to your database here
                                          
                                          
                          $selectedVal $_POST['category'];
                                          
                          $query 'SELECT * from offers WHERE category = "' $selectedvalue '"  ORDER BY id DESC';
                                          
                          $rs mysql_query($query$conn);
                                          while (
                          $row mysql_fetch_assoc($rs)) {
                                              echo 
                          $row['col1'] . ' ' $row['col2'] . '<br />'//col1, col2 are the columns in category table
                                          
                          }
                                          
                          mysql_free_result($rs);
                                          
                          mysql_close($conn);
                                      }
                                      
                          ?>
                                  </div>
                              </body>
                          </html>
                          I haven't bothered to include any data validation/sanitisation at this stage - rather stay with the KISS principle for now.
                          Last edited by webdev1958; Aug 29, 2011, 12:11 AM.

                          Comment


                          • #14
                            See, from your post #4 I just *assumed* that the page with the <select> and the page that *used* the selection were/are one and the same.

                            YOU are the one who put the mysql_query line of code into the same page of code as the <select>. If you want them to be different pages, then need that mysql_query to be on the page that is specified in the action= of the <form>.

                            Read again what webdev said in his post #9. You *CAN* do it either way. Honest. It's 100% your choice. Webdev's code in post #13 is clean and clear and a way to do it all in one page. If you really want two pages, though, just ask. It's a truly minor mod to that code.
                            Be yourself. No one else is as qualified.

                            Comment


                            • #15
                              didnt work... just refreshed the page.
                              Ever want to see inside of a rave? Check out RedRoll and join our community of EDM lovers and rave fanatics.

                              Comment

                              Working...
                              X