I have created a script to run after the due date is over once due date is over the query will update the status and expire the subscription but as of now I am getting a malfunction issue and the issue is that weh I run script it updates the status to expired I do not know as it should not run query
Here is my code
<?php
if($user_data['subscription'] == 'Activated'):
$sql_chk = $this->db->get_where('payment_trans', array('user_id' => $user_data['id']));
$date_payed = $sql_chk->row()->date_payed;
$d_get = new DateTime($date_payed);
$d_get->modify('+1 year');
$new_year = $d_get->format('m-d-Y');
$crnt_year = time();
if($crnt_year > strtotime($new_year)) {
$data = array(
'subscription' => 'Expired'
);
$this->db->where('id', $user_data['id']);
$this->db->update('users', $data);
}
endif;
?>
Please try this .hope it helps
$current_date = date("Y-m-d H:i:s");
if($user_data['subscription'] == 'Activated')
{
$sql_chk = $this->db->where('date_payed <', $current_date)->where('user_id', $user_data['id'])
->get('payment_trans');
if ($sql_chk->num_rows() > 0) {
$data = array(
'subscription' => 'Expired'
);
$this->db->where('id', $user_data['id']);
$this->db->update('users', $data);
}
}
Related
I am using CodeIgniter. I am working on the small project which is a Batch list. Now If an admin wants to create the batch list then should enter the start date and end date and start time and end time then it will check in the database that batch is running on the same date and time? If yes then it will display the message if not then it will create a new batch list.
If the date is the same the time should be different.
Now My logic is,
I am comparing the first new_start_date with exist_start_date and exist_end_date if date found in between then it will check the time.
It's working till date compare. Even it's checking the time but from there how to exit the process and call the JSON? because from there my JSON not working.
I added "echo "time not match";" from there I am not able to call the JSON I am getting the output on my network tab.
I am getitng the output
enter 1enter 2{"error":true,"msg":"Batch Created"}time not match
Would you help me out in this?
$id = $this->input->post('venue_id');
$venue_id = implode(',',$id);
$activity_list_id = $this->input->post('activity_name');
$new_batch_start_date = date('Y-m-d',strtotime($this->input->post('start_date')));
$new_batch_end_date = date('Y-m-d',strtotime($this->input->post('end_date')));
$new_batch_start_time = $this->input->post('start_time');
$new_batch_end_time = $this->input->post('end_time');
$days = implode(',',$this->input->post('days'));
//print_r($days);
if($new_batch_start_date >= $new_batch_end_date)
{
$response['error'] = false;
$response['msg'] = "End Date Should be Greater than Start Date";
echo json_encode($response);
return false;
}
//convert in Time Format
$new_batch_start_time = strtotime($new_batch_start_time);
$new_batch_end_time = strtotime($new_batch_end_time);
$venue = $this->input->post('name');
$data = array(
'activity_list_id' => $this->input->post('activity_name'),
'batch_venue_id' => $venue_id,
'batch_name' => $this->input->post('batch_name'),
'start_date' => date('Y-m-d',strtotime($this->input->post('start_date'))),
'end_date' => date('Y-m-d',strtotime($this->input->post('end_date'))),
'start_time' => $this->input->post('start_time'),
'end_time' => $this->input->post('end_time'),
'total_capacity' => $this->input->post('total_capecity'),
'batch_status' => 1,
'created_by' => trim($this->session->userdata['login_data']['user_id']),
'created_date' => date('d-m-Y h:i:s A'),
'batch_days' => $days
);
$get_batch_details = $this->Batch_model->fetchBatches();
if(!empty($get_batch_details))
{
foreach ($get_batch_details as $rows)
{
$exist_batch_start_date = $rows->start_date;
$exist_batch_end_date = $rows->end_date;
$batch_time1 = strtotime($rows->start_time);
$batch_time2 = strtotime($rows->end_time);
$batch_venue_id = explode(',',$rows->batch_venue_id);
$common_venue_id = array_intersect($id,$batch_venue_id);
//print_r($common_venue_id);
if($common_venue_id)
{
echo "enter 1";
//if new batch start date between existing batch start date
if($exist_batch_start_date <= $new_batch_start_date && $exist_batch_end_date >= $new_batch_start_date ){
echo "enter 2";
if($batch_time1 <= $new_batch_start_time && $batch_time2 > $new_batch_start_time){
$msg = "Other Batch Alredy Running On from Date $batch_start_date to $exist_batch_end_date on Time : $batch_time1 to $batch_time2.
Please Change Time Slot or Start And End Date";
$response['error'] = false;
$response['msg'] = $msg;
echo json_encode($response);
exit;
}
else{
$result = $this->Batch_model->createBatch($data);
echo "time not match";
print_r($result);
}
break;
}
//if date is different
else
{
$result = $this->Batch_model->createBatch($data);
}
}else
{
$result = $this->Batch_model->createBatch($data);
}
}
}
//first time creating batch
else
{
$result = $this->Batch_model->createBatch($data);
}
Mobel
function createBatch($data){
if($this->db->insert('batch_list',$data))
{
$response['error'] = true;
$response['msg'] = "Batch Created";
echo json_encode($response);
}
else
{
$response['error'] = true;
$response['msg'] = "Failed to Create Batch";
echo json_encode($response);
}
}
function fetchBatches()
{
$result = $this->db->where(['batch_list.batch_status'=>1,'activity_list.act_status'=>1])
->from('batch_list')
->join('activity_list','activity_list.activity_id = batch_list.activity_list_id')
->get()
->result();
return $result;
}
Ajax
success: function(response){
var data = JSON.parse(response);
if (data.error == true){
swal({
title: "Success",
text: data.msg ,
type: "success"
}).then(function(){
location.reload();
}
);
} else {
swal({
title: "Warning",
text: data.msg ,
type: "warning"
});
}
}
Would you help me out in this issue?
your entire approach is a bit messy because you find yourself in a ton of redundant code fragments and nobody is able to understand what exactly you want - i gv you some hints here including an example based on your code
Use Exceptions - it's perfect for your case - if something goes wrong - stop it
Try to filter your need to an extent of one single task und try to solve it - and only after that go to the next task
Always - remember always - think about one term - if you find repeatedly the same code in your application - you know something is wrong - and you should refactor it - don't be ashamed about redundancies - they do always happen - but if you find them, you must refactor those code snippets
Now to your example
What are your tasks here ?
you can try to ask your database if a batch is already running - you dont need to iterate over the entire table entries
Compare both input Dates from Administrator - if start date is in the future of end date, instantely stop the application
your intersection isn't really clear to me what you want to achieve here - but i'm really convinced you can ask the database here too (catchword: find_in_set)
Based on that information we can start to develop things now ;) (if i don't have everything just complete the list above and try to implement your task)
Controller:
try
{
$id = $this->input->post('venue_id');
$venue_id = implode(',',$id);
$activity_list_id = $this->input->post('activity_name');
$new_batch_start_date = date('Y-m-d',strtotime($this->input->post('start_date')));
$new_batch_end_date = date('Y-m-d',strtotime($this->input->post('end_date')));
$new_batch_start_time = $this->input->post('start_time');
$new_batch_end_time = $this->input->post('end_time');
$days = implode(',',$this->input->post('days'));
$objDateStart = DateTime::createFromFormat('Y-m-d h:i a', $new_batch_start_date.' '.$new_batch_start_time);
$objDateEnd = DateTime::createFromFormat('Y-m-d h:i a', $new_batch_end_date.' '.$new_batch_end_time);
if ($objDateEnd < $objDateStart) throw new Exception('End Date Should be Greater than Start Date');
if ($this->Batch_model->hasBatchesBetweenDates($objDateStart, $objDateEnd)) throw new Exception('Other Batch already running On from '.$objDateStart->format('d-m-Y H:i').' to '.$objDateEnd->format('d-m-Y H:i').'. Please Change Time Slot for Start and End Date');
$data = array(
'activity_list_id' => $this->input->post('activity_name'),
'batch_venue_id' => $venue_id,
'batch_name' => $this->input->post('batch_name'),
'start_date' => $objDateStart->format('Y-m-d'),
'end_date' => $objDateEnd->format('Y-m-d'),
'start_time' => $objDateStart->format('H:i'),
'end_time' => $objDateEnd->format('H:i'),
'total_capacity' => $this->input->post('total_capecity'),
'batch_status' => 1,
'created_by' => trim($this->session->userdata['login_data']['user_id']),
'created_date' => date('d-m-Y h:i:s A'),
'batch_days' => $days
);
$this->Batch_model->createBatch($data);
}
catch(Exception $e)
{
$arrError = [
'error' => false,
'msg' => $e->getMessage()
];
echo json_encode($arrError);
}
Model:
public function hasBatchesBetweenDates(DateTime $objDateStart, DateTime $objDateEnd)
{
$query = $this->db
->from('batch_list')
->join('activity_list','activity_list.activity_id = batch_list.activity_list_id')
->where('CONCAT(start_date,\' \',start_time) >=', $objDateStart->format('Y-m-d H:i:s'))
->or_group_start()
->where('CONCAT(end_date, \' \', end_time) <=', $objDateEnd->format('Y-m-d H:i:s'))
->where('CONCAT(end_date, \' \', end_time) >=', $objDateStart->format('Y-m-d H:i:s'))
->group_end()
->get();
return ($query->num_rows() > 0);
}
i hope you understand the concepts here - if you've questions - don't hesitate to ask
I wrote a following PHP+codeigniter code:
$msg = $this->db->get('Messages');
$curDate = date('Y-m-d H:i:s');
$array = array('IsRead =' => 0, 'Date <=' => $curDate);
$this->db->where($array);
$this->db->set(array('IsRead' => 1));
$this->db->update('Messages');
Which basically checks which records in Messages table meet the two conditions regarding fields IsRead and Date, and then updates only those records.
Now I would like to pick up every Date value that meets said criteria and execute some PHP code for it. I suppose it's a job that foreach directive could manage, but I have no success coding it.
Could I ask for some help.
Try following code, hope will help....
$curDate = date('Y-m-d H:i:s');
$array = array('IsRead' => 0, 'Date <=' => $curDate);
$this->db->where($array);
$this->db->set('IsRead',1));
$this->db->update('Messages');
if ($this->db->affected_rows()>0) {
echo "Update successful";
}
else
{
echo "No any row updated";
}
So, the answer seems to be:
$curDate = date('Y-m-d H:i:s');
$array = array('IsRead =' => 0, 'Date<=' => $curDate);
$this->db->where($array);
$msg = $this->db->get('Messages');
foreach ($msg->result() as $row)
{
echo $row->Date . " "; //here goes the PHP code
}
$this->db->where($array);
$this->db->set(array('IsRead' => 1));
$this->db->update('Messages');
I am using PHP and SQL Server to first request an HTML template stored in my database, and then update it in a different table in the database based on form input from my website.
my update function in my model uses a get_template function to pull the text from the database, then updates the table.
public function update_domain_rules($template_content, $domain_guid) {
$sql = "UPDATE domains
SET rules = ?
WHERE domain_guid = ?";
$query = $this->db->query($sql, array($template_content, $domain_guid));
return $query->result();
}
I need to be able to replace certain words in the template I pulled with variables in my controller, specifically the start date, end date, and the name of the domain that is selected in the dropdown, and then update the database. this is the code for my controller:
function content()
{
$this->load->model('domains_model');
$this->load->model('insert_rules_model');
$domain_guid = $this->input->post('edit_domain_guid');
$start_date = $this->input->post('start_date');
$end_date = $this->input->post('end_date');
$template = $this->input->post('select_template');
if ($start_date == '') {
$start_date ==date("m/d/Y",strtotime("-1 day"));
}
if ($end_date =='') {
$end_date ==date("m/d/Y",strtotime("-1 day"));
}
$start_date_formatted = date_create($start_date);
$end_date_formatted = date_create($end_date);
$template_name = array( //create array referenced in the form_dropdown in the view
'aliens_ep1_template',
'aliens_ep2_template',
'zombies_ep1_template',
'zombies_ep2_template'
);
$template_text = $this->insert_rules_model->get_template($template);
if ($template != null) {
foreach ($template_text as $row) {
$query = $this->insert_rules_model->update_domain_rules($row->template, $domain_guid);
}
}
//redirect ("admin/insert_rules");
$data = array(
'template_name' => $template_name,
'domains' => $this->domains_model->domain_dropdown_list(),
'start_date' => $start_date,
'end_date' => $end_date,
'domain_guid' => $domain_guid
);
$this->load->view('includes/header_admin');
$this->load->view('admin/insert_rules', $data);
$this->load->view('includes/footer_admin');
}
I am very new to PHP so forgive me if my code looks ugly. I am just trying to get this thing to be functional.
The first condition is working, the flag is set to 0 if it is 1
but it cannot turn back to 1 on click punch in button. this is the model function.called login_info_model it store the date & time of punchin & punch out.
function login_info_model($id)
{
$reg = "%Y:%m:%d:%h:%i";
$data = array(
'logindt' => mdate($reg) ,
'userid' => $id
);
$qry = $this->db->insert('registry', $data);
if ($qry) {
$on = array(
'flag' => 1
);
$off = array(
'flag' => 0
);
$flg = $this->db->get('users', 'flag');
if ($flg == 1) {
$this->db->where('id', $id);
$this->db->where('flag', 1);
$this->db->update('users', $off);
} else {
$this->db->where('id', $id);
$this->db->where('flag', 0);
$this->db->update('users', $on);
}
return $qry;
}
}
This solved the problem.
$this->db->select('flag');
$this->db->from('users');
$this->db->where('id',$id);
$flaggg=$this->db->get();
$flagg=$flaggg->row_array();
$flg=implode("",$flagg);
I'm trying to make a visit counter in codeigniter with filters by cheking the ip.
This is my code so far:
public function new_visit($ip)
{
$new_ip = $ip;
$this->db->from('visitas');
$this->db->where('ip_addr', $new_ip);
$query = $this->db->get();
$count = $query->num_rows();
if ($count > 0) {
$lastv = $query->row('last_visit');
$currentv = new DateTime('NOW');
$consulta = $this->db->query('SELECT TIMESTAMPDIFF(HOUR,"$currentv","$lastv" ) FROM visitas WHERE ip_addr = "$new_ip"',FALSE);
**if ($consulta->result() > 10) {**
$this->db->set('cant_visit', 'cant_visit+1', FALSE);
$this->db->set('last_visit', 'NOW()', FALSE);
$this->db->update('visitas');
}
} else {
$data = array(
'ip_addr'=>$new_ip
);
$this->db->set('cant_visit', 'cant_visit+1', FALSE);
$this->db->set('first_visit', 'NOW()', FALSE);
$this->db->insert('visitas', $data);
}
My problem is, that the highlighted sector always return true, so I always get a new visit every time I click the menu bar. I tried different ways to do it, but no success so far.
The date field in my sql table (cant_visit) is a datetime.
Hope someone can help me!, thanks!
I answer myself:
public function new_visit($ip)
{
$new_ip = $ip;
$this->db->from('visitas');
$this->db->where('ip_addr', $new_ip);
$query = $this->db->get();
$count = $query->num_rows();
if ($count > 0) {
$currentv = new DateTime('NOW');
$currentv = $currentv->format('Y-m-d H:i:s'); // had to format this
$q = $this->db->query("SELECT TIMESTAMPDIFF(HOUR, '$currentv', last_visit) as timediff FROM visitas WHERE ip_addr = '$new_ip'");
$time_diff = $q->row()->timediff;
// return $time_diff; <-- just for testing
if ($time_diff > 10) {
$this->db->set('cant_visit', 'cant_visit+1', FALSE);
$this->db->set('last_visit', 'NOW()', FALSE);
$this->db->update('visitas');
}
} else {
$data = array(
'ip_addr'=>$new_ip
);
$this->db->set('cant_visit', 'cant_visit+1', FALSE);
$this->db->set('first_visit', 'NOW()', FALSE);
$this->db->insert('visitas', $data);
}
}
thanks!