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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,936
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    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
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    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; 08-29-2011 at 12:35 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    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; 08-29-2011 at 02:11 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,936
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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=
    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.

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    i thought it would bring me to another page.. i dunno im really confused.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    It will take you to whatever page you have specified in the form's action attribute.

  • #8
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    but... then why is the SELECT on that page? *confused*

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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; 08-29-2011 at 03:09 AM.

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    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; 08-29-2011 at 03:17 AM.

  • #11
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.

  • #12
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    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.

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote 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; 08-29-2011 at 04:11 AM.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,936
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    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.

  • #15
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    didnt work... just refreshed the page.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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