Welcome Guest, Not a member yet? Register   Sign In
Simple query
#1

I'm starting with ci4, so I'm still learning.
It's working like a charm, but I would like to get all the results without the getWhere and I can't find the solution.
i've tried a lot of things but always get an "Undefined index id_labo"

My table is very simple, (id_labo, name, active) and I'm using different "db base" by adding different conf in Config > Database.php

This is my model :
PHP Code:
<?php
namespace App\Models;
use 
CodeIgniter\Database\ConnectionInterface;
use 
CodeIgniter\Model;

class 
LabosModel extends Model
{
     protected 
$db;

    public function __construct(ConnectionInterface &$db)
    {
        $this->db =& $db;
    }

    function getLabos()
    {

        $builder $this->db->table('labos');
        $labos $builder->getWhere(['active' => '1'])
                        ->getResult();
        return $labos;
    }   



My controller :
PHP Code:
public function labos()
    {
        
$db = \Config\Database::connect('fh');
        
$model = new LabosModel($db);

        
$data['labos'] = $model->getlabos();

        echo 
view('partials/header'$data);

        echo 
view('pages/labos'$data);

        echo 
view('partials/footer'$data);

    } 

and view :
PHP Code:
<table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Nom</th>
      
      
<th><class="fa fa-eye" aria-hidden="true"></i></th>
    </tr>
  </thead>
  <tbody>
<?
php foreach ($labos as $labo) : $labo get_object_vars($labo); ?>

    <tr>
      <th><?=$labo['id_labo']?></th>
      <td><?=$labo['name']?></td>
      <td><a href="#" target="_blank" class="btn btn-primary">Voir</a></td>
    </tr>
    <?php endforeach;?>
  </tbody>
</table> 
Reply
#2

Model :

PHP Code:
public function get_labos(){

        $db      = \Config\Database::connect();
        $builder $db->table('labos');
        $query $builder->select('*')
                ->get();

        return $query->getResult();       
    



Controller:

You can shorten your Model link in public __ construct first :

PHP Code:
public function __construct()
    {
        
        $this
->lab = new LabosModel();
        
    


down in controller:
PHP Code:
public function labos(){
$data['labos'] = $this->lab->get_labos();
 
        echo view('admin/assets/header'$data);
        echo view('admin/labos');
        echo view('admin/assets/footer');



view:

PHP Code:
<?php 
    
foreach($labos as $lab){?>

<p><?php echo $lab->name?></p>

<?php ?>


Check if you have assigned auto increment to your id in mysql
Reply
#3

Thx guy, really appreciate.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB