Home > front end >  Variable contains data but it is not printed out
Variable contains data but it is not printed out

Time:06-20

I am fetching data from database and trying to display it in a view but it does not work. However, print_r outputs the data successfully.

Model:

<?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();
    }
}
?>

Controller:

<?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);

    }
}
?>

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.

CodePudding user response:

You should use echo .

For your code example write <td><?php echo $users['name']; ?></td>

  • Related