duanguoyin7008 2018-08-17 15:26
浏览 129
已采纳

我想使用PHP和MySQL对我选择的数据进行过滤

I need to display data in an HTML table, fetching from the database.

I have to select two tables with INNER JOIN, table1 and table2.

This is my query:

$sql="SELECT * FROM `cdr` INNER JOIN `clients`
ON `cdr`.`dst`=`clients`.`mobile`
WHERE
DATE_FORMAT(calldate,'%m/%d/%Y')='$calldate' OR 
src='$src'
dst='$dst' OR
disposition='$disposition' OR
bitrix_id='$btx_id' OR 
lead_name='$lead_name' OR
agent='$agent'
ORDER BY DATE_FORMAT(calldate,'%m/%d/%Y') DESC";

For example, I want to make a filter like this: to select all dispositon=answered on date=8/16/2018 ,dst=xxxxxx.

This is also all my PHP code:

if(isset($_POST['filter'])){
//Filter by html form
$date=$_POST['calldate'];
$calldate=date("m/d/Y", strtotime($date));
$src=$_POST['src'];
$dst=$_POST['dst'];
$disposition=$_POST['disposition'];   
$r=1;
$total_duration=0;
$btx_id=$_POST['btx_id'];
$lead_name=$_POST['lname'];
$agent=$_POST['agent'];
// test
$sql1="SELECT * FROM `cdr` INNER JOIN `clients`
ON `cdr`.`dst`=`clients`.`mobile`
WHERE 
DATE_FORMAT(calldate,'%m/%d/%Y')='$calldate' OR 
src='$src' OR
dst='$dst' OR
disposition='$disposition' OR
bitrix_id='$btx_id' OR
lead_name='$lead_name' OR
agent='$agent'
ORDER BY DATE_FORMAT(calldate,'%m/%d/%Y') DESC";
$query=mysqli_query($con,$sql1) or die(mysqli_error());
while($row1=mysqli_fetch_array($query,MYSQLI_ASSOC)){
echo " 
<tr>
<td>".$r."</td>
<td>".$row1['bitrix_id']."</td>
<td>".$row1['lead_name']."</td>
<td>".$row1['agent']."</td>
<td>".$row1['calldate']."</td>
<td>".$row1['src']."</td>
<td>".$row1['dst']."</td>
<td>".$row1['disposition']."</td>
<td>".$row1['duration']."</td>
</tr>
 ";
 $r++;
 $total_duration=$total_duration+$row1['duration'];
 }
$durationmin=$total_duration/60;    
echo "
<tr>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>".$total_duration."(".round($durationmin,1)."min)</th>
</tr> 
   ";
}
  • 写回答

1条回答 默认 最新

  • doumang20060820 2018-08-21 17:15
    关注

    After many attempts to solve this problem , I found this solution thanks guys for your suggestions. Here is the PHP code:

    <pre>
    <?php
    /********************************
    -Comments...
    -Show Alldata
    *********************************************/
    if(isset($_POST['filter'])){
    //Filter by html form
    $date=$_POST['calldate'];
    $calldate=date("m/d/Y", strtotime($date));
    $date2=$_POST['calldate2'];
    $calldate2=date("m/d/Y", strtotime($date2));
    $src=$_POST['src'];
    $dst=$_POST['dst'];
    $disposition=$_POST['disposition'];   
    $r=1;
    $client=0;
    $agent=0;
    $durationmin=0;
    $total_duration=0;
    $btx_id=$_POST['btx_id'];
    $lead_name=$_POST['lname'];
    $agent=$_POST['agent'];
    $last_row=" ";
    // test
    $sql1 ="SELECT * FROM cdr INNER JOIN clients ON cdr.`dst`=`clients`.`mobile`";
    
    $at_least_one = false;
    $first_date=false;
    
    if($date){
        $sql1.="where DATE_FORMAT(calldate,'%m/%d/%Y')>='$calldate'";
        $first_date=false;
        $at_least_one = true;
        //if we want all calls of any agent in given time
     }
    if ($src) {
        if($at_least_one === false){
        $sql1.=" WHERE src='$src'";
      }
      else{
    $sql1.=" AND src='$src'";
    }
    $at_least_one = true;
    }
    if ($dst) {
        if($at_least_one === false){
        $sql1.=" WHERE dst LIKE '$dst%'";
        }else{
        $sql1.=" AND dst LIKE '$dst%'";
    }
      $at_least_one = true;
    }
    if ($disposition) {
    if($at_least_one === false){
        $sql1.=" WHERE disposition='$disposition' ";
    }else{
        $sql1.=" AND disposition='$disposition' ";
    }
      $at_least_one = true;
    }
    if ($btx_id) {
    if($at_least_one === false){
        $sql1.=" WHERE bitrix_id='$btx_id'";
    }else{
        $sql1.=" AND bitrix_id='$btx_id'";
    }
      $at_least_one = true;
    }
    
    if ($lead_name) {
    if($at_least_one === false){
        $sql1.=" WHERE lead_name LIKE '%$lead_name%'";
    }else{
        $sql1.=" AND lead_name LIKE '%$lead_name%'";
    }
      $at_least_one = true;
    
    }
    
    if ($agent) {
      if($at_least_one === false){
      $sql1.=" WHERE agent LIKE '%$agent%' ";
    } else{
      $sql1.=" AND agent LIKE '%$agent%'";
    }
    }
    if ($date2) {
     if($at_least_one === false){
      $sql1.="AND DATE_FORMAT(calldate,'%m/%d/%Y')<='$calldate2'";
    } else{
      $sql1.=" AND DATE_FORMAT(calldate,'%m/%d/%Y') BETWEEN '$calldate' AND '$calldate2'";
    }
    }
    
    $sql1.=" ORDER BY DATE_FORMAT(calldate,'%m/%d/%Y') DESC";
    
    echo "<div style='color:red;font-size:16px;'>".$sql1."</div>";
    $query=mysqli_query($con,$sql1) or die(mysqli_error($con));
    while($row1=mysqli_fetch_array($query,MYSQLI_ASSOC)){
         echo " 
                <tr>
                     <td>".$r."</td>
                     <td>".$row1['bitrix_id']."</td>
                     <td>".$row1['lead_name']."</td>
                     <td>".$row1['agent']."</td>
                     <td>".$row1['calldate']."</td>
                     <td>".$row1['calldate']."</td>
                     <td>".$row1['src']."</td>
                     <td>".$row1['dst']."</td>
                     <td>".$row1['disposition']."</td>
                     <td>".$row1['duration']."</td>
                </tr>
               ";
               $r++;
               $total_duration=$total_duration + $row1['duration'];
    
      }
    $durationmin=round($total_duration/60,1);   
    echo "<tr>
          <th>&nbsp;</th>
             <th>&nbsp;</th>
             <th>(".$r.")Times Called This Client</th>
             <th>(".$r.")Total Calls From This Agent</th>
             <th>(".$r.")Total Calls on This Date</th>
             <th>&nbsp;</th>
             <th>&nbsp;</th>
             <th>(".$r.")Total Calls To This Number</th>
             <th>&nbsp;</th>
             <th>".$total_duration."(".round($durationmin,1)."min)</th>
          </tr> ";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler