1

I am trying to pass a value from my database to an input, but I throw the following error.I'm not sure if for this case you should use return $ this-> db-> get () -> row (); in the model. Any help is welcome.

A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: usuarios/vConsulta_Horarios.php

Line Number: 590

Backtrace:

File: C:\xampp\htdocs\SAE\application\views\usuarios\vConsulta_Horarios.php
Line: 590
Function: _error_handler

File: C:\xampp\htdocs\SAE\application\controllers\cCalendar.php
Line: 26
Function: view

File: C:\xampp\htdocs\SAE\index.php
Line: 315
Function: require_once

Model (mCalendar)

public function lunes(){


$this->db->select('MIN(horario.hrs_ini)');
$this->db->from('horario');
$this->db->join('usuarios','horario.rut_usu = usuarios.rut_usu');
$this->db->where('usuarios.rut_usu','17811942-4'); 
$this->db->where('horario.lunes','ATTE. ESTUDIANTES');


return $this->db->get()->row();

Controller (cCalendar)

public function index(){


        $this->load->view('layouts/header.php');
        $this->load->view('layouts/menu.php');

        $this->load->model('mCalendar');
        $data['start_lunes'] = $this->mCalendar->lunes();
        $this->load->view('usuarios/vConsulta_Horarios.php',$data);
        $this->load->view('layouts/footer.php');

}

View

    <div class="col-md-2">

        <div class="form-group">
          <label>fecha</label>
          <input tupr="text" id="fecha_actual" name="fecha_actual" value="<?php echo $start_lunes;?>">
        </div>
     </div>
5
  • can you add the code of cCalendar.php? Commented Oct 4, 2017 at 16:03
  • is the controller Commented Oct 4, 2017 at 16:04
  • ah, sorry, my misread! What do you use to manage db? probably the result of "row()" is not a single value but an object Commented Oct 4, 2017 at 16:06
  • I should return this 17:30:00 Commented Oct 4, 2017 at 16:09
  • will only return a result always Commented Oct 4, 2017 at 16:15

2 Answers 2

1

Even though you are only selecting one column, you still need to specify it when you output, because $start_lunes is the object that represents the entire row, and as the error tells you, it can't be converted to a string.

I think echo $start_lunes->hrs_ini should work.


Based on your updated function, I think it would be easier if you aliased the MIN expression in your select

$this->db->select('MIN(horario.hrs_ini) AS min_hrs');

Then refer to that in your view: echo $start_lunes->min_hrs

Sign up to request clarification or add additional context in comments.

10 Comments

<input type="text" id="fecha_actual" name="fecha_actual" value="<?php echo $start_lunes->hrs_ini;?>"> it does not work :C
do var_dump($start_lunes); and let us know what's the output
How so? What does it do? Same error? Different error? Nothing?
$data['start_lunes'] = $this->mCalendar->lunes(); var_dump($start_lunes);should it go this way? I've never used it
just add the var_dump() on top of the view's page... <?php print "<pre>"; var_dump($start_lunes); die(); ?>... this will show you the content of variable
|
0

As return $this->db->get()->row(); returns object you are getting this error You need to refer this link

So now, I will suggest you one thing to return data properly. In your Controller change return $this->db->get()->row();

to

  return $this->db->get()->result_array();

Now you are getting data in the format of Array and you can echo this in your View using

// In your view

foreach($start_lunes as $var){
        $value = $var['MIN(horario.hrs_ini)'];
    }

<input type="text" id="fecha_actual" name="fecha_actual" value="<?php echo $value?>"

12 Comments

<input type="text" id="fecha_actual" name="fecha_actual" value="<?php foreach($start_lunes as $var){ echo $var['hrs_ini']; } } ?>" ???
<?php foreach($start_lunes as $var){ $value = $var['hrs_ini']; } ?> <input type="text" id="fecha_actual" name="fecha_actual" value="<?php echo $value?>">
try to print_r($start_lunes);
tell me the result of that
Array ( [0] => Array ( [MIN(horario.hrs_ini)] => 15:30:00 ) )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.