Melvin Beverwijk
Melvin Beverwijk

Reputation: 63

I can't display my table (codeigniter)

I have searched this site for the answer, but all the answers i got didn't work.

MODEL:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_product extends CI_Model 
{
    function __construct()
    {
         // Initialization of class
        parent::__construct();
    }
    function viewauction()
    {
        $query = $this->db->select('*')->from('producten')->get();
        return $query->result();
    }
}

CONTROLLER:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller 
{
    function __construct()
    {
         // Initialization of class
        parent::__construct();
    }
    public function index()
    {
        $this->load->model('model_product');
        $data ['query'] = $this->model_product->viewauction();
        $this->load->view('header');
        $this->load->view('welcome_message', $data);
        $this->load->view('footer');
    }
}

VIEW:

<table class="table">
    <thead>
        <th>Naam</th>
        <th>Beschrijving</th>
        <th>Prijs</th>
        <th>Vooraad</th>
        <th>Categorie</th>
    </thead>
    <tbody>
        <?php foreach($query as $row): ?>
        <tr> 
        <td><?php echo $row->naam; ?></td>
            <td><?php echo $row->beschrijving; ?></td>
            <td><?php echo $row->prijs; ?></td>
            <td><?php echo $row->producten_op_voorraad; ?></td>
            <td><?php echo $row->categorie; ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

I get a empty output. no error's I have 1 item in the table.

To be sure here is the database file:

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

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '127.0.0.1';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'webshop';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Upvotes: 2

Views: 1548

Answers (3)

Federico J.
Federico J.

Reputation: 15922

I would attack the problem from the root up, and FIRST OF ALL, activate the debug database. As you're in localhost AND ONLY WHEN YOU'RE IN LOCALHOST, active the debug database from codeigniter: Change this line in your config dabase.php:

$db['default']['db_debug'] = TRUE;

And run it again, if there exists a database problem it'll arise.

If not, go checking where you have problems:

First: Make sure your query will take results: As @Shaiful Islam points out, first check what sql sentence you run, and if that gives you back result:

function viewauction()
{
   $query = $this->db->select('*')->from('producten')->get();

   $results = $query->result();

   echo $this->db->last_query();

   return $results;
}

This will give you back the SQL you're running, and you can run it directly and check if you're receiving results from the SQL.

Second: If you run the SQL and get data, print it in the controller to check if they work correctly:

public function index()
{
    $this->load->model('model_product');
    $data ['query'] = $this->model_product->viewauction();

    // Check the data:
    var_dump( $data['query'] );

    $this->load->view('header');
    $this->load->view('welcome_message', $data);
    $this->load->view('footer');
}

Third: If above went well, last thing is checking data in the view: In the first line of the file, write:

<?php var_dump($query); ?>

With all that, you should know where the data are missed, and with that, it'll be easy to know where the problem is.

Upvotes: 0

In your model, try this:

function viewauction()
{
    $query = $this->db->get('producten');
    return $query->result();
}

Note that $this->db->get('tablename') is the standard syntax of select * from table query in Codeogniter Active Records Class. Please check this out: https://ellislab.com/codeigniter/user-guide/database/active_record.html

Additionally, you can try this in your controller, to check whether all the data are coming from your query inside the array $data['query']:

$data ['query'] = $this->model_product->viewauction();
//do the following to check whether you got the data correctly from your query and inside the array.
$echo "<pre>";
print_r();
die();

Edit - 1:

If my solution doesn't work, then I'm pretty sure the problem is with your database config file. I've a few observations and questions:

  1. Why are you using the address '127.0.0.1' in your database default hostname? Isn't 'localhost' a better option?
  2. Why don't you have a username for your database? Usually in mysql the default username is root. I think you should set $db['default']['username'] = 'root';
  3. Is your database name correct? Is it really 'webshop'?

I'm pretty much sure the problem is somewhere out here. Please check.

Upvotes: 0

Saqueib
Saqueib

Reputation: 3520

it seems a type in table name make sure table with producten name is present in db

and your config is missing $db['default']['username'] = ''; you need to a username, generally it will be $db['default']['username'] = 'root'; on localhost

$query = $this->db->select('*')->from('producten')->get();

you can get result using just

$query = $this->db->get('producten');

Upvotes: 1

Related Questions