I-AM-OBODO Posted January 30, 2013 Share Posted January 30, 2013 Hi all, i am try to get the result of all transaction made by a user between a given period of date i.e from 2013-01-10 to 2013-01-30 but don't know how to get it done. I only get the result of just a date even when there are other dates and transaction by the user. thanks all this is what i did: <?php if(isset($_POST['search'])){ $s = $_POST['start']; $end = $_POST['end']; if($s == ''){ echo "invalid Entry"; } else{ $s = date('Y-m-d', strtotime(str_replace('-','/', $s))); $end = date('Y-m-d', strtotime(str_replace('-','/', $end))); $r = mysql_query("SELECT * FROM transaction WHERE username = '$_SESSION[username]' AND date LIKE '$s' AND $end ORDER BY date DESC") or die(mysql_error()); $num_rows = mysql_num_rows($r); print "There are $num_rows records.<P>"; echo "<center><table border='1' bordercolor='#000000' cellpadding='1' cellspacing='1'> <tr> <th>Trans ID</th> <th>Trans Ref</th> <th>Sender Acct</th> <th>Receiver Acct</th> <th>Trans Status</th> <th>Date</th> </tr>"; while ($get_info = mysql_fetch_row($r)){ print "<tr>\n"; foreach ($get_info as $field) print "\t<td><font face=arial size=2/>$field</font></td>\n"; print "</tr>\n"; } print "</table>\n"; print "<br>"; mysql_close($link); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/ Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 MySQL has the self-explanatory BETWEEN...AND operator. `date` BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD' Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409105 Share on other sites More sharing options...
Love2c0de Posted January 30, 2013 Share Posted January 30, 2013 You didn't open any curly braces for your foreach loop. I'm not sure if that was the problem but I added them in. Give it a try: if(isset($_POST['search'])){ $s = $_POST['start']; $end = $_POST['end']; if($s == ''){ echo "invalid Entry"; } else{ $s = date('Y-m-d', strtotime(str_replace('-','/', $s))); $end = date('Y-m-d', strtotime(str_replace('-','/', $end))); $r = mysql_query("SELECT * FROM transaction WHERE username = '$_SESSION[username]' AND date LIKE '$s' AND $end ORDER BY date DESC") or die(mysql_error()); $num_rows = mysql_num_rows($r); print "There are $num_rows records.<P>"; echo "<center><table border='1' bordercolor='#000000' cellpadding='1' cellspacing='1'> <tr> <th>Trans ID</th> <th>Trans Ref</th> <th>Sender Acct</th> <th>Receiver Acct</th> <th>Trans Status</th> <th>Date</th> </tr>"; while ($get_info = mysql_fetch_row($r)){ print "<tr>\n"; foreach ($get_info as $field) { print "\t<td><font face=arial size=2/>$field</font></td>\n"; print "</tr>\n"; } } print "</table>\n"; print "<br>"; mysql_close($link); } } Hope this helps you in the right direction. Regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409106 Share on other sites More sharing options...
PFMaBiSmAd Posted January 30, 2013 Share Posted January 30, 2013 @Love2c0de, The foreach() loop's purpose was to run the single print statement for all the <td>$field</td> values, followed by printing one </tr> tag. For a single statement, you don't need the {} and the code you posted would print the </tr> tag after every </td>$field</td> value. Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409113 Share on other sites More sharing options...
Love2c0de Posted January 30, 2013 Share Posted January 30, 2013 Ah my bad. Thank you very much for clearing that up. I thought the foreach was meant to loop inside the while loop. I thought he needed the curly braces because I thought he was trying to execute more than one line of code also. Thanks again. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409196 Share on other sites More sharing options...
I-AM-OBODO Posted February 1, 2013 Author Share Posted February 1, 2013 @all thanks but still not working. I tried the BETWEEN statements and did a "where date <= $s and >= $end. all to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409461 Share on other sites More sharing options...
Barand Posted February 1, 2013 Share Posted February 1, 2013 "where date <= $s and >= $end. all to no avail. either where date >= '$s' and date <= '$end' or where date BETWEEN '$s' AND '$end' Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409469 Share on other sites More sharing options...
I-AM-OBODO Posted February 2, 2013 Author Share Posted February 2, 2013 Thank you all. Finally I got it working. I tried both the 'BETWEEN' and '>= <='. The problem was that I omitted the apostrophe sign on the end date. '$end'. Thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/273818-search-by-two-given-dates/#findComment-1409661 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.