need help, next record problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doxtor
    New Member
    • Aug 2008
    • 8

    need help, next record problem

    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_un it->GetValue();
    //check if the stock is enough
    $sql = "SELECT SUM(stock_in_un it) 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("Stoc k 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_un it");

    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
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    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('messa ge'" statement to try to see how the code is executing (tracing the logic)

    Let us know...



    Dan

    Comment

    • doxtor
      New Member
      • Aug 2008
      • 8

      #3
      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_ar ray($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]

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        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

        Comment

        • doxtor
          New Member
          • Aug 2008
          • 8

          #5
          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

          Comment

          Working...