Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2011, 11:51 PM   PM User | #1
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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
markman641 is offline   Reply With Quote
Old 08-29-2011, 12:04 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-29-2011, 12:25 AM   PM User | #3
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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..
markman641 is offline   Reply With Quote
Old 08-29-2011, 02:02 AM   PM User | #4
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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..
markman641 is offline   Reply With Quote
Old 08-29-2011, 02:30 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-29-2011, 02:50 AM   PM User | #6
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
i thought it would bring me to another page.. i dunno im really confused.
markman641 is offline   Reply With Quote
Old 08-29-2011, 02:55 AM   PM User | #7
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
It will take you to whatever page you have specified in the form's action attribute.
webdev1958 is offline   Reply With Quote
Old 08-29-2011, 02:57 AM   PM User | #8
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
but... then why is the SELECT on that page? *confused*
markman641 is offline   Reply With Quote
Old 08-29-2011, 03:06 AM   PM User | #9
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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..
webdev1958 is offline   Reply With Quote
Old 08-29-2011, 03:11 AM   PM User | #10
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
Quote:
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..
markman641 is offline   Reply With Quote
Old 08-29-2011, 03:20 AM   PM User | #11
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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.
webdev1958 is offline   Reply With Quote
Old 08-29-2011, 03:43 AM   PM User | #12
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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.
markman641 is offline   Reply With Quote
Old 08-29-2011, 04:00 AM   PM User | #13
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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..
webdev1958 is offline   Reply With Quote
Old 08-29-2011, 04:11 AM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-29-2011, 04:12 AM   PM User | #15
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
didnt work... just refreshed the page.
markman641 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 AM.


Advertisement
Log in to turn off these ads.