Jump to content

Dropdown selection query


Steve_NI

Recommended Posts

I have a mysql database that contains account transactions amount, account and Date

I want to give the user a dropdown option where they have a number of options over which time frame they can see the data

i.e they can see transactions over the last 1, 7, 28 days

 

I know the mysql query I need to write to search the database for each option but how do I link the two together?

i.e. the user selects the 7 day option, this runs the mysql query and I can produce a table with that data

or if the user selects the 28 day option this runs a different mysql query and I can reproduce a table with that data.

 

Can anyone give me some pointers

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/
Share on other sites

If $days is the value selected from your dropdown, then your query will be like

$days = intval($_GET['days']);
$query = "SELECT account, amount, date
    FROM transaction
    WHERE date BETWEEN CURDATE() - INTERVAL $days DAY AND CURDATE()";
  On 11/9/2013 at 4:00 PM, Barand said:

 

If $days is the value selected from your dropdown, then your query will be like

$days = intval($_GET['days']);
$query = "SELECT account, amount, date
    FROM transaction
    WHERE date BETWEEN CURDATE() - INTERVAL $days DAY AND CURDATE()";

Thanks Barand

Here is my HTML code

<html>
    <head>
        <title>
            Dropdown
        </title>
    </head>
    <body>
        <form  action="data.php" method="get">
               <select name="days">
                  <option value="One">1</option>
                  <option value="Seven">7</option>
                  <option value="TwentyEight">28</option>
                  <option value="OneHundredEighty">180</option>
            </select>
            <input type="submit" value="Submit">
               
    </form>
    </body>
</html>

But when i put the following into php

$days = $_GET['days'];

I get an undefined index error. Can you see where i have gone wrong?

1. The option values should be 7, 28 and 180 and not the text descriptions

<option value='7'>Seven</option>

2. You need to test if the $_GET data exists (ie has the form been submitted)

$days = isset($_GET['days']) ? intval($_GET['days']) : 7;    // sets seven as the default
  On 11/9/2013 at 6:51 PM, Barand said:

 

1. The option values should be 7, 28 and 180 and not the text descriptions

<option value='7'>Seven</option>

2. You need to test if the $_GET data exists (ie has the form been submitted)

$days = isset($_GET['days']) ? intval($_GET['days']) : 7;    // sets seven as the default

Again thanks Barand

I have changed the option values in the form to make them integers 1,7,28 & 100

<form  action="data.php" method="get">
               <select name="days">
                  <option value="1">One Day</option>
                  <option value="7">Seven Days</option>
                  <option value="28">Twenty Eight days</option>
                  <option value="180">One Hundred and Eighty Days</option>
            </select>
            <input type="submit" value="Submit">
               
    </form>

And in the data.php form I am running the test you suggest

$days = isset($_GET['days']) ? intval($_GET['days']):7;

echo "Number of days = ".$days; 

Each time however it echoes out the days is equal to 0. I cannot understand why it is not getting the value submitted from the dropdown menu. Should days at the very least be 7 if that is what we put the default value to be in the ternary operator?

after you submit the form, does the URL in your browser's address bar contain ?days=n and if so is the n value a zero or the correct value that you selected in the dropdown menu?

 

just the code you have shown should work. there's something going on elsewhere, a .htaccess url rewriting/redirect or in code other than what you have posted that is causing the problem. is that the full php processing code? do you have a session variable or a cookie named 'days'?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.