Home > database >  fetch data from database in php codeigniter but not showing but data print_r in view working is fine
fetch data from database in php codeigniter but not showing but data print_r in view working is fine

Time:06-17

I am fetch data from database but is data on view page data is print with prin_r but can't show in foreach loop please

the model you can check

    <?php
class Usermodel Extends CI_model{
    public function getUserdata() 
    {
        $this->load->database();
        // $q=$this->db->select('name');
        $q=$this->db->get('user');
        return $q->result_array();
    }
}
?>

and controller I m getting data from database

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_controller{
    public function User(){
        $this->load->model('Usermodel');
        $data['users']=$this->Usermodel->getUserdata();
        $this->load->view('Users/userlist',$data);

    }
}
?>

in view

<!DOCTYPE html>
<html lang="en">
<head>
    <title>User Details</title>
</head>
<body>
    <br>
    <?php print_r($users); ?>
    <h1>User Account Details</h1>
    <tr>
        <td>First Name</td>
        <td>Account No</td>
    </tr>
    <?php foreach($users as $users): ?>
    <tr>
        <td><?php $users['name']; ?></td>
        <td><?php $users['accountnumber']; ?></td>
    </tr>
    <?php endforeach ?>
</body>
</html>

CodePudding user response:

You must use either echo construct or short echo tag in your view. Short echo tag is more preferred as it is more concise. For example: <?= $users['name'] ?>

In your code you just return the value of variables to nowhere instead of printing it out so the result is not showing.

  • Related