• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Need help adding arrays

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
0
teedo757
Asked:
teedo757
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
Beverley PortlockCommented:
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
0
 
ollyatstithiansCommented:
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.
0
 
Ray PaseurCommented:
For starters you need to know if your queries worked.

$result2 = mysql_query($sql2);
if ($result2 === FALSE) die( mysql_error() );

Then once you know that worked, you can use the while() iterator to return each row of the results set.

while ($row = mysql_fetch_assoc($result2))
{
    $rentedqty += $row["itemrentqty"];
}

You might enjoy this book:
http://www.sitepoint.com/books/phpmysql4/

HTH, ~Ray
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Beverley PortlockCommented:
On further reflection, I would get MySQL to do the work

//Get the number of units rented out (array)
$sql2="SELECT SUM( itemrentqty ) AS totalRentQty FROM orderitem where orderitem.itemnum = '".$q."'";
$result2 = mysql_query($sql2);
$row = mysql_fetch_array($result2);
$rentedqty = $row['totalRentQty'];
0
 
teedo757Author 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
0
 
Beverley PortlockCommented:
"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.
0
 
teedo757Author Commented:
Thanks for the help and the explanations.
0
 
Ray PaseurCommented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now