Welcome Guest, Not a member yet? Register   Sign In
Using HTML Table Class in a View
#1

[eluser]Unknown[/eluser]
Hi,

I'm new to Codeigniter and indeed the whole MVC structure, I'm learning fast but have come across an issue I just can't get my head round. Hoping someone can help!

My aim at the moment is a simple enquiry into a database which is then automatically turned into a table and presented via a view. I can pass the data to the View and present it via a Foreach loop without any problems (see below code which works). However, I've spent hours now trying to use the table->generate() approach via the View and I just can't work out whether I'm supposed to run the command in the Model and pass it to a variable which goes to the View (via the Controller) or run the command in the Controller and pass it to a variable. I've tried both with no success. The only way I can get the table->generate to work is by echoing it from the Model or Controller which seems messy and not best practice.

My code is below, at the moment it generates output via a Foreach, anyone who can show/tell me how to amend the code to use table->generate() properly is officially a legend!

Thanks

Tom

Controller:
Code:
<?php

class displayrounds extends CI_Controller {

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

Public Function Index()

{
$this->load->model('getallmodel',"",TRUE);

$data['query'] = $this->getallmodel->allentries();

$this->load->view('tableview',$data);
}
    
}

?>

Model:
Code:
<?php

Class getallmodel extends CI_Model {
    
function __construct()  
{  
    parent::__construct();  
}

Function allentries()
{
    $query = $this->db->get('Test');
    return $query->result();    
}    
    
    
}

?>

View:
Code:
<html>
<head></head>
<body>

<?php
Foreach ($query as $row) {
    print $row->field1;
    print $row->field2;
    print $row->field3;
    print "<br>";
}
//$this->load->library('table');
//echo $this->table->generate($query);
?&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]LuckyFella73[/eluser]
Just guessing - but try that as your model-code:

Code:
&lt;?php

Class getallmodel extends CI_Model {
    
function __construct()  
{  
    parent::__construct();  
}

Function allentries()
{
    $query = $this->db->get('Test');
    return $query; // CHANGE HERE
}    
    
    
}

?&gt;
#3

[eluser]Unknown[/eluser]
After all that I can't believe it was something so simple!

Thank you very much indeed!
#4

[eluser]LuckyFella73[/eluser]
Glad it works now!




Theme © iAndrew 2016 - Forum software by © MyBB