Jump to content

Doing math within an array


phpstuck

Recommended Posts

I am trying to do a calculation within an array. I finally have the array printing the words I need but the math will just not work out... I either get all zeros for amount needed or I get all 5's for amount needed... I need the right answer though...LOL

 

I am pulling data from two tables. It prints a list of items that are below the set minimums. The quantity on hand is on one table and the mimimum allowed is in another table along with another field that instructs how many over min. to buy when the min. is reached.

 

Simple math would say

 

$need = (($min + $tobuy) - ($quant));

 

However this is in an array and I am missing something, somewhere.... My Code as follows:

 

<?php

include_once 'db.php';


echo "<hr>";
$deflist=mysql_query(        
"SELECT l.groccat, l.quant, b.grocname, b.min, b.tobuy FROM inven l,groc b WHERE l.groccat = b.grocname");
while ($all = mysql_fetch_array($deflist)) {
   $results[$all['grocname']][] = array ('quant' => $all['quant'], 'min' => $all['min'], 'groccat' => $all['groccat'], 'tobuy' => 

$all['tobuy']);



}

foreach ($results as $groName => $groData)



IF ($itemData['quant'] < $itemData['min']){

$need = (($itemData['tobuy'] + $itemData['min']) - $itemData['quant']);

Echo "You need " . $need ." - " . $groName ."<br>";



}ELSE{

{


foreach ($groData as $itemNum => $itemData)

{





}


}

}





?>






Link to comment
Share on other sites

Just under }ELSE{ is the $itemData reference

 

Which brings up another interesting point... I don't really want the ELSE statement, but it fails completely without it.

 

arrays are somewhat new to me when calculating from them. I have researched the world over and can't seem to find anything that applies to this problem.

 

Sorry about the indents, this code has been worked and reworked so many times just trying to get it to work, I'm half way there, at least it prints from the database now in relation to the items under min.

 

Thanks for the heads up on the <?php highlighting.

 

<?php

include_once 'db.php';


echo "<hr>";
$deflist=mysql_query(        
"SELECT l.groccat, l.quant, b.grocname, b.min, b.tobuy FROM inven l,groc b WHERE l.groccat = b.grocname");
while ($all = mysql_fetch_array($deflist)) {
   $results[$all['grocname']][] = array ('quant' => $all['quant'], 'min' => $all['min'], 'groccat' => $all['groccat'], 'tobuy' => 

$all['tobuy']);



}

foreach ($results as $groName => $groData)



IF ($groName['quant'] < $groName['min']){


Echo "You need " . (($itemData['tobuy'] + $itemData['min']) - ($itemData['quant'])) ." - " . $groName ."<br>";




{


foreach ($groData as $itemNum => $itemData)

//Echo "You need " . (($itemData['tobuy'] + $itemData['min']) - ($itemData['quant'])) ." - " . $groName ."<br>";


{





}


}

}





?>






Link to comment
Share on other sites

Thanks for asking me to look at that again... I realize where that is... But if I move anything at all it won't return anything to the screen. I'm here asking for help after researching this for several days. This is my first attempt at trying to return these results and adding an IF statement, I have been all over the PHP manual and searching the internet high and low and coming up empty. Maybe I'm using the wrong kind of query and I'll never get to point B... I don't know.

 

I used this same query and produce results for other types of problems within my application, but this one has the MATH and IF involved.

 

Maybe I'll try explaining a little better what I'm trying to do and you'll be able to point me to a place where I can get a little help constructing it. I'm not asking you to write it, but maybe a little more in the line of a complete answer and I'd be on my way to learning something.

 

I have inventory of groceries and supplies in the first table. People use a barcode scanner to keep track of thing that are on hand, when they take one out of inventory they scan it and it is removed from the database, one field is GROCCAT thats where they link all the different brands of a similar product (say for instance canned corn 10 oz.) the GROCCAT will be "canned corn" for all brands. The second table is where all the GROCCATS are created and stored under the name GROCNAME, in the second table along with GROCNAME there is a field called MIN where the user decides how many canned corn should be on hand as a minimum (for example 36) when the minumum is reached in the main inventory a trigger to buy is set, there is one more field called TOBUY, when the minimum is reached the TOBUY field says how many to buy at that point.

 

So I am trying to build a query where it pulls from both tables and matches up items based on GROCCAT = GROCNAME then it adds together all the different brands as a total and checks to see if they are below the set minimum... Thus the IF statement and if they are below minimum a echo is sent to buy canned corn add together the min + tobuy and print that to the screen.

 

Somewhere along the way I think I am trying to use the wrong query or results return to make this work...

 

Please if you have any constructive ideas let me know where to look for help answering this question.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.