如何使用连接操作查询添加日期选择器代码?

时间:2015-10-26 13:30:13

标签: javascript php jquery html mysql

添加日期选择器,它将使用日期到日期的记录打印输出。 jointtable.php包含整个代码和jquery代码。 jquery代码返回答案......

jointable.php代码从这里开始;

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Purches Pagination</title>
<!-- Load jQuery from Google's CDN -->
<!-- Load jQuery UI CSS  -->
<link rel="stylesheet"href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<!-- Load jQuery JS -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<!-- Load jQuery UI Main JS  -->
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<!-- Load SCRIPT.JS which will create datepicker for input field  -->
<script src="javascript.js"></script>
<link rel="stylesheet" href="runnable.css" />
</head>
<body>
 <h1> Sells Reports </h1>
 <br />
 <form id="form1" name="form1" method="post" action="">
 <p>From: </label> <input type="text" id="from" /> To: <input type="text"id="to" /></p> 
 <input type="button" id="submit" name="submit" value="submit"/>
 </form>
 <br />
  <?php
  include ('conn.php');
  $per_page=5;
  if (isset($_GET["page"]))
   {
   $page = $_GET["page"];
   }
  else 
   {
   $page=1;
   }
  // Page will start from 0 and Multiple by Per Page
  $start_from = ($page-1) * $per_page;
  //Selecting the data from table but with limit
  $sql = ("SELECT ,customer.Customer_email,customer .Customer_address,customer.Customer_phone_num,final_report.Product_id,final_repo rt.total_units,final_report.total_price,final_report.Date
FROM final_report 
INNER JOIN customer
ON customer.Customer_id = final_report.customer_idLIMIT$start_from,$per_page") or die(mysql_error());
 //$result = $conn->query($sql);
 $result = mysqli_query($conn, $sql) or die("MySQL error:"mysqli_error($conn)."<hr>\nQuery: $sql");
?>
<table align="center" border="2" cellpadding="3">
<tr><th>srn</th><th>customer_id</th><th>Customer_name</th><th>Customer_email</th><th>Customer_address</th><th>Customer_phone_num</th><th>Product_ID</th><th>total_units</th><th>total_price</th><th>Date</th></tr>
<?php
$serial = $start_from  + 1;  // THIS CODE IS USE FOR ASSIGNING SERIAL NUMBER      WHICH IS START FROM NUMBER 1---------------------//
?>
<?php
while($row = mysqli_fetch_assoc($result)) 
    {   
      $s_num = $serial++; // THIS CODE INCREMENTING THE SERIAL NO PER PAGE 
?>
<tr align="center">
<td><?php echo $s_num; ?></td> <!-- THIS CODE PRINT THE SERIAL NO IN SERIAL NUMBER COLUMN -->
<td><?php echo $row["customer_id"]; ?></td>
<td><?php echo $row["Customer_name"]; ?></td>
<td><?php echo $row["Customer_email"]; ?></td>
<td><?php echo $row["Customer_address"]; ?></td>
<td><?php echo $row["Customer_phone_num"]; ?></td>
<td><?php echo $row["Product_id"]; ?></td>
<td><?php echo $row["total_units"]; ?></td>
<td><?php echo $row["total_price"]; ?></td>
<td><?php echo date('d/M/Y - h:i A',strtotime($row['Date'])); ?></td>
</tr>
<?php
};
?>
</table>
<div>
<?php
//Now select all from table
$query = "SELECT `customer_id`,`Product_id`,`total_units`,`total_price`,`Date`FROM`final_report`;
$result = mysqli_query($conn, $query) or die("MySQL error:".mysqli_error($conn)."<hr>\nQuery: $query");
// Count the total records
$total_records = mysqli_num_rows($result);
echo "total records are: ".$total_records."<br><br>";
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

//Going to first page
if('page= 1'){
?>
<?php
$f = 'FirstPage';
echo "<center><a href='jointable1.php?page=1'>".$f."</a> ";
}
else
{
 echo "error";
}
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='jointable1.php?page=".$i."'>".$i."</a> ";
};

// Going to last page
$l = 'LastPage';
echo "<a href='jointable1.php?page=".$total_pages."'>".$l."</a></center> ";
?>
</div>
</body>
</html>

jquery代码;

    $(function() 
    {  
    $( "#from" ).datepicker(
    {  
        defaultDate: "+1d",  
        changeMonth: true,  
        changeYear: true,  
        numberOfMonths: 1,  
        onSelect: function( selectedDate ) 
        {  
            $( "#to" ).datepicker( "option", "minDate", selectedDate ); 
        }  
    }); 
    var currentDate = new Date(); 
    $("#from").datepicker("setDate",currentDate);  

    $("#from").datepicker("option",{  
        dateFormat: 'mm/dd/yy',  
        showOn: "button",  
      // whatever option Or event you want   
    });  


    $( "#to" ).datepicker({  
        defaultDate: "+1d",  
        changeMonth: true,  
        changeYear: true,  
        numberOfMonths: 1,  
        onSelect: function( selectedDate ) {  
            $( "#from" ).datepicker( "option", "maxDate", selectedDate );  
        }  
    });  

        var currentDate = new Date();  
    $("#to").datepicker("setDate",currentDate);  

    $("#to").datepicker("option",{  
        dateFormat: 'mm/dd/yy',  
        showOn: "button",  
      // whatever option Or event you want   
    });  
 });  

1 个答案:

答案 0 :(得分:0)

将MySQL BETWEENstr_to_date

一起使用
$from=$_REQUEST['from'];
$to=$_REQUEST['to'];
where final_report.Date BETWEEN str_to_date('$from','%m/%d/%Y') 
and str_to_date('$to','%m/%d/%Y')