liamloveslearning Posted May 19, 2010 Share Posted May 19, 2010 Hi everyone, I have a list containing all records in a table. Upon inserting these records a the date is also inserted into the record. I need to create a filter where I can pick 2 dates and filter the table showing only those records added within the 2 dates. Ive no idea how or how to start working on this so could anybody provide me ideas? Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/ Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 SELECT something FROM stuff WHERE created_at BETWEEN '2010-02-05' AND '2010-03-13'; Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1060624 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Thanks Daniel, The filter needs to be defined by the user, so the dates wont be static they will be varied, Is this still possible? Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1060980 Share on other sites More sharing options...
Daniel0 Posted May 20, 2010 Share Posted May 20, 2010 Of course... $stmt = $db->prepare('SELECT something FROM stuff WHERE created_at BETWEEN ? AND ?'); $stmt->execute(array($_POST['start'], $_POST['end'])); $stuff = $stmt->fetchAll(); (Assuming a PDO interface and that the values have already been validated) Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1060981 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Could you explain it please Daniel, Im unsure what parts Id need to swop out with my code etc? Sorry to be so naive Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061025 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Is it not possible to variables within' a query such as SELECT * FROM worksorders WHERE worksorders.intradateadd BETWEEN '<?php $POST_['date1'] ?>' AND '<?php $POST_['date2'] ?>' Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061028 Share on other sites More sharing options...
cags Posted May 20, 2010 Share Posted May 20, 2010 As it's unlikely you are using a PDO interface (since you don't have a clue what Daniel is talking about), your code would look something more like... mysql_query("SELECT * FROM worksorders WHERE worksorders.intradateadd BETWEEN '{$_POST['date1']}' AND '{$_POST['date2']}'"); Though as Daniel said you would want to validate/sanitize the $_POST variables first. Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061039 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 haha I did have to google it brilliant, thanks cags Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061041 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Okay I now have my dates worked, I just need to put them into my query, my date variables are <?php $startdate = date('Y-m-d', strtotime ("last Monday")); $enddate = date('Y-m-d', strtotime ("next Friday")); echo $startdate; echo $enddate; ?> and my query is SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 0 ORDER BY {$NXTSort_rsworksorders1}"; Ive tried... SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete, worksorders.intradateadd FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 1 AND worksorders.intradateadd BETWEEN '{$startdate}' AND '{$enddate'); ORDER BY {$NXTSort_rsworksorders1}" To no avail, Can anybody see why? are my variables in the query incorrect? Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061062 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Ive also just tried $query_rsworksorders1 = "SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 1 AND worksorders.intradateadd BETWEEN "$startdate" AND "$enddate" ORDER BY {$NXTSort_rsworksorders1}"; I read variables in queries need double quotes, I tried this, again without success Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061065 Share on other sites More sharing options...
cags Posted May 20, 2010 Share Posted May 20, 2010 Even a basic syntax highlighting software package should point out the various issues with that. Variables will only be evaluated when they are contained within a string that is delimited by double quotes this doesn't mean the variables themselves should be delimited by them. Any value which is string based, which is being passed to MySQL need to be delimited by quotes (single or double, but single makes most sense because the string object in PHP is itself delimited by double quotes, so if you use double quotes you'll have to escape them). When placing variables in strings I find it useful to delimit them with curly braces as it helps to differentiate the difference between the variable name and part of the string eg $var = "like; echo "{$var}able"; echo "$varable"; Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061071 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 So in my query I should have my variables as {'$myvar'} ? Thanks for being so helpful Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061074 Share on other sites More sharing options...
cags Posted May 20, 2010 Share Posted May 20, 2010 No, the value needs to be delimited by quotes and the variable by curly brackets (which are entirely optional in most circumstances). Switch them over. Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061081 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 Sorry cags im new to php and now im completely lost, by the value needs to be delimited do you mean.. <?php $startdate = 'date('Y-m-d', strtotime ("last Monday"));' $enddate = 'date('Y-m-d', strtotime ("next Friday"));' ?> I know this is wrong as the highlighting suggests so but im completely dumbfounded Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061087 Share on other sites More sharing options...
Daniel0 Posted May 20, 2010 Share Posted May 20, 2010 That's funny. Your profile suggests that you started with PHP at least two years ago You seem to be struggling a lot with syntax. I would suggest that you check out the Langauge Reference in the manual before you try to do anything further. Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061090 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 haha i did join then, I didnt touch it for a long time and i started using it again last month. Its just so confusing Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061095 Share on other sites More sharing options...
liamloveslearning Posted May 20, 2010 Author Share Posted May 20, 2010 SORTED! Thanks for being so patient Link to comment https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/#findComment-1061103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.