0

hi i am working on project that has a filtering option for the admin to sort his data according to the days and i will have to display the data according to the select value here is the code

  <select id="duration" class="main-admin selectpicker">

                    <option value="today">Today</option>
                    <option value="3d">3 Days Ago</option>
                    <option value="1w">1 Week/7 Days Ago</option>
                    <option value="3w">3 Weeks Ago</option>
                    <option value="1m">1 Month Ago</option>
                    <option value="all">Show All 1 Month And Older</option>
                </select>

this is the select box of which admin is selecting the different days duration

$('#duration').on('change', function() {

$("#appendclientdata").html("");
if(this.value=="3d"){
value=3;
}
    else if(this.value=="1w"){
    value=7;
    }
    else  if(this.value=="3w"){
    value=21;
    }
else if(this.value=="1m"){
value=30;
}




if(this.value=="today"){
    //geting current date in javascript
    var dt = new Date();
    var date=(dt.getMonth() + 1)+ "/" + dt.getDate()  + "/" + dt.getFullYear();
//geting current date in javascript


 $.ajax({
     type: 'post',
     url: 'duration_client_data_get.php',
     dataType: 'json',
     data:'date='+date,
     success: function (data) {
     for(i=0;i<data.length;i++){

        var append='<div class="panel panel-default"><div class="panel-heading"><span class="admin-white-box">CLIENT ID:</span><span class="admin-white-box">&nbsp;&nbsp;&nbsp;'+data[i].client_id+'</span><span style="float: right;" class="admin-white-box">'+data[i].time+'</span></div><div class="panel-body"> <div class="col-lg-2"><span class="classfloat client-information-default"><p>Full Name:</p><p>Email:</p><p>Phone:</p><p>City:</p></span></div><div class="col-lg-1"><span class="client-information-detail"><p>'+data[i].fullname+'</p><p>'+data[i].email+'</p><p>'+data[i].phone+'</p><p>'+data[i].city+'</p></span></div> <div class="col-lg-9"> <button onclick=window.location.href="admin_leadbox2.php?id='+data[i].client_id+'" class="admin_view_more_details_button">VIEW MORE DETAILS</button></div> <div  class="col-lg-9">  <button href="#" class="admin_request_quote">SETTINGS</button></div></div>';
        $("#appendclientdata").append(append);

       }
     }
   });
}
else
{

var dt = new Date();
var today=(dt.getMonth() + 1)+ "/" + dt.getDate()  + "/" + dt.getFullYear();
dt.setDate(dt.getDate() - value);
var daysback=(dt.getMonth() + 1)+ "/" + dt.getDate()  + "/" + dt.getFullYear();

 var obj={currentdate:today,daysback:daysback};
 $.ajax({
     type: 'post',
     url: 'duration_client_data_get.php',
     dataType: 'json',
     data:obj,
     success: function (data) {
     for(i=0;i<data.length;i++){
        var append='<div class="panel panel-default"><div class="panel-heading"><span class="admin-white-box">CLIENT ID:</span><span class="admin-white-box">&nbsp;&nbsp;&nbsp;'+data[i].client_id+'</span><span style="float: right;" class="admin-white-box">'+data[i].time+'</span></div><div class="panel-body"> <div class="col-lg-2"><span class="classfloat client-information-default"><p>Full Name:</p><p>Email:</p><p>Phone:</p><p>City:</p></span></div><div class="col-lg-1"><span class="client-information-detail"><p>'+data[i].fullname+'</p><p>'+data[i].email+'</p><p>'+data[i].phone+'</p><p>'+data[i].city+'</p></span></div> <div class="col-lg-9"> <button onclick=window.location.href="admin_leadbox2.php?id='+data[i].client_id+'" class="admin_view_more_details_button">VIEW MORE DETAILS</button></div> <div  class="col-lg-9">  <button href="#" class="admin_request_quote">SETTINGS</button></div></div>';
        $("#appendclientdata").append(append);

       }
     }
   });
}

});

