Jump to content

Dropdown selection query


Steve_NI
Go to solution Solved by 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
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()";

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?

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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'?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.