Get total from mysql fields - php

I have prices set up in a simple mysql table and I would like to output the total price of the "services" they selected. I was able to achieve this using Laravel 4 however only way I knew how to do it was using a foreach loop. But that returns all the records that have a service and then adds them as each row is returned. How can I get just the last row returned in the foreachloop? Or is their a better method?
Foreach
#foreach ($serviceSummary as $services)
<tr><td>Total:</td><td>{{{ $zero += $services->price }}}</td></tr>
#endforeach
Var Dump from table
var_dump($serviceSummary); die;
array(2) {
[0]=>
object(stdClass)#211 (9) {
["id"]=>
string(1) "1"
["userID"]=>
string(1) "1"
["services"]=>
string(1) "1"
["price"]=>
string(4) "8.95"
["created_at"]=>
string(19) "2013-10-08 19:55:47"
["updated_at"]=>
string(19) "2013-10-08 19:55:47"
["service"]=>
string(20) "Service"
["count"]=>
string(1) "1"
["label"]=>
string(2) "RC"
}
[1]=>
object(stdClass)#212 (9) {
["id"]=>
string(1) "1"
["userID"]=>
string(1) "1"
["services"]=>
string(1) "1"
["price"]=>
string(4) "8.95"
["created_at"]=>
string(19) "2013-10-08 20:38:56"
["updated_at"]=>
string(19) "2013-10-08 20:38:56"
["service"]=>
string(20) "Service"
["count"]=>
string(1) "1"
["label"]=>
string(2) "RC"
}
}
basically what it looks like when printed is:
Total: 8.95
Total: 17.9
I obviously would like it to be just the total 17.9

Wouldn't Laravel's query builder function sum() do the trick?
In your controller:
$totalCost = DB::table('tableName')
->sum('price');
return View::make('your.view', array('totalCost' => $totalCost));
And then in your blade template:
Total: {{{ $totalCost }}}

Related

passing array into view (codeigniter )

array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);

how do I extract a value from a JSON object Array of Arrays

I am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}

Codeigniter Pass the serial number of current image

I am using codeigniter. I have retrieved a array from my database. I need to pass the current number (Serial number) of image into the view so that I can display it in there.
Say, in the first page, I need to pass #1 so that it would let the user know that it is the first image. And say if they are visiting the 3rd image (array[2]) then it should display #3 .
Basically what I have is the option for the users to click next #4 and #2 previous to go through the images and I want to display the serial number of the current image in the page.
array(5) {
[0]=>
object(stdClass)#21 (6) {
["id"]=>
string(2) "17"
["gallery_id"]=>
string(1) "5"
["title"]=>
string(6) "sample"
["filename"]=>
string(12) "n9a67681.jpg"
["description"]=>
string(10) "sdhdfhafdj"
["created"]=>
string(19) "2013-06-29 08:25:56"
}
[1]=>
object(stdClass)#22 (6) {
["id"]=>
string(2) "18"
["gallery_id"]=>
string(1) "5"
["title"]=>
string(6) "sample"
["filename"]=>
string(13) "n9a676811.jpg"
["description"]=>
string(10) "sdhdfhafdj"
["created"]=>
string(19) "2013-06-29 08:26:28"
}
[2]=>
object(stdClass)#23 (6) {
["id"]=>
string(2) "19"
["gallery_id"]=>
string(1) "5"
["title"]=>
string(6) "sample"
["filename"]=>
string(13) "n9a676812.jpg"
["description"]=>
string(10) "sdhdfhafdj"
["created"]=>
string(19) "2013-06-29 08:27:04"
}
[3]=>
object(stdClass)#24 (6) {
["id"]=>
string(2) "20"
["gallery_id"]=>
string(1) "5"
["title"]=>
string(7) "safdhfh"
["filename"]=>
string(35) "tumblr_mfyn3l81ft1qkfae2o1_1280.jpg"
["description"]=>
string(4) "dsgd"
["created"]=>
string(19) "2013-06-29 08:28:16"
}
[4]=>
object(stdClass)#25 (6) {
["id"]=>
string(2) "21"
["gallery_id"]=>
string(1) "5"
["title"]=>
string(8) "dshfdhsd"
["filename"]=>
string(36) "tumblr_mfyn3l81ft1qkfae2o1_12801.jpg"
["description"]=>
string(6) "dahadf"
["created"]=>
string(19) "2013-06-29 08:29:35"
}
}
Controller:
$this->load->library('pagination');
$config['base_url'] = example.com/controller/action/uri_segment;
$config['total_rows'] = 5;
$config['per_page'] = 1;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$position = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();
$data['image'] = your_array[$position];
$this->load->view('your_view', $data);
Put in your view file "echo $links" (to create the next and previous link), and work as you want with your image informations.

PHP Arrays gatther arrays from previous post

Using WP I'm building the breakdown of a score. Each breakdown is a custom select field with values from 1 to 10. The final result i'm trying to achieve is to show the total score inside the WP loop, by calculating the sum of all breakdown scores and dividing it by the number of added breakdown scores.
The problem is that each post i add with the total score it taking the array from a previous posts also. For example:
Post 1
array(6) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" }
Post 2
array(9) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" }
Post 3
array(13) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" [9]=> array(0) { } [10]=> string(1) "9" [11]=> string(2) "10" [12]=> string(1) "3" }
As you can see Post 2 got it's own array and the array from Post 1, Post 3 got it's own array and arrays from Post 1 and Post 2.
This is the code i'm using inside the WP while loop
<?php
$score_rows = get_post_meta($post->ID, 'review_score', true);
$score[] = array();
foreach( $score_rows as $row ):
$score[] = $row['score_number'];
endforeach;
$score_items = count( $score );
$score_divide = $score_items;
$score_sum = array_sum( $score );
$score_total = $score_sum / $score_divide;
?>
This code works great in single.php where i have only one post, but what to do where i have more then one? How to prevent from posts to combine arrays like this?
UPDATE
I have found that all the values are strings, maybe this is what causing it act like this. Is it possible to convert the values to integers?
change
$score[] = array();
to
$score = array();

removing data from array

I have a problem i need to ignore any array key if it has "lastpost" set to "No Posts"
This is the array dump of $sublast:
array(3) { [0]=> array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=> string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" } [1]=> array(9) { ["forum_category_id"]=> string(1) "7" ["name"]=> string(16) "Test sub Sub cat" ["description"]=> string(0) "" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:42 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=7&forum_post_id=63" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_7" } [2]=> array(9) { ["forum_category_id"]=> string(1) "9" ["name"]=> string(11) "Test cat #3" ["description"]=> string(20) "Short description #3" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:15 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=9&forum_post_id=62" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_9" } }
Here is the code i am trying to do that only it messes up as it always goes for the top one which is right but i need a way to remove the top entries if the have "lastpost" set to "No Posts" so it get the correct info .
<?php
$sublast = $forum_category['children'];
foreach ($sublast as $key => $row) {
$dates[$key] = $row['lastpost'];
}
array_multisort($dates, SORT_ASC, $sublast);
$lastitem = $sublast[count($sublast) - 1];
var_dump($sublast);
?>
This is the dump of $lastitem :
array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=> string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" }
And as you can see it took the "No Posts" when that whole array entry should be removed so it takes array[1] Which is the one i need.
Any ideas please ?
Use unset function in php to delete elements
Unset will do your need.
unset($array)

Categories