We help IT Professionals succeed at work.

Need help adding arrays

teedo757
teedo757 asked
on
290 Views
Last Modified: 2013-12-12
I am having trouble adding arrays. I cant seem to get the correct result. Here is my code.

$q=$_GET["itemnum"]; //Item number is passed in (integer)


**DB CONNECT**

//Get the Total item Quantity (Inventory amount) * I don' think this needs to be an array since there is only 1 item quantity
$sql="SELECT itemqty FROM item WHERE itemnum = '".$q."'";
$result = mysql_query($sql);
$totalqty = mysql_fetch_array($result);

//Get the number of units rented out (array)
$sql2="SELECT itemrentqty FROM orderitem where orderitem.itemnum = '".$q."'";
$result2 = mysql_query($sql2);
$rentedqty = mysql_fetch_array($result2);

//Get rows needed to be added
$numRows = count($rentedqty); // This does not work for some reason

Open in new window



Add all the rented units (Several customers may rent only a portion of inventory so this will add to get the total. variables are array so does not work yet.

//$i=1;
// while($i<=$numRows)
//  {
//  $rentedqty += $rentedqty[$i];
//  $i++;
//  }



// $availableqty=$totalqty - $rentedqty ; 

mysql_close($con);

?> 
<!-- <table> -->
<?
echo $availableqty; // get nothing
echo $totalqty;  // get array 
echo $rentedqty; // get array
?>

Open in new window



I want to get all the records from itemrentqty for a particular item and add them to get the total number of rented items. Then I want to subtract to the total itemqty to get any remaining inventory. I am having trouble getting the arrays into a single variable in order to do the math. Any help would be appreciated. Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
I don't think you understand what mysql_fetch_array does. It fetches one row from the database and in your case that array will have one item per row and the key to access it will be itemrentqty. So your code needs to be more like this

//Get the number of units rented out (array)
$sql2="SELECT itemrentqty FROM orderitem where orderitem.itemnum = '".$q."'";
$result2 = mysql_query($sql2);
while ( $row = mysql_fetch_array($result2) )
     $rentedqty += $row ['itemrentqty'];


See http://www.php.net/mysql_fetch_array for examples
mysql_fetch_array() returns an array of the next record in a result set. You will need to put it in a loop to create an array of arrays, which is what you seem to be expecting.

You can probably get mysql to do the summing for you. I'll post back shortly.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
So mysql_fetch_array can only handle one row in a database? How would you get multiple rows? Example I have 3 rows returned when I run:

//Get the number of units rented out (array)
$sql2="SELECT itemrentqty FROM orderitem where orderitem.itemnum = 1";

Open in new window


4
1
1
CERTIFIED EXPERT

Commented:
"How would you get multiple rows? "

You either iterate with a loop as I showed in my example above or you SUM in MySQL and return only one row with the summed result as per my other example.

Author

Commented:
Thanks for the help and the explanations.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
How would you get multiple rows?  Like this:

while ($row = mysql_fetch_assoc($result2))

Good news: The PHP man pages are online -- just a click away.
http://php.net/manual/en/control-structures.while.php

Why did you mark the grade down to a "B?"  What did we do wrong?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.