OK guys.. My problem is I want to create a loop on my model in a bootstrap HTML table. The looped data should look like this in HTML table:
Here's my model:
<?php class Dash_model extends CI_Model {
public function __construct()
{
parent::__construct();
// Loading second db and running query.
$CI = &get_instance();
//setting the second parameter to TRUE (Boolean) the function will return the database object.
$this->db2 = $CI->load->database('db2', TRUE);
}
public function itemboughttotal()
{
$query = $this->db->query("
SELECT namecust as Name, COUNT(namecust) as itembought
FROM db1..Table_1
INNER JOIN db2..Table_2
ON db2..Table_2.ID = db1.Table_1.CustID
INNER JOIN db2..Table_3
ON Table_3.TransID = Table_2.TransID
WHERE rsp_code = '11'
GROUP BY namecust
ORDER BY namecust
");
return $query->result();
}
public function itemsoldtotal()
{
$query = $this->db->query("
SELECT namecust as Name, COUNT(namecust) as itemsold
FROM db1..Table_1
INNER JOIN db2..Table_2
ON db2..Table_2.ID = db1.Table_1.CustID
INNER JOIN db2..Table_3
ON Table_3.TransID = Table_2.TransID
WHERE rsp_code = '22'
GROUP BY namecust
ORDER BY namecust
");
return $query->result();
}
public function transsuccesstotal()
{
$query = $this->db->query("
SELECT namecust as Name, COUNT(namecust) as TransSuccess
FROM db1..Table_1
INNER JOIN db2..Table_2
ON db2..Table_2.ID = db1.Table_1.CustID
INNER JOIN db2..Table_3
ON Table_3.TransID = Table_2.TransID
WHERE trans_code = '100'
GROUP BY namecust
ORDER BY namecust
");
return $query->result();
}
public function transfailedtotal()
{
$query = $this->db->query("
SELECT namecust as Name, COUNT(namecust) as TransFail
FROM db1..Table_1
INNER JOIN db2..Table_2
ON db2..Table_2.ID = db1.Table_1.CustID
INNER JOIN db2..Table_3
ON Table_3.TransID = Table_2.TransID
WHERE trans_code = '200'
GROUP BY namecust
ORDER BY namecust
");
return $query->result();
}
}
It's not the actual query, but it looks just like my query and it works perfectly when I test it on my SQL Server. So, there's nothing wrong with my query. Next I pass the query result into my controller like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('dash_model');
$this->load->library('table');
}
public function index()
{
$tmpl = array (
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
);
$this->table->set_template($tmpl);
$data['resultitembought'] = $this->dash_model->itemboughttotal();
$data['resultitemsold'] = $this->dash_model->itemsoldtotal();
$data['resulttranssuccess'] = $this->dash_model->transsuccesstotal();
$data['resulttransfailed'] = $this->dash_model->transfailedtotal();
$this->load->view('dashboard', $data);
}
}
And then I pass th $data into my VIEW like this:
<tbody>
<?php foreach (array_merge($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed) as $row)
{ ;
?>
<tr>
<td><?php echo $row->Name; ?></td>
<td><?php echo $row->itembought; ?></td>
<td><?php echo $row->itemsold; ?></td>
<td><?php echo $row->TransSuccess; ?></td>
<td><?php echo $row->TransFail; ?></td>
</tr>
<?php } ?>
</tbody>
After that, I can only loop the 'Name' and the 'Item Bought' data. Anything else is just a complete error. And here's the error I got:
ERROR ON ITEM SOLD:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$itemsold
Filename: views/dashboard.php
Line Number: 302
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 302
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
ERROR ON TRANSACTION SUCCESS:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$TransSuccess
Filename: views/dashboard.php
Line Number: 303
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 303
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
ERROR ON TRANSACTION FAILURE
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$TransFail
Filename: views/dashboard.php
Line Number: 304
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 304
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
I got a tip from someone that the error was happened because I failed to call the $value of other function in my controller.
I tried to remove the $resultitemsold, $resulttranssuccess, and $resulttransfail on my controller, leaving only the name and the itembought, and it works just fine.
Does this means that Codeigniter won't allow me to put more than 2 value to put in the View on the index function? I don't know about this, please help me, guys...
Thank you for your help...
. .
.
UPDATE
Thank for all of your answers, but I still got the error. Let me show what the error look like:
So, as you can see. It actually looped, but why I still got the error.
When the value of the 'itemsold' looped, it looped the array result, but error on the 'namecust' and the 'itembought'.
A PHP Error was encountered
Severity: Notice
Message: Undefined index: itemsold
Filename: views/dashboard.php
Line Number: 302
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 302
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once