?
Solved

Need help adding arrays

Posted on 2011-04-19
8
Medium Priority
?
261 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
0
Comment
Question by:teedo757
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35425721
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
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 35425733
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1050 total points
ID: 35425743
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 450 total points
ID: 35425753
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
 
LVL 3

Author Comment

by:teedo757
ID: 35425806
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35425876
"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
 
LVL 3

Author Closing Comment

by:teedo757
ID: 35425947
Thanks for the help and the explanations.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35429352
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question