Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    how to delete a certain number of item from a numbered quantity

    i am working on a test system which is an php built inventory system. and i stumbled on this problem since then and has not made any progress.

    so i have a table of batched products :

    Code:
    batch 1 - product 1 = 30 pcs
    batch 2 - product 1 = 60 pcs
    and i need to find a way to deduct 40 pcs from batch 1 and my system will
    then find batch 2 and deduct the remaining 10 pcs to complete the process.


    anyone has an idea on how to implement this? any idea is welcomed.

    regards,
    randomIT

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,195
    Thanks
    23
    Thanked 605 Times in 604 Posts
    It looks like your DB has a "batch" number that tells us what "product" was made and how many pieces of that product. At this point I have to assume there is a product 2 and product 3 also. With that assumption the "products" have to be column headings and the amount of each product is tracked there.

    I made a table called prodbatch; looks a little like this:
    Run_number | product_1 | product_2 | product_3
    batch_1 | 30 | 0 | 0
    batch_2 | 60 | 0 | 0
    batch_3 | 0 | 0 | 40
    batch_4 | 0 | 70 | 0
    batch_5 | 0 | 0 | 80

    In the code I do a check for not have the amount you want to remove. I left some echoes in so you would know that it's working. You might want to add something to also call for other products beside product_1.

    The amount removed is set in the program as $removeThisAmt. The trick is setting the return from the select to another array: $TempRow[$j] = $row[0];
    See what you think:
    Code:
    <?php
    require ('./inc/test_connect.php');
    $removeThisAmt = 80;
    $query = "SELECT Run_number, product_1 FROM prodbatch WHERE product_1 != 0";
    $result = mysql_query($query);
    
    $j = 0;
    $i = 1;
    while($row = mysql_fetch_row($result))
    {
    	$TempRow[$j] = $row[0];
    	$TempRow[$i] = $row[1];
    	$j = $j + 2;
    	$i = $i + 2;
    }
    if(array_sum($TempRow) < $removeThisAmt){echo "We don't have that amount of product. We have ".array_sum($TempRow)." pieces. We need to run another batch!";die;}
    
    $j = 0;
    $i = 1;
    DO{
    	if($TempRow[$i] >= $removeThisAmt){
    		$final = $TempRow[$i] - $removeThisAmt;
    		$removeThisAmt = 0;
    		$query = "UPDATE prodbatch set product_1 = $final where Run_number = '$TempRow[$j]' and product_1 = $TempRow[$i]";
    		echo $query.'<br />';
    		$result = mysql_query($query);
    		$j = $j + 2;
    		$i = $i + 2;
    
    	}else{
    		$final = $removeThisAmt - $TempRow[$i];
    		$removeThisAmt = $final;
    		$query = "UPDATE prodbatch set product_1 = 0 where Run_number = '$TempRow[$j]' and product_1 = $TempRow[$i]";
    		echo $query.'<br />';
    		$result = mysql_query($query);
    		$j = $j + 2;
    		$i = $i + 2;
    	}
    }while($removeThisAmt > 0);
    ?>

  • Users who have thanked sunfighter for this post:

    randomIT (10-05-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    will try this one as soon as i get free time, pretty much caught up with studies..

    i will definitely review this code and try to implement it!

    thank you very much, sunfighter!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •