Steve_NI Posted November 9, 2013 Share Posted November 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/ Share on other sites More sharing options...
Barand Posted November 9, 2013 Share Posted November 9, 2013 (edited) 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()"; Edited November 9, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457656 Share on other sites More sharing options...
Steve_NI Posted November 9, 2013 Author Share Posted November 9, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457662 Share on other sites More sharing options...
Barand Posted November 9, 2013 Share Posted November 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457670 Share on other sites More sharing options...
Steve_NI Posted November 10, 2013 Author Share Posted November 10, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457751 Share on other sites More sharing options...
mac_gyver Posted November 10, 2013 Share Posted November 10, 2013 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'? Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457752 Share on other sites More sharing options...
Solution Steve_NI Posted November 10, 2013 Author Solution Share Posted November 10, 2013 Ignore my last post I was being a bit of a tube. Sorted now, many thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/283750-dropdown-selection-query/#findComment-1457753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.