Undefined $db - El Forum - 02-05-2012
[eluser]akien[/eluser]
I have a simple code where I will display the record in my database but I have an error of
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined property: Report_model::$db
Filename: models/report_model.php
Line Number: 11
This is my code for the model:
Code: <?
class Report_model extends Model
{
function report_model()
{
parent :: Model();
}
function restaurant_master_getall()
{
$query = $this->db->query("SELECT restaurant_master.code, restaurant_master.name,
SUM(CASE WHEN mds_orders.GrossTotal < '100' THEN mds_orders.GrossTotal END) AS 'less100',
SUM(CASE WHEN mds_orders.GrossTotal >= '100' AND mds_orders.GrossTotal < '200' THEN mds_orders.GrossTotal END) AS 'less200',
SUM(CASE WHEN mds_orders.GrossTotal >= '200' AND mds_orders.GrossTotal < '300' THEN mds_orders.GrossTotal END) AS 'less300',
SUM(CASE WHEN mds_orders.GrossTotal >= '300' AND mds_orders.GrossTotal < '400' THEN mds_orders.GrossTotal END) AS 'less400',
SUM(CASE WHEN mds_orders.GrossTotal >= '400' AND mds_orders.GrossTotal < '500' THEN mds_orders.GrossTotal END) AS 'less500',
SUM(CASE WHEN mds_orders.GrossTotal >= '500' AND mds_orders.GrossTotal < '600' THEN mds_orders.GrossTotal END) AS 'less600',
SUM(CASE WHEN mds_orders.GrossTotal >= '600' AND mds_orders.GrossTotal < '700' THEN mds_orders.GrossTotal END) AS 'less700',
SUM(CASE WHEN mds_orders.GrossTotal >= '700' AND mds_orders.GrossTotal < '800' THEN mds_orders.GrossTotal END) AS 'less800',
SUM(CASE WHEN mds_orders.GrossTotal >= '800' AND mds_orders.GrossTotal < '900' THEN mds_orders.GrossTotal END) AS 'less900',
SUM(CASE WHEN mds_orders.GrossTotal >= '900' AND mds_orders.GrossTotal < '1000' THEN mds_orders.GrossTotal END) AS 'less1k',
SUM(CASE WHEN mds_orders.GrossTotal >= '1000' THEN mds_orders.GrossTotal END) AS 'gtr1k',
SUM(mds_orders.GrossTotal) AS 'total'
FROM mds_orders
JOIN restaurant_master
ON mds_orders.RestaurantID = restaurant_master.PKID
WHERE mds_orders.OrderDate BETWEEN '2011-12-01 00:00:00' AND '2011-12-02 23:59:59'
GROUP BY restaurant_master.code, restaurant_master.name");
return $query;
}
}
?>
This is my code for the controller:
Code: <?php
class Report extends Controller
{
function report()
{
parent :: Controller();
}
public function GetAll()
{
$this->load->model('report_model');
$data['query'] = $this->report_model->restaurant_master_getall();
$this->load->view('report_view', $data);
}
}
?>
This is my code for the view:
Code: <html>
<head>
</head>
<body>
<h1>Sales Per Sales Frequency Bracket</h1>
<form name="fetching" method="POST" action=""></p>
<b>Date From:</b> <br><input type="text" name="dfrom" id="dfrom"></p>
<b>Date To:</b> <br><input type="text" name="dto" id="dto"><br /><br />
<input name="submit" type="submit" value="Go">
</form>
<table border="2" cellpadding="2" cellspacing="1">
<tr>
<th>Restaurant Code</th>
<th>Restaurant Name</th>
<th>100 & Below</th>
<th>101-200</th>
<th>201-300</th>
<th>301-400</th>
<th>401-500</th>
<th>501-600</th>
<th>601-700</th>
<th>701-800</th>
<th>801-900</th>
<th>901-1000</th>
<th>1001 & Above</th>
<th>Total Gross Sales</th>
</tr>
<?php
if($query->num_rows() > 0):
foreach ($query->result() as $row):
?>
<tr>
<td><?php echo $row->code; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->less100; ?></td>
<td><?php echo $row->less200; ?></td>
<td><?php echo $row->less300; ?></td>
<td><?php echo $row->less400; ?></td>
<td><?php echo $row->less500; ?></td>
<td><?php echo $row->less600; ?></td>
<td><?php echo $row->less700; ?></td>
<td><?php echo $row->less800; ?></td>
<td><?php echo $row->less900; ?></td>
<td><?php echo $row->less1k; ?></td>
<td><?php echo $row->gtr1k; ?></td>
<td><?php echo $row->total; ?></td>
</tr>
<?php
endforeach;
endif;
?>
</table>
</body>
</html>
Can someone tell me what is wrong with this? What causes the error?
Undefined $db - El Forum - 02-05-2012
[eluser]InsiteFX[/eluser]
look at the top of your model the php tag <? is wrong <?php!
Also make sure that you have loaded the database...
Also do not use the ending php tag just place a comment
Undefined $db - El Forum - 02-05-2012
[eluser]pbflash[/eluser]
You need to load the database in your model.
$this->load->database()
Or you could autoload it.
http://ellislab.com/codeigniter/user-guide/database/connecting.html
Undefined $db - El Forum - 02-05-2012
[eluser]akien[/eluser]
InsiteFX & pbflash: Thanks for the help.
I have another two questions regarding my code.
1. How can I declare a variable in Code Igniter? In my query the date there will be replace by a variable $date_from and $date_to. Is this kind of declaration acceptable?
Code: $date_from = $_POST['dfrom']
2. I have a submit button. When it is clicked the result will display. In my php code I put it like this.
Code: if(isset($_POST['submit']))
{
//condition
}
Did it work in code igniter? Where I can insert it.
--
Sorry for my questions. I'm a student and I'm new about Code Igniter. Thanks. Best Regards.
Undefined $db - El Forum - 02-05-2012
[eluser]InsiteFX[/eluser]
Code: // declare a veriable
public $test;
// you should be using ci input
if(isset($this->input->post('submit', TRUE))) // TRUE invokes the ci xss_clean for protection.
{
//condition
}
Undefined $db - El Forum - 02-05-2012
[eluser]akien[/eluser]
I did your advice but it display nothing but anyway I already solve my problem by doing the simple.
Code: if(isset($_POST['submit'])):
//condition
endif;
And I declare my variable with.
Code: $date_from = $this->input->post('dfrom')
Thanks a lot InsiteFX. Your help did something big on my code. Thanks again.
|