codeigniter Date_Format in DB->select - codeigniter

Can someone tell me how to correctly use the following query using codeigniter:
DATE_FORMAT(sr.dateCreated,'%m/%d/%Y # %h:%i %p') AS dateCreated
ive tried:
public function viewSR($id) {
$this->db->select('sr.id
,sr.woid
,sr.description
,DATE_FORMAT(sr.dateCreated),'%m/%d/%Y # %h:%i %p') AS dateCreated
,sr.startTime
,sr.endTime
,rt.name AS rateName');
$this->db->from('service_report sr');
any help would be very grateful!

Try to change your code like this:
public function viewSR($id) {
$this->db->select("sr.id
,sr.woid
,sr.description
,DATE_FORMAT(sr.dateCreated,\"%m/%d/%Y # %h:%i %p\") AS dateCreated
,sr.startTime
,sr.endTime
,rt.name AS rateName",FALSE);
$this->db->from('service_report AS sr');
or try modify your code this way:
function list_all($lang, $limit = FALSE, $offset = FALSE)
{
$this->db->select('*');
$this->db->select("DATE_FORMAT(datetime, '%d.%m.%Y à %H:%i:%s') AS datetime");
$this->db->where('lang', $lang);
$this->db->limit($limit);
$this->db->offset($offset);
$this->db->order_by('datetime', 'desc');
$query = $this->db->get('news');
return $query->result();
}

Related

Codeigniter : array to string conversion error

Here is my model:
public function count_diabetic(){
$query = $this->db->query("Select count(diabetic) as count_diabetic from member where diabetic is not NULL");
return $query->result_array();
}
public function count_hypertensive(){
$query = $this->db->query("Select count(hypertensive) as count_hypertensive from member where hypertensive is not NULL");
return $query->result();
}
here is my controller:
public function home(){
$this->load->model('Jsv_model');
$data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata($data);
$this->load->view('home');
}
Here is my view with in php tag:
echo $this->session->userdata('count_diabetic');
But Here it shows error that array to string conversion error..
please help me
Inside count_diabetic() you should change $query->result_array()
into
$query->row()->count_diabetic,
it would return number of count only not array.
Do it to count_hypertensive() too.
In PHP, you can display arrays by using the print_r function instead of echo.
For example, you have to change your code to:
print_r($this->session->userdata['count_diabetic']);
You are doing it wrong in this line.
echo $this->session->userdata('count_diabetic');
What should you do?
// To Set a Data in Session
$session_data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata('user_data', $session_data);
// To get the session data
$session_data = $this->session->userdata('user_data');
$user_name = $session_data['username'];
$count_diabetic = $session_data['count_diabetic'];
$count_hypertensive = $session_data['count_hypertensive'];

Laravel 5 dates

i tried checking and set status by range date
foreach($reserves as $reserve){
$table = App\TableDiagram::where('t_number',$reserve->st_number)
->first();
if (App\BusyTable::where('table_diagram_id',$table->id)
->where('end','>=',Carbon::now())
->first()) {
$reserve->status = 'active';
}
if (App\DepTable::where('table_diagram_id',$table->id)
->where('end','>=',Carbon::now())
->first()) {
$reserve->status = 'deposited';
}
}
many for some reason an active, but don't pass on the condition
what is wrong?
UPDATE
added screen what i doing
date format on screen just replaced like \Carbon\Carbon::parse($reserve->reserve_date)->format('d-m-Y H:i:s')
I think you just need to format your date.
Try this
$formattedDate = date('m-d-Y H:i:s',time());
if (App\BusyTable::where('table_diagram_id',$table->id)
->where('end','>=',$formattedDate)
->first()) {
$reserve->status = 'active';
}
if (App\DepTable::where('table_diagram_id',$table->id)
->where('end','>=',$formattedDate)
->first()) {
$reserve->status = 'deposited';
}
You have to use either ->whereDate('end', '>=', Carbon::now()); or ->where(\DB::raw("DATE(end) >= '".Carbon::now()."'"));
UPDATE
I would do like this
$table = App\BusyTable::where('table_diagram_id',$table->id)->first();
if (Carbon::now()->lt(Carbon::createFromFormat('Y-m-d H:i:s', $table->end)) {
$reserve->status = 'active';
}
Try this it must work:
if (App\BusyTable::where( function ($query) use ($table) {
$query->where('table_diagram_id',$table->id)
->where('end','>=',Carbon::now()->format('Y-m-d H:i:s'))
})->first()) {
$reserve->status = 'active';
}
if (App\DepTable::where( function ($query) use ($table) {
$query->where('table_diagram_id',$table->id)
->where('end','>=',Carbon::now()->format('Y-m-d H:i:s'))
})->first()) {
$reserve->status = 'deposited';
}
Hope it help you :)

I couldnt get the joined table value in my query

Workflow type table
[Workflow caterory table][2]
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
$this->db->from('workflow_category');
$this->db->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() AS $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
here im not able to print the $row["type_name"] and also showing a notice as undefined index for $row["type_name"].
Try like this...
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat,workflow_type.type_name')
->from('workflow_category')
->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
Error due to this line..
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
So it should be just
$this->db->select('workflow_category.wf_cat,workflow_type.type_name');
In above query i have been used query grouping.

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

Debug Codeigniter Shoppincart Functions:

Can I have some help with this, please. This is a Codeigniter script MVC. There is a Controller "Function add to cart" with a Model: "Function get_products" in Models. I cannot see what is wrong here and why the function get_products doesn't execute. Could someone help me please.
This is the Model: get_product which connects to database:
function get_product()
{
$product_id = $this->input->post(‘product_id’);
$query = $this->db->select(‘product_id, product_name, description, price, photopath’);
$query = $this->db->from(‘product’);
$query = $this->db->where(‘product_id’, $product_id);
$query = $this->db->get(’‘);
return $query->result_array();
}
This is the Controller called Function add_cart, which add products to the "shopping cart view":
public function add_cart()
{
$thisProduct = $this->Cart_model->add_product();
if($thisProduct->num_rows() > 0)
{
$data = array(‘id’ => $thisProduct[‘product_id’],
‘qty’ => 1,
‘price’ => $thisProduct[‘price’],
‘name’ => $thisProduct[‘product_name’],
‘description’ => $thisProduct[‘description’]
);
$this->cart->insert($data);
}
$this->load->view(“site_header”);
$this->load->view(“site_nav”);
$this->load->view(“shoppingcart”, $data);
$this->load->view(“site_footer”);
}
you make many mistakes.
read here: http://codeigniter.com/forums/viewthread/128969/

Resources