CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

Codeigniter 4 View or fetch record


app/Controllers/Crud.php

<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
 
class Crud extends BaseController {
 
    public function index(){
$data['name'] = 'John Doe';
$data['content'] = 'crud/index_crud';
        return view('templates/main', $data);
    }

    public function user_table(){
        $model = new UserModel();
$data['users'] = $model->getUsers();
        echo view('crud/user_table', $data);
    }

}
?>

app/Models/CrudModel.php

<?php
namespace App\Models;
use CodeIgniter\Model;
 
class CrudModel extends Model
{
    protected $table = 'users';
    protected $allowedFields = ['name', 'email'];

    public function getUsers($id = false) {
      if($id === false) {
        return $this->findAll();
      } else {
          return $this->getWhere(['id' => $id]);
      }
    }
   
}
?>

app/Views/crud/user_table.php

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

  <title>Codeigniter AJAX Tutorial - Fetch Data from Database Example</title>
</head>

<body>
<table class="table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach($users as $data) {  ?>
      <tr>
        <td><?php echo $data['id']; ?></td>
        <td><?php echo $data['name']; ?></td>
        <td><?php echo $data['email']; ?></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
  </body>

</html>