Piglet Piglet - 3 months ago 17
MySQL Question

Codeigniter PHP pulling records from mysql database table to display on page

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

}
}
?>

Answer

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 } ?>
Comments