2

So it appears I'm in need of some more assistance, I'm trying get records from my database to show on my view page as row descending down for each value but I am just getting HTTP 500 errors not sure where I have went wrong.

ci_users - database schema

id(PK, int, not null)
user_name(nchar255, not null)
user_email(nchar255, not null)
user_password(nchar255, not null)
user_displayname(nchar255, not null)
user_active(smallint, not null)
user_level(smallint, not null)

userlist_view

<table cellSpacing="0" cellPadding="4" width="100%" border="0">

                <tr bgColor="#a5a6a9">
                    <td width="20%" align="left"><b>User ID</b></td>
                    <td width="20%" align="left"><b>Username</b></td>
                    <td width="20%" align="center"><b>Email Address</b></td>
                    <td width="20%" align="center"><b>Displayname</b></td>
                    <td width="20%" align="center"><b>User Level</b></td>
                </tr>

                <?php
                    foreach ($result as $result) ?>

                        <tr>
                            <td width="20%" align="left"><?php echo $result[0]->id; ?></td>
                            <td width="20%" align="left"><?php echo $result[0]->user_name; ?></td>
                            <td width="20%" align="left"><?php echo $result[0]->user_email; ?></td>
                            <td width="20%" align="left"><?php echo $result[0]->user_displayname; ?></td>
                            <td width="20%" align="left"><?php echo $result[0]->user_level; ?></td>


                <?php endforeach; ?>
            </table>

userlist controller

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class userlist extends CI_Controller {

    public function __construct(){
        parent::__construct();
    }

    function index()
        {
            $this->load->view('userlist_view');
        }

    function records()
       {
        $data   = array();
        $this->load->model('userlist_database');
        $result = $this->userlist_database->getUsers();
        $this->load->view('userlist_view', $result);
    }

    function logout()
    {
        $this->session->sess_destroy();
        $this->index();
    }

}
?>

userlist_database model

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class userlist_database extends CI_Model{

      function __construct()
        {
            // Call the Model constructor
            parent::__construct();
            $this->load->database();
        }

    function getUsers() {
        $this->db->select('id','user_name','user_email','user_displayname','user_level');
        $this->db->from('ci_users');
        $query = $this->db->get();
        return $result = $query->result();

    }
}
?>

1 Answer 1

1

Missing </tr> at the foreach loop and improved

In Model

function getUsers() {
    $this->db->select('id','user_name','user_email','user_displayname','user_level');
    $this->db->from('ci_users');
    $query = $this->db->get();
    return $result = $query->result_array();

}

In Controller

$data['result'] = $this->userlist_database->getUsers();
$this->load->view('userlist_view', $data);

In View

<?php
    foreach ($result as $item)
    {

        ?>

        <tr>
            <td width="20%" align="left"><?php echo $item['id']; ?></td>
            <td width="20%" align="left"><?php echo $item['user_name']; ?></td>
            <td width="20%" align="left"><?php echo $item['user_email']; ?></td>
            <td width="20%" align="left"><?php echo $item['user_displayname']; ?></td>
            <td width="20%" align="left"><?php echo $item['user_level']; ?></td>

         </tr>

<?php } ?>
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks dude thats it up and running though I'm getting this little error now A PHP Error was encountered Severity: Notice Message: Undefined index: user_name Filename: views/userlist_view.php Line Number: 100
@Piglet check DB have filed call user_name
Hey, checked DB and they all have entries in them ..weird, id is there just everything else is not populating
the field 'id' is the only one displaying data 'user_name' 'user_email' etc have that error message and are not displaying any data
print_r($data['result']);die; add this in controller before view and post the data
|

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.