Welcome Guest, Not a member yet? Register   Sign In
Help with a simple query
#1

[eluser]Miguel Diaz[/eluser]
I have a database in which I have reservations on that reservations I have one field named reservationServicedate i need to make a query in which I can get the reservations for that date.

I have been trying a few ways but it is not working I hope someone can help

Here is my code

Controller
Code:
function index()
    {
        //Muestra Reservaciones por dia
        $op['todaysReservations'] = $this->data_model->getTodaysreservations();
        
        //Vista//
        $this->layouts->view('index-view', $op);
    }

view
Code:
<?php foreach($todaysReservations as $rowtodaysReservations) :?>
    <?=$rowtodaysReservations->reservationID;?>
<?php endforeach; ?>

this is my function model
Code:
function getTodaysreservations(){
        $data = array();
        $q = $this->db->query("SELECT * FROM reservations WHERE reservationServicedate = date('y-m-d')");
        if($q->num_rows() > 0) {
            foreach($q->result() as $row){
                $data[] = $row;
            }
            $q->free_result();      
        }
        return $data;
        
    }

Thanks in advance
#2

[eluser]JHackamack[/eluser]
Try this:
Code:
$this->db->where('reservationServicedate',date('Y-m-d');
$q = $this->db->get('reservations');

It's better to use Y rather than y because MySQL's date field is a 4 digit year rather than a 2 digit one.
#3

[eluser]JHackamack[/eluser]
You can also optimize your code by replacing your Foreach statement with $data = $q->result();
#4

[eluser]Miguel Diaz[/eluser]
Thanks for the replay Hackmack I put how you write it and it send me a syntax error any idea
#5

[eluser]davidbehler[/eluser]
PHP or MySQL syntax error? What error exactly?
#6

[eluser]Miguel Diaz[/eluser]
I was put it in a single view that query and it is sending me this error.

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/pruebadate-view.php

Line Number: 10

This is the foreach I have in my view
Code:
<?php foreach($todaysReservations as $rowtodaysReservations) :?>
    <?=$rowtodaysReservations->reservationID;?>
<?php endforeach; ?>

this is my controller
Code:
<?php

class Prueba extends Controller {
    
    function index()
    {
        //Optimizacion y conexion de tags para SEO//
        $tags = $this->uri->uri_string();
        $op['tags'] = $this->data_model->cargarTags($tags);
        
        //Muestra Reservaciones por dia
        $op['todaysReservations'] = $this->data_model->getTodaysreservations();
        
        //Carga los extras al Template Layout//
        $this->layouts->add_include('assets/js/.js');
        
        //Vista//
        $this->layouts->view('pruebadate-view', $op);
    }
}
and this is the function model
Code:
function getTodaysreservations(){
        $date = date('Y-m-d');
        $op = array('reservationServicedate >' => $date);
        $this->db->where($op);
        $query = $this->db->get('reservations');  
    }
#7

[eluser]davidbehler[/eluser]
add this at the bottom of your function in the model
Code:
if($query->num_rows() > 0) {
  return $query->result_array();
} else {
  return FALSE;
}

and in your view add a check to see if the returned result is an array:
Code:
<?php
if(is_array(todaysReservations)) {
  foreach($todaysReservations as $rowtodaysReservations) {
    echo $rowtodaysReservations->reservationID;?>
  }
} else {
  echo 'No reservations';
}
?>
#8

[eluser]Miguel Diaz[/eluser]
I already put it like you write it and it is sending me a white page with nothing

Code:
<div class="nav-wrap">
    <div class="nav center">
    <img src="&lt;?=base_url()?&gt;assets/graphics/logo.jpg" alt="logo">
    </div>
    <div class="nav">
    </div>
</div>
<div class="content-wrap">
&lt;?php
if(is_array(todaysReservations)) {
  foreach($todaysReservations as $rowtodaysReservations) {
    echo $rowtodaysReservations->reservationID;?&gt;
  }
} else {
  echo 'No reservations';
}
?&gt;

and in my function model
Code:
function getTodaysreservations(){
        $date = date('Y-m-d');
        $op = array('reservationServicedate >' => $date);
        $this->db->where($op);
        $query = $this->db->get('reservations');
        if($query->num_rows() > 0) {
          return $query->result_array();
        } else {
          return FALSE;
}  
    }

I guess the fuction model maybe the view code what you think?

Thanks in advanced
#9

[eluser]davidbehler[/eluser]
White page usually means there's a PHP error or something.
Have you turned on error reporting?




Theme © iAndrew 2016 - Forum software by © MyBB