Jump to content

search by two given dates


I-AM-OBODO

Recommended Posts

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);
}

}			

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

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.