I have this codeigniter mysql code to create an appointment:
if ($this->form_validation->run() == TRUE)
{
$data = array(
'startTime' => $startTime ,
'endTime' => $endTime ,
'day' => $date ,
'contact_id' => $this->input->post('InputPatient')
);
$this->db->insert('rdv', $data);
echo 'Appointment Created Successfully.';
}
What I need is - for a specific time like 8pm, I should not have more than 2 appointment created. On third insert with same start time, I should give an error.
Thanks.
Before you do your insert you could do a select to see how many entries there are with the same start time. If there are less than 2 then you can proceed to do your insert.
$this->db->select('count(*) as count')
->from('rdv')
->where('startTime', $startTime)
->where('day', $date);
$query = $this->db->get();
$aResult = $query->row_array();
if($aResult['count'] < 2)
{
//Do your insert
}
Here you go. Before you insert, check if there are already 2 appointments for a specific time and date.
if ($this->form_validation->run() == TRUE)
{
$data = array(
'startTime' => $startTime ,
'endTime' => $endTime ,
'day' => $date ,
'contact_id' => $this->input->post('InputPatient')
);
$isAlreadyTwo = $this->db->from("rdv")
->where(array("startTime" => $startTime, "day" => $day))
->count_all_results() >= 2;
if($isAlreadyTwo)
{
echo "Error!!";
}
else
{
$this->db->insert('rdv', $data);
echo 'Appointment Created Successfully.';
}
}
Hope this helps :)
Related
this code repeats one IP hits every time when I visit this IP...
if I visit repeatedly with one IP hits increment, not IP increment
how can I solve this ?? can anyone guide?
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$value = date_create($get_visit_day->created_at);
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}else{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])
->increment('hits')->insert('updated_at', $date);
// DB::table('visitors')->insert('updated_at', $date);
}
// $value = date_create($get_visit_day->created_at);
if ($check_if_exists && date_format($value, 'd') != date('d')) {
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}[enter image description here][1]
try this if you want to add new record for each day , and increment view count for every hit for today form unique ip
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$is_today_if_exists = DB::table('visitors')->whereDate('created_at',$date)
->where('ip', $_SERVER['REMOTE_ADDR'])->count();
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}
elseif ($is_today_if_exists>0)
{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])->whereDate('created_at',$date)
->increment('hits');
}
else
{
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}
Hello i need serious help cause i have tried all way and not find an answer... so i really want to display chart that count many data each month, ex january 2 data, febuary 3 data etc... pls look at this brother
public function lihatkeluhan(){
$halaman="tindaklayanan";
$keluhan_list=DB::table('keluhans')
->select(DB::raw('id,tanggal,produk,username,area,masalah,status'))->get();
$keluhan_group = keluhan::select(DB::raw('id,tanggal,produk,username,area,masalah,status'))
->get()->groupBy(function($date) {
return Carbon::parse($date->tanggal)->format('m'); // grouping by months
});
foreach ($keluhan_group as $group) {
$count[] = count($group);
}
$bulan = array("Jan","Feb","Mar","Apr","Mei","Jun","Jul","Agu","Sep","Okt","Nov","Des");
$count = Count($keluhan_list);
$population = Lava::DataTable();
$population->addDateColumn("Month")
->addNumberColumn('Keluhan');
foreach($keluhan_group as $group){
$population->addRow(["jan",$count]);
}
Lava::LineChart('Population', $population, [
'title' => 'Tahun : 2017',
'titleTextStyle' => [
'color' => '#212F3C',
'fontSize' => 14
]
]);
$keluhan_group used to group by month
$count result is number of data each month
But idk how to display on chart...
Btw $population->addDateColumn("Month") is not work, it not display month but year T_T
Replace your code from $keluhan_group to $population with below code and check.
here you need to pass array of month and count of that month
$keluhan_group = keluhan::select(DB::raw("COUNT(*) as count , MONTHNAME(created_at) as month"))
->orderBy("created_at")
->groupBy(DB::raw("month(created_at)"))
->get()->toArray();
$chart_array = array();
foreach($keluhan_group as $data){
$n_data = [];
array_push($n_data,$data['month'],$data['count']);
array_push($chart_array,$n_data);
}
$population = Lava::DataTable();
$population->addDateColumn("Month")
->addNumberColumn('Keluhan')
->addRow($chart_array);
hope this will Help.
see i have implemented chart using my 'User' tabel,
i was not having lavachart in my project so i have update it using http://itsolutionstuff.com/post/laravel-5-geo-chart-example-and-demo-using-lavacharts-packageexample.html
here is my controller code:
$users = Users::select(DB::raw("COUNT(*) as count , MONTHNAME(created_at) as month"))
->orderBy("created_at")
->groupBy(DB::raw("month(created_at)"))
->get()->toArray();
$chart_array = array();
foreach($users as $data){
$n_data = [];
array_push($n_data,$data['month'],$data['count']);
array_push($chart_array,$n_data);
}
$lava = new Lavacharts;
$popularity = $lava->DataTable();
$popularity->addStringColumn('Country')
->addNumberColumn('Popularity')
->addRows($chart_array);
$lava->LineChart('demochart', $popularity, [
'title' => 'Demo population count',
'animation' => [
'startup' => true,
'easing' => 'inAndOut'
],
'colors' => ['blue', '#F4C1D8']
]);
return view('welcome', ['lava' => $lava]);
and in my .blade file:
<div id="temps_div"></div>
<?= $lava->render('LineChart', 'demochart' , 'temps_div') ?>
Hope this will Help.
$companies=Company::all();
$start = (new DateTime('2017-01-01'))->modify('first day of this month');
$end = (new DateTime('2022-01-01'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('12 month');
$period = new DatePeriod($start, $interval, $end);
$lava = new Lavacharts; // See note below for Laravel
$finances = \Lava::DataTable();
$finances->addDateColumn('Year');
foreach($companies as $company)
{
$finances ->addNumberColumn($company->company_name." ".$company->company_location." total sales amount");
}
foreach ($period as $dt) {
$yeardate=$dt->format("Y-m-d");
$insertrow = array( $yeardate );
foreach($companies as $company)
{
$cmp= $company->company_name;
$loc= $company->company_location;
$companiesinfo[]=$cmp." ".$loc;
$databasename=$company->database_name;
\Config::set('database.connections.tenant.host', 'localhost');
\Config::set('database.connections.tenant.username','root');
\Config::set('database.connections.tenant.password', '');
\Config::set('database.connections.tenant.database', $databasename);
\Config::set('database.default', 'tenant');
DB::reconnect('tenant');
$calculatesalesofcompany=B2CFINAL::
select(DB::raw('sum(GrandTotal) as total'))
->where(DB::raw('YEAR(SaleDate)'), '=', $yeardate)
->first();
if($calculatesalesofcompany->total==null)
{
$calculatesalesofcompany->total=0;
}
array_push( $insertrow, $calculatesalesofcompany->total );
}
$finances ->addRow($insertrow);
}
\Lava::ComboChart('Finances2', $finances2, [
'title' => 'Company Performance',
'titleTextStyle' => [
'color' => 'rgb(123, 65, 89)',
'fontSize' => 16
],
'legend' => [
'position' => 'in'
],
'seriesType' => 'bars',
]);
I have startdate and end date in my table like below.
startdate = 2016-01-01
Enddate = 2016-06-20
Now i want to send email automatically before 1 month from end date.
everything is working fine...email is also sending..
But my problem is i want to send email to multiple users if there are 2 rows match in table with my conditions then email is gone to both the users.
But my code only send email to only single users..not all users.
I want email is gone to all users one by one.
public function certificateExpired()
{
$this->autoRender = False;
$this->loadModel('Certificate');
$data = $this->Certificate->find("all", array(
'recursive' => -1, // should be used with joins
'fields' => array('User.*', 'Certificate.*'),
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array('User.id = Certificate.user_id')
)
),
'conditions' => array('Certificate.is_expirable' => 1,'Certificate.status' => 1)
));
foreach ($data as $row) {
$currentdate = date("Y-m-d");
$date = $row['Certificate']['end_date'];
$newdate = strtotime($date .' -1 months');
$enddate = date('Y-m-d', $newdate);
if ($currentdate >= $enddate) {
//For sending email
$data['email'] = $row['User']['email'];
$data['name'] = $row['User']['name'];
$data['document_name'] = $row['Certificate']['name'];
$data['templateid'] = 19;
$send_mail = $this->EmailFunctions->certificateExpiredTemplate($data);
if ($send_mail) {
$this->redirect(array('controller' => 'Dashboards', 'action' => 'shipperDashboard'));
}
}
}
}
foreach ($data as $row) {
$currentdate = date("Y-m-d");
$date = $row['Certificate']['end_date'];
$newdate = strtotime($date .' -1 months');
$enddate = date('Y-m-d', $newdate);
if ($currentdate >= $enddate) {
//For sending email
$data['email'] = $row['User']['email'];
$data['name'] = $row['User']['name'];
$data['document_name'] = $row['Certificate']['name'];
$data['templateid'] = 19;
$send_mail = $this->EmailFunctions->certificateExpiredTemplate($data);
}
}
$this->redirect(array('controller' => 'Dashboards', 'action' => 'shipperDashboard'));
Change the foreach loop as above.
Whenever i try to run this code, it seems like it gets into an endless loop. But, i cant figure out whats causing this problem. Maybe an extra eye on the thing would be able do point out the problem?
The problem only accours when there is a different year zone, example 2012-2018
Example: $this->budget_model->set_monthly_budget('1','2012', '8','2014','1');
function set_monthly_budget($start_month, $start_year, $end_month, $end_year, $budget_group_id)
{
$user_id = 2;
// Current date
$current_month = $start_month;
$current_year = $start_year;
$days_in_current_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
$company_id = 1;
$month_goal = 100;
// Check if it is the current month
if($start_year == $end_year)
{
for($x=$current_month;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
return true; // Return true if the task was completed
}
if($start_year !== $end_year)
{
$temp_start_year = $start_year;
while($temp_start_year !== $end_year)
{
// Check if we are in the same year as we started
if($temp_start_year == $current_year)
{
// Insert remaining months for this year
for($x=$current_month;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if the temp and end year is the same
if($temp_start_year < $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if we are in the same year as we started
if($temp_start_year == $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
$temp_start_year++;
}
}
}
in your code
while($temp_start_year !== $end_year)
you used !== which also check if the type of the 2 variables are the same.
but this line
$temp_start_year++;
will implicitly cast the variable to integer.
Therefore the !== will be comparing integer to string, which will always evaluate to true.
The solution is as simple as using != instead of !==, or feed an integer instead of string when you call your function (remove the single quotes).
I just wondered if anybody can point me in the right direction: I'm looking to make a script whereby the logo on my site changes depending on the date; so for instance a haloween style one soon.
I started off by having 2 arrays, 1 of start dates and 1 of end dates(not sure even if this is the best way!):
<?php
$start_dates = array('01/01' => 'New Years',
'14/02' => 'Valentine Day',
'16/02/2010' => 'Pancake Day',
'17/03' => 'St Patricks Day',
'01/04' => 'April Fools',
'02/04/2010' => 'Easter',
'23/04' => 'St Georges Day',
'11/06/2010' => 'World Cup',
'31/10' => 'Halloween',
'05/11' => 'Guy Fawkes',
'11/11' => 'Armistice Day',
'16/10' => 'Today',
'15/12' => 'Christmas');
$end_dates = array( '08/01' => 'New Years',
'15/02' => 'Valentine Day',
'17/02/2010' => 'Pancake Day',
'18/03' => 'St Patricks Day',
'02/04' => 'April Fools',
'06/04/2010' => 'Easter',
'24/04' => 'St Georges Day',
'12/07/2010' => 'World Cup',
'01/11' => 'Halloween',
'06/11' => 'Guy Fawkes',
'12/11' => 'Armistice Day',
'17/10' => 'Today',
'01/01' => 'Christmas');
?>
Easy so far...the problemis that I need a way of working out if todays date falls between the start date and end date, then changing the image file name.
Its a long shot but I hope someone would be kind enough to help.
Thanks,
B.
like this
$events = array(
'New Year' => '01/01 01/08',
'Pancake Day' => '16/02/2010 17/02/2010',
//etc
);
echo find_event($events, '16/02');
where find_event() is
function mdy2time($date) {
$e = explode('/', $date);
if(count($e) < 3)
$e[] = '2010';
return strtotime("$e[1]-$e[0]-$e[2]");
}
function find_event($events, $date = null) {
$date = is_null($date) ? time() : mdy2time($date);
foreach($events as $name => $range) {
list($start, $end) = explode(' ', $range);
if($date >= mdy2time($start) && $date <= mdy2time($end))
return $name;
}
return null;
}
you should use an array more like this:
$dates = array();
$dates[] = array(
'name' => 'New Years'
'start' = '01/14',
'end' => '01/20',
'style' => 'haloween',
);
$dates[] = array(
//...
);
then you can get the style as follows:
$style='default';
// date as number e.g. 130 (january 30th)
$currDate = date('md',time()) * 1;
foreach ($dates AS $k => $v) {
$tmp = explode("/",$v['start'];
$start = ($tmp[1].$tmp[0])*1;
$tmp = explode("/",$v['end'];
$stop = ($tmp[1].$tmp[0])*1;
if ($start <= $currDate && $currDate < $stop) {
$style=$v['style'];
break;
}
}
echo 'style: '.$style;
Didn't check the code yet, so feel free to correct me if iam wrong.