this is the ajax request sent to server

<?php
include("db.php");
     if(isset($_POST['date'])){
     $date = $_POST['date'];
        //echo "inside date.<br>";
     $i=0;
       $query=mysqli_query($db,"SELECT * FROM `atom_users` WHERE dateforlogic='$date'");

     while($row=mysqli_fetch_array($query))
     {

         $data2[$i]['client_id']=$row['client_id'];
         $data2[$i]['fullname']=$row['fullname'];
         $data2[$i]['email']=$row['email'];
         $data2[$i]['phone']=$row['phone'];
         $data2[$i]['city']=$row['city'];
         $data2[$i]['time']=$row['time'];
         $i++;
     }
     }
     if (isset($_POST['currentdate'])){
    //echo "inside other.<br>";
     $contractDateEnd= $_POST['currentdate'];
     $contractDateBegin=$_POST['daysback'];
    //echo "date begin request"."<br>".$contractDateBegin."<br>";
    //echo "date end request"."<br>".$contractDateEnd."<br>";
//
    $i=0;
      $query=mysqli_query($db,"SELECT * FROM `atom_users`");

          while($row=mysqli_fetch_array($query))
          {

            //  echo "server date".$row['dateforlogic']."<br>";
              //  echo $row['dateforlogic'].">" . $contractDateBegin."&&". $row['dateforlogic'] ."<". $contractDateEnd."<br>";
             if (($row['dateforlogic'] > $contractDateBegin) && ($row['dateforlogic'] < $contractDateEnd))
             {
                // echo "inside true"."<br>";
                     $data2[$i]['client_id']=$row['client_id'];
                     $data2[$i]['fullname']=$row['fullname'];
                     $data2[$i]['email']=$row['email'];
                     $data2[$i]['phone']=$row['phone'];
                     $data2[$i]['city']=$row['city'];
                     $data2[$i]['time']=$row['time'];
                     $i++;
             }
      }
     }
   echo json_encode($data2);
   exit();
?>

this is the php code for handling the request
this code is working perfectly for every scenario and returning the desired data but the issue is when admin select 1 week/7 days ago option this code does not work the error is

Notice: Undefined variable: data2 in C:\xampp\htdocs\atom\duration_client_data_get.php on line 60
null

this issue only arises in the case of 1 week/7 days ago option i did a dry run on it and logically it is working fine the conditions are satisfied on every scenario but instead of conditions being true in the case of 1 week/7 days ago i simply couldn't understand why is giving an issue on only one option and working fine for the rest?

uneeb meer
  • 682
  • 2
  • 7
  • 27
  • 2
    If `$data2` is undefined, it means your `while` loop is not executing. If the loop isn't running, its because the database isn't returning any results. Start at the database and make sure your query gets back what you think it does. Also, you should initialize it at the top as an empty array: `$data2 = [];` – Jeremy Harris Jul 15 '16 at 15:43
  • [Improve your security using prepared statements](http://stackoverflow.com/questions/1290975/how-to-create-a-secure-mysql-prepared-statement-in-php). – Davide Pastore Jul 15 '16 at 15:45
  • yea i will initialize it, but most interestingly data exists but the condition that i put is not validating only in the case of 1 week/7 days ago and for the rest it is working because the data that is to be displayed in 1 week/7 days ago will also be displayed in 3 weeks ago and the fascinating part is it is displaying the data in 3 weeks ago but not in 1 week/7 days ago – uneeb meer Jul 15 '16 at 15:48
  • @davide i will thanks for the advice – uneeb meer Jul 15 '16 at 15:51
  • @uneebmeer did you log into the console the value of daysback when substract 7 days? maybe is having some problem with the specific date – cralfaro Jul 15 '16 at 15:57
  • yes i have verified each and every value that is coming and going and everything is working – uneeb meer Jul 15 '16 at 16:28

0 Answers0