Connecting Tech Pros Worldwide Forums | Help | Site Map

need help, next record problem

Newbie
 
Join Date: Aug 2008
Posts: 8
#1: Aug 16 '08
Dear all..
I try to make an inventory control, with FIFO(first in first out) base, but I have a problem with the next record.
this is the code i make :


[PHP]$db = new clsDBMassimo;
$product_id = $Stock->product_id->GetValue();
$AmOrdered = $Stock->stock_out_unit->GetValue();
//check if the stock is enough
$sql = "SELECT SUM(stock_in_unit) as Total FROM stock_level WHERE product_id = $product_id";
$db->query($sql);
$result = $db->next_record();
if($result) ($Total = $db->f("Total"));
//if stock not enough, then cancel insert and give error message, but i still have an error
if($Total < $AmOrdered){
$Stock->Errors->addError("Stock Not Enough");=>in this part also have problem.event
$db->close(); though I already add an error, but stock out still inserted on table.any suggestion?
}else{ //if the stock is enough then select from table stock_level
$sql = "SELECT * FROM stock_level WHERE product_id = $product_id ORDER BY in_date";
$db->query($sql);
$result= $db->next_record();
$IDnya = $db->f("stock_level_id");
$AmOnHand = $db->f("stock_in_unit");

while($db->next_record()){
//loop as long as amount ordered > 0
while ($AmOrdered > 0){
if($AmOrdered < $AmOnHand){
$Res = $AmOnHand - $AmOrdered;
$update = "UPDATE stock_level SET stock_in_unit = $Res where stock_level_id = $IDnya";
$db->query($update);
$AmOrdered = 0;
}else{
$AmOrdered = $AmOrdered - $AmOnHand;
$update = "UPDATE stock_level SET stock_in_unit = 0 where stock_level_id = $IDnya";
$db->query($update);
//here should be move to the next record to get the next stock, but can not move to next record
}
}
}
$db->close();
} [/PHP]

Please give any suggestions, for the error I got or for the code structure. any helps are appreciated.
Thanks a lot

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Aug 16 '08

re: need help, next record problem


Let's see your next record function.

does it correctly? ie each time the while loop calls it, it returns the next record until empty?

try to put "echo" or "die('message'" statement to try to see how the code is executing (tracing the logic)

Let us know...



Dan
Newbie
 
Join Date: Aug 2008
Posts: 8
#3: Aug 16 '08

re: need help, next record problem


Hi
thanks for reply. I forget to attach the function. Here's the function :
[PHP]function next_record() {
if (!$this->Query_ID)
return 0;

$this->Record = @mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();

$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) {
$this->free_result();
}
return $stat;
}[/PHP]
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Aug 17 '08

re: need help, next record problem


I think the problem lies in your db class and or in the way you are using it.

Did you write that class yourself?

you're initializing $result twice, with the same thing function call. In this case, next_record(). This function returns a boolean if the next record exists.

For example if you have only one record, the second time you call it, it will return false.

I wish I could help you more, but you must debug this yourself. I'm not too sure of the problem your having either, you didn't describe the issue well enough.



Dan
Newbie
 
Join Date: Aug 2008
Posts: 8
#5: Aug 17 '08

re: need help, next record problem


Hi Dan..
Thank you for your reply. You're right, the problem is the way I use my class.
After few day stuck, finally i did it :)
Thank you
Reply