Item is not being deleted from database when time is expired

49 Views Asked by At

I am trying to make deal of the day feature in online store. I can make any product deal of the day, so I will give expiry time and date so that product is automatically removed from deal of the day table in data base. My js code is

   $.ajax({
 url:'<?php echo base_url() ?>public_controller/get_deals_of_the_day',
 method:'post',
 dataType:'json',
 success:function(data) {
   console.log(data);
   let i;
   setInterval(function(){
     var today = new Date();
     var date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
     var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

  for(i in data){
    var month = parseInt(data[i].expire_time.slice(0,2));
    var day = parseInt(data[i].expire_time.slice(3,5));
    var year = parseInt(data[i].expire_time.slice(6,10));
    var format = data[i].expire_time.slice(16,19);
    var hours = parseInt(data[i].expire_time.slice(11,13));
    if(format == 'PM' || format == ' PM'){
      hours = hours + 12;
    }
    // console.log(hours);
    var mins = parseInt(data[i].expire_time.slice(14,16));
    var cMonth = today.getMonth() + 1;
    var cDay = today.getDate();
    var cYear = today.getFullYear();
    var cHours = today.getHours();
    var cMins = today.getMinutes();

            if (cDay >= day && cHours >= hours && cMins > mins) {
            let delId = data[i].product_id;
            console.log(delId);
            $.ajax({
              url:"<?php echo base_url(); ?>public_controller/del_deal",
              method:"post",
              data:{delId: delId},
              success:function(){
                // alert('deal expired');
                // location.reload(true);
              },
              error:function(e){
                alert(e);
              }
            });
            }
   }

 }
  },1000);

});

what i am doing is that I am fetching data of deal of the day table from database as the homepage loads and checking every second either any product has reached it's expiry time. When products any products expires it send id to controller to delete product from deal of the table. My controller method is

  public function del_deal(){
$deal_id = $this->input->post('delId');
$deal = $this->db->where('deal_id',$deal_id)->join('products','products.product_id = 
deal_of_day.product_id')->get('deal_of_day')->row_array();
$update['price'] = $deal['old_price'];
$this->db->where('product_id',$deal['product_id'])->update('products',$update);
$this->db->where('deal_id',$deal_id)->delete('deal_of_day');

}

Condition is verifying I am just checking for day, hours and mins for testing purpose. Problem is it is not deleting product as conditions satisfies. I making online store in codeigniter framework. I haven't made any feature like this before so if I am doing or if there is better way to do it please suggest me that would be so helpful.

0

There are 0 best solutions below