I am trying to get the sum of a column using PHP.
However this is my output:
Array ( [0] => Array ( [OrderPrice] => 5.99 ) )
Price: 0
Can someone please explain why I am not getting the sum, which should be 5.99 as shown above.
This is not a direct answer/solution to your immediate PHP problem, which some guru might address, but if you want to sum a SQL column, you would probably be better off doing this in SQL:
SELECT SUM(OrderPrice) AS order_price_sum FROM PizzaOrder
As you have an array of arrays, you need to sum up the OrderPrice elements of each sub array. So in PHP you would extract this column using array_column() and add this array instead...
echo array_sum(array_column($orderArr, 'OrderPrice'));
BUT, you should do this in MySQL, unless you also need the data for other purposes.
why you just edit your sql statement to get the sum
SELECT SUM(OrderPrice) FROM PizzaOrder
if you need the all values SELECT OrderPrice,SUM(OrderPrice) FROM PizzaOrder
and you can get the sum from the whole column.
Related
I'm using PHP but that's not important. I have a variable that contains a standard array of positive int IDs. And I want to do a SELECT query with this array against a MySQL table to find out what IDs are not already existing in the data table. My first thought was to use IN() but then I realized that doing it that way I can only get a list of IDs that do exist not ones that don't. Of course with a list of IDs that do exist, I could compile it into a second array and then use array_diff() but I can't help wondering if there's another way to do it.
I decided to use unset()
SELECT QUERY
where (row) {
unset(id)
}
Should be a nice easy one today but I can't find the answer anywhere.
I have a multidimensional array. It stores partid and quantity.
$pCombined[] = array ( $newName[$b], $newQty[$b] );
I want to use PHP join as suggested here: Passing an array to a query using a WHERE clause
I need to get more data from mySQL about the parts so I need the list of ids joined up as I don't like the idea of running one SQL query for each row.
I tried to use:
$ids = join(',', $pCombined);
That just gives array, array, array....e.t.c.
I tried a few obvious combinations of square brackets. What I guess I want is...
$ids = join(',', $pCombined[*][0]); (where * is all)
I think I might also be overcomplicating the whole thing and I could use foreach or a for loop and join manually I guess. Its not quite a basic shopping cart but the principals are similar. Any ideas how I can do this or perhaps push me in a different direction. Thanks.
to combine array elements can use the following
$elemens = array("one","two","there","four")
$exit = implode(',',$elements);
print_r($exit);//one,two,...
I hope you serve
I need some help here.
I have a mysql table called dictionary like this:
id word
1 test
2 hello
4 bye
7 werd
In php, I want to query the db so that I can get an array of the id field
so the array would list 1 2 4 7.
With this array, I want to run the array_rand function in php so I can get a random number from 1 2 4 7.
Can someone show me the proper mysql query to list the id field and return it into php as an array?
and how I could run that array with random array function.
No need shuffle this in php, - effective is to
use "SELECT id FROM table ORDER BY rand();"
than take all records and store id into array ...
Use the shuffle function
If you plan on using the random row and then querying for another, I would just randomize on the MySQL query. Get a count of the rows in the table, create a random integer somewhere between 0 and that count, and then use LIMIT in your MySQL query to return a random row.
If you plan on keeping it all in memory, then David's answer would work better.
I have a column of data returned from my database and I'd like to know how to get the sum for the entire column. Can someone tell me if there is a built in function for this?
123.00
12.00
1.00
-----
136.00 < this is what I need.
Please see the accompanying post for the mysql portion of my code.
How to get the total sum for a column
You can use the aggregate functions of your database SQL; something like SELECT SUM(column) FROM table;. This way you leave the data on the database server, saving bandwidth and php-computational resources.
in mysql you can return all this values in a sum() function, If important to you also all values and the total amount
if you store this in array
in php u can use array_sum
What do you mean ?
if you want to get sum through query
Go for SUM() in query using GROUP BY
SELECT SUM(`price`) FROM TABLE GROUP BY `item`;
or
use
array_sum() in php
I have an array like array ( array 0 ('item1'=>1,'item2'=>3))....etc like so.
And I want to access say the value of item2 but I don't want to use array[0]['item2']...Is there another way to access it? I just need 1 value so I don't think putting it in a foreach loop would be efficient..
Any ideas?
You have two choices: if you know the row and column you want to access, then you can access it directly. If you don't know the row and column, then you'd need to use a foreach loop to find the item you want.
You can only access it directly if you know where it is.
Assuming you meant array ( 0 => array ('item1'=>1,'item2'=>3)), you can use
array_values() to renumber the top level values. Then you know that the first value ('item1'=>1,'item2'=>3) will be indexed by the key 0.