 |
มึนๆ กับ Fatal error: Call to a member function result_array() on a non-object |
|
 |
|
|
 |
 |
|
ติด Fatal error: Call to a member function result_array() on a non-object in C:\AppServ\www\minimart\application\models\reports\detailed_receives.php on line 28
ผมเอาโปรแกรมสำเร็จรูปมาปรับปรุงนะครับ คือ PHP Point Of Sale
และผมเพิ่มการทำรายงานเข้าไปแต่ติดตรงนี้ครับ
ไฟล์ detailed_receives.php บรรทัด 28 คือ
$data['summary'] = $this->db->get()->result_array();
Code (PHP)
<?php
require_once("report.php");
class Detailed_receives extends Report
{
function __construct()
{
parent::__construct();
}
public function getDataColumns()
{
return array('summary' => array($this->lang->line('reports_receive_id'), $this->lang->line('reports_date'), $this->lang->line('reports_items_purchased'), $this->lang->line('reports_sold_by'), $this->lang->line('reports_sold_to'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_profit'), $this->lang->line('reports_payment_type'), $this->lang->line('reports_comments')),
'details' => array($this->lang->line('reports_name'), $this->lang->line('reports_category'), $this->lang->line('reports_quantity_purchased'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_profit'),$this->lang->line('reports_discount'))
);
}
public function getData(array $inputs)
{
$this->db->select('receive_id, receive_date, sum(quantity_purchased) as items_purchased, CONCAT(employee.first_name," ",employee.last_name) as employee_name, CONCAT(supplier.first_name," ",supplier.last_name) as supplier_name, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit, payment_type, comment', false);
$this->db->from('receives_items_temp');
$this->db->join('people as employee', 'receives_items_temp.employee_id = employee.person_id');
$this->db->join('people as supplier', 'receives_items_temp.supplier_id = supplier.person_id', 'left');
$this->db->where('receive_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
$this->db->group_by('receive_id');
$this->db->order_by('receive_id');
$data = array();
<b>$data['summary'] = $this->db->get()->result_array();</b>
$data['details'] = array();
foreach($data['summary'] as $key=>$value)
{
$this->db->select('name, category, quantity_purchased, subtotal,total, tax, profit, discount_percent');
$this->db->from('receives_items_temp');
$this->db->join('items', 'receives_items_temp.item_id = items.item_id');
$this->db->where('receive_id = '.$value['receive_id']);
$data['details'][$key] = $this->db->get()->result_array();
}
return $data;
}
public function getSummaryData(array $inputs)
{
$this->db->select('sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit');
$this->db->from('receives_items_temp');
$this->db->where('receive_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
return $this->db->get()->row_array();
}
}
?>
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-05-25 17:40:00 |
By :
แมนนิดหน่อย |
View :
7872 |
Reply :
16 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีใครมีข้อแนะนำเลยหรอครับ.
|
 |
 |
 |
 |
Date :
2010-05-26 16:37:39 |
By :
mkamsoy0 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มี method result_array() ใน class
|
 |
 |
 |
 |
Date :
2010-05-26 23:00:46 |
By :
DownsTream |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัว Detailed_receives ผม copy มาจาก Detailed_sales ครับ (ซึ่ง Detailed_sales ทำได้ครับ)
แล้วก็ทำการเปลี่ยนจาก sale เป็น receive และเปลี่ยนจาก customer เป็น supplier นะครับ
แต่ผลที่ได้คือติด error ครับ..
งงๆ
|
 |
 |
 |
 |
Date :
2010-05-27 10:06:02 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดู Class Report หน่อยครับ
|
 |
 |
 |
 |
Date :
2010-05-28 03:07:33 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ report.php ครับผม
report.php
<?php
abstract class Report extends Model
{
function __construct()
{
parent::Model();
//Make sure the report is not cached by the browser
$this->output->set_header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
$this->output->set_header("Pragma: no-cache");
//Create our temp table to work with the data in our report
$this->Sale->create_sales_items_temp_table();
//Create our temp table to work with the data in our report // jeng add
$this->Receive->create_receives_items_temp_table(); // jeng add
}
//Returns the column names used for the report
public abstract function getDataColumns();
//Returns all the data to be populated into the report
public abstract function getData(array $inputs);
//Returns key=>value pairing of summary data for the report
public abstract function getSummaryData(array $inputs);
}
?>
|
 |
 |
 |
 |
Date :
2010-05-31 12:01:38 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ receive.php เป็นไฟล์ที่สร้าง table temp ครับ
ลักษณะตำแหน่งไฟล์ คือ
(โฟลเดอร์)model ---->(โฟลเดอร์) Report-->Detailed_receive.php
............................---->(โฟลเดอร์) Report-->report.php
............................---->receive.php
receive.php
<?php
class Receive extends Model
{
public function get_info($receive_id)
{
$this->db->from('receives');
$this->db->where('receive_id',$receive_id);
return $this->db->get();
}
function exists($receive_id)
{
$this->db->from('receives');
$this->db->where('receive_id',$receive_id);
$query = $this->db->get();
return ($query->num_rows()==1);
}
function save ($items,$supplier_id,$employee_id,$comment,$payment_type,$receive_id=false)
{
if(count($items)==0)
return -1;
$receives_data = array(
'supplier_id'=> $this->Customer->exists($supplier_id) ? $supplier_id : null,
'employee_id'=>$employee_id,
'payment_type'=>$payment_type,
'comment'=>$comment
);
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
$this->db->insert('receives',$receives_data);
$receive_id = $this->db->insert_id();
foreach($items as $item_id=>$item)
{
$cur_item_info = $this->Item->get_info($item_id);
$receives_items_data = array
(
'receive_id'=>$receive_id,
'item_id'=>$item_id,
'quantity_purchased'=>$item['quantity'],
'discount_percent'=>$item['discount'],
'item_cost_price' => $cur_item_info->cost_price,
'item_unit_price'=>$item['price']
);
$this->db->insert('receives_items',$receives_items_data);
//Update stock quantity
$item_data = array('quantity'=>$cur_item_info->quantity + $item['quantity']);
$this->Item->save($item_data,$item_id);
foreach($this->Item_taxes->get_info($item_id) as $row)
{
$this->db->insert('receives_items_taxes', array(
'receive_id' =>$receive_id,
'item_id' =>$item_id,
'name' =>$row['name'],
'percent' =>$row['percent']
));
}
}
$this->db->trans_complete();
return $receive_id;
}
function get_receive_items($receive_id)
{
$this->db->from('receives_items');
$this->db->where('receive_id',$receive_id);
return $this->db->get();
}
function get_supplier($receive_id)
{
$this->db->from('receives');
$this->db->where('receive_id',$receive_id);
return $this->Customer->get_info($this->db->get()->row()->supplier_id);
}
//We create a temp table that allows us to do easy report/receives queries
public function create_receives_items_temp_table()
{
$this->db->query("CREATE TEMPORARY TABLE ".$this->db->dbprefix('receives_items_temp')."
(SELECT date(receive_time) as receive_date, receive_id, comment,payment_type, customer_id, employee_id, item_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, SUM(percent) as item_tax_percent,
discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax,
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit
FROM ".$this->db->dbprefix('receives_items')."
INNER JOIN ".$this->db->dbprefix('receives')." USING (receive_id)
INNER JOIN ".$this->db->dbprefix('items')." USING (item_id)
LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'."
LEFT OUTER JOIN ".$this->db->dbprefix('receives_items_taxes')." USING (receive_id, item_id)
GROUP BY receive_id, item_id)");
//Update null item_tax_percents to be 0 instead of null
$this->db->where('item_tax_percent IS NULL');
$this->db->update('receives_items_temp', array('item_tax_percent' => 0));
//Update null tax to be 0 instead of null
$this->db->where('tax IS NULL');
$this->db->update('receives_items_temp', array('tax' => 0));
//Update null subtotals to be equal to the total as these don't have tax
$this->db->query('UPDATE '.$this->db->dbprefix('receives_items_temp'). ' SET total=subtotal WHERE total IS NULL');
}
}
?>
|
 |
 |
 |
 |
Date :
2010-05-31 12:07:31 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนเพิ่มบันทึกก็ทำได้นะครับ แต่ติดตรงออกรายงานครับ ติด error ที่ว่า
Fatal error: Call to a member function result_array() on a non-object in C:\AppServ\www\minimart\application\models\reports\detailed_receives.php on line 28
ไฟล์ detailed_receives.php บรรทัด 28 คือ
$data['summary'] = $this->db->get()->result_array();
ทั้งๆ ที่ผมก็เพิ่มเหมือนกันกับ sale ทุกอย่าง
โดยผมเปลี่ยนจาก sale เป็น receive และเปลี่ยน customer เป็น supplier ครับ
แนะนำด้วยครับ จะได้เรียนจบซะที..
|
 |
 |
 |
 |
Date :
2010-05-31 12:21:37 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมคิดว่าเกิดจากการ Queue ไม่ถูกต้องหรือเปล่าครับ..นี้คือ Queue ที่สร้างครับ
$this->db->select('receive_date, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax,sum(profit) as profit'); $this->db->from('receives_items_temp'); $this->db->group_by('receive_date'); $this->db->having('receive_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"'); $this->db->order_by('receive_date');
หรือผมหลงประเด็น
|
 |
 |
 |
 |
Date :
2010-05-31 23:27:39 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันต้องไล่โค้ดกันยาวเลย OOP ด้วย ต้องเห็นงานผมแก้ให้ได้แน่ กล้าให้ FTP ผมป่าวล่ะคับ นี่ msn: [email protected] add มาคุยได้คับ
|
 |
 |
 |
 |
Date :
2010-06-01 01:42:21 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวนี้คิดว่าน่าจะใช้ CI (CodeIgniter) เป็น frameworkครับ
จริงๆ statement ที่ error มันน่าจะจบแค่นี้นะครับ
$data['summary'] = $this->db->get();
เพราะด้านบนได้ define พารามีเตอร์การ get มาเรียบร้อยแล้ว
อย่างที่คุณว่ามา $data['summary'] = $this->db->get()->result_array(); เท่าที่เห็นยังไม่พบว่า เอา $this->db->get() มาใช้ในลักษณะนี้ได้ มีแต่เพียง $this->db->get('tablename') นั่นหมายถึง select * from tablename
|
 |
 |
 |
 |
Date :
2010-06-01 10:08:35 |
By :
pnbps |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอจะมีวิธีตรวจสอบ เช่นการ สร้างไฟล์ หรือ ฟอร์มขึ้นมาเพื่อตรวจสอบค่าได้บ้างไหมครับ?
หรือจะ echo ""; , print ""; ก็ได้ครับ
เพราะผมเข้าใจว่า ตัว $this->db นะมันไม่มีค่าก็เลย get() ไม่ได้ครับ
แต่จะตรวจสอบยังไงนี่สิครับ..
ปกติทำแต่ php ธรรมดา เจอ oop php นี่ก็ มึนๆ เหมือนกัน
ใครพอจะมีคำแนะนำไหมครับ?
|
 |
 |
 |
 |
Date :
2010-06-01 12:37:51 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมคิดว่าคงเป็นเหมือนที่คุณ "บุญพรหมสุข " พูดนะครับ ใช้ CI
ที่ผมคิดคือตัว table temp ที่ชื่อ 'receives_items_temp' ถูกต้องหรือเปล่า?
หรือผม join ผิด ....
หรือ เกี่ยวกับ function get_info และ function exists ใน receive.php
พอจะมีคำแนะนำบ้างไหมครับ
|
 |
 |
 |
 |
Date :
2010-06-01 12:51:44 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าคุณ mkamsoy หมายถึง echo ตัว sql ที่เพิ่งรันไปล่าสุด
ใช้ echo $this->db->last_query(); ใต้บรรทัดที่สั่ง run query ล่าสุดได้เลยครับ
แต่เท่าที่ผมดูตั้งแต่บรรทัดที่ 19-25 ใน method getData ดูเหมือนจะผิดหลักการ
ใช้ aggregate functionของ sql นะครับ เพราะคุณ group by ด้วย receive_id เพียงตัวเดียว
แต่ select ออกมาหลายฟิลด์ หากจะ select ออกมาหลายฟิลด์แล้ว group by ก็ต้อง group by
ให้หมด
ผมก็ใช้ CI อยู่บ้างเหมือนกันแต่ไม่ได้ใช้ $this->db->get() ผมถนัด $this->db->query() มากกว่า
เช่น
$sql="select * from table";
$query = $this->db-query($sql);
หรือหากผมต้องการ join กันหลาย table ผมจะใช้ ms access ช่วย join ให้ แล้ว sql จาก ms access
ไป run บน mysql หรือไม่ก็เอามา paste บน php สะดวกดี ไม่ต้องนั่งนับวงเล็บ join เอง, ms access ที่ว่าก็ลิ้งไปยัง mysql โดยผ่าน odbc
|
 |
 |
 |
 |
Date :
2010-06-01 13:35:18 |
By :
pnbps |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ผมมี group by ด้วย receive_id เพียงตัวเดียว เพราะต้องการให้ออกรายงานโดยถือ receive_id เป็นหลักครับ

ตัว pos เป็นเลขที่บิล Receive ครับ เวลากดแล้วก็จะแสดง ตัวใบเสร็จรับ(Receive) ครับ
|
 |
 |
 |
 |
Date :
2010-06-01 14:10:46 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้แก้ปัญหาได้แล้วครับ..
ขอบคุณสำหรับคำแนะนำของคุณ kerb ครับ ที่ช่วยจนผ่านไปได้
ปัญหาเกิดจากการ สร้าง table temp ผิดพลาดครับ
โดยเป็นการดึง ฟิลด์ที่ไม่มี (customer_id) และการ join กันของ supplier_id ครับ.
ต้องระบุ table item ให้กับ supplier_id ครับ ถึงทำได้
ถ้าเจอปัญหาเดียวกัน..
คำถามของผมอาจจะมีประโยชน์บ้าง..
ขอบคุณสำหรับความคิดเห็นครับ
|
 |
 |
 |
 |
Date :
2010-06-02 11:57:34 |
By :
แมนนิดหน่อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอเสริมนิดนึงครับ จาก error
ติด Fatal error: Call to a member function result_array() on a non-object in C:\AppServ\www\minimart\application\models\reports\detailed_receives.php on line 28
เนื่องจาก query ก่อนหน้าคำสั่ง $this->db->get()->result_array() เกิดerror
query error เพราะว่า ไม่มี table temp
ไม่มี table temp เพราะว่า code ตอนสร้าง table temp (แบบ temporary) error สาเหตุดังที่ คุณ No.15 บอกครับ
|
 |
 |
 |
 |
Date :
2010-06-02 22:30:41 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|