Sum of a table column - SMARTY - smarty

I have a multidimensional associative array, which I pass to a smarty template. I use below code in my smarty template to generate the shown two tables in my browser.
The smarty code:
<!--language:smarty-->
{foreach from=$allarr key=header item=table}
<h5>{$header}</h5>
<table>
<tr>
<td>{t}Emp #{/t}</td>
<td>{t}Employee Name{/t}</td>
<td>{t}Amount{/t}</td>
</tr>
{foreach from=$table item=sub}
<tr>
<td>{$sub.emp_num}</td>
<td>{$sub.user_name}</td>
<td>{$sub.amount}</td>
</tr>
{/foreach}
<tr>
<td colspan="2">{t}Variation Total{/t}</td>
<td colspan="1">{t}+++{/t}</td>
</tr>
</table>
{/foreach}
The tables:
Table One
________________________________
Emp # |Employee Name |Amount |
________|_______________|_______|
1000001 | Test User | 775.00|
26 | user1 | 555.00|
________|_______________|_______|
Variation Total | +++ |
________________________|_______|
Table Two
______________________________
Emp # |Employee Name| Amount|
________|_____________|_______|
1000001 | Test User | 110.00|
________|_____________|_______|
Variation Total | +++ |
______________________|_______|
What I want, is to replace the "+++" with the actual sum of the values in each Amount column. Nothing seems to work. Can anyone help? Thanks in advance.

Figured out a way. :)
The complete code is below.
{foreach from=$allarr key=header item=table}
<h5>{$header}</h5>
<table>
<tr>
<td>{t}Emp #{/t}</td>
<td>{t}Employee Name{/t}</td>
<td>{t}Amount{/t}</td>
</tr>
{foreach from=$table item=sub}
<tr>
<td>{$sub.emp_num}</td>
<td>{$sub.user_name}</td>
<td>{$sub.amount}</td>
</tr>
{assign var="sum" value="`$sum+$sub.amount`"}
{/foreach}
<tr>
<td colspan="2">{t}Variation Total{/t}</td>
<td colspan="1">{t}{$sum}{/t}</td>
{assign var="sum" value=0}
</tr>
</table>
{/foreach}

Related

Display data on my codeigniter view page from the same database table but with different categories (using the same model and controller)

All of this has worked fine for a year, however now I have to separate my goals on the same page between 2020 and 2021. I have "goal_year" as my column within the "goals_table" database table.
I tried to create 2 separate cells where the code pulls in goals for 2020 and goals for 2021 but it makes the entire page show blank.
I'm trying to do this without creating a separate model and controller file.
DATABASE
id | district | goal_year | goal_af_am_msm | goal_b | goal_c
---------------------------------------------------------------
1 | macon | 2020 | 456 | 87 | 234
2 | cobb | 2020 | 987 | 34 | 762
3 | macon | 2021 | 432 | 67 | 819
4 | fulton | 2021 | 843 | 21 | 236
Here's my code:
MODEL
function _goal()
{
$this->db->where('district', 'macon');
$query = $this->db->get('goals_table');
return $query->row();
}
CONTROLLER
public function index()
{
$data['_goal_data'] = $this->Macon_model->_goal(); // GOALS
$this->template->load('admin', 'default', 'macon', $data);
}
VIEW
<td>
<?php echo $_goal_data->goal_af_am_msm; ?> <!-- ENTERED GOALS -->
</td>
ALTERNATE VIEWS THAT I TRIED
1).
<td>
<?php echo $_goal_data->goal_af_am_msm->where('goal_year','2020'); ?>
<!-- Shows entire page blank -->
</td>
2).
<td>
<?php if($_goal_data->goal_year == '2020') echo $_goal_data->goal_af_am_msm ; ?>
<!-- Shows 2020 GOALS but show blank for 2021 -->
</td>
3).
<td>
<?php echo $_goal_data->goal_year == '2020' ? $_goal_data->goal_af_am_msm : ''; ?>
<!-- Shows 2020 GOALS but show blank for 2021 -->
</td>
So I kept playing around with it and I got it figured out. I ended up having to change my model from
return $query->row();
to
return $query->result();
Then I had to update my view file to
<?php foreach($_goal_data as $macon_data) : ?>
<?php if($macon_data->goal_year == '2020') echo $macon_data->goal_af_am_msm; ?>
<?php endforeach; ?>

How to show product sizes related to product using laravel?

I want to show product sizes related to product I already have made a relation b/w tables into database i just only want to know that when i click the specific product the size related to that product will be shown in the table.inside function how to make joining b/w three tables please make the join table using DB class because it will be easy to understand for me thanks.
Does Anyone have an idea ?
please check product sizes image
https://i.stack.imgur.com/Yt6Jk.png
Database
product_sizes table
----------------------------------------------------
id | size_name | Body_length | Body_width |
----------------------------------------------------
1 | M | 2.3 | 3.4
2 | L | .5 | 4.4
3 | s | 2.5 | 3.2
4 | xs | 3.5 | 2.4
products table
----------------------------------------
id | product_name | Description |
----------------------------------------
1 Ultraclub 1 | Ultraclub 300 |
2 Ultraclub 2 | Ultraclub 200 |
3 Ultraclub 3 | Ultraclub 500 |
4 Ultraclub 4 | Ultraclub 600 |
5 Ultraclub 5 | Ultraclub 400 |
available_product_sizes table
----------------------------------------
id | product_id | product_size_id
----------------------------------------
1 | 2 | 1
2 | 3 | 4
3 | 4 | 3
4 | 1 | 2
5 | 2 | 3
Controller
public function single_product($product_slug){
$Sizes=DB::table('product_sizes')->get();
$single_product=DB::table('products')->where('product_slug',$product_slug)->get();
return view('front_end/single_product',compact('single_product','Sizes'));
}
html view
<div class="product-tabs__pane product-tabs__pane--active" id="chart">
<table class="table table-bordered text-center">
<tr style="background-color:#3366cc;color:white">
<td><b>Sizes</b>
</td>
<td><b>Body length</b>
</td>
<td><b>Body width</b>
</td>
<td><b>Sleeve length</b>
</td>
</tr>
#foreach($Sizes as $size)
<tr>
<td><b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
</table>
</div>
I assume you're already has/desfined many-to-many relationship between products and product_sizes tables
//App\Product.php
public function sizes()
{
return $this->belongsToMany('App\ProductSize', 'available_product_sizes', 'product_id', 'product_size_id');
}
//App\ProductSize.php
public function products()
{
return $this->belongsToMany('App\Product', 'available_product_sizes', 'product_size_id', 'product_id');
}
In order to get available product sizes you can use :
$sizes = App\Product::find(1)->sizes()->orderBy('size_name')->get();
Controller fix
public function single_product($product_slug) {
$single_product = Product::with('sizes')->where('product_slug',$product_slug)->first();
return view('front_end/single_product',compact('single_product'));
}
View Fix
//...
#foreach($single_product->sizes as $size)
<tr>
<td>
<b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
//...
Answer to the question without using eloquent
use Illuminate\Support\Facades\DB; //include this
$products = DB::table('products as p')
->join('available_product_sizes as aps','aps.product_id','p.id')
->join('product_sizes ps','ps.id','aps.product_size_id')
->where('product_slug',$product_slug)->get();
But it is highly recommended to use eloquent relationships.

Group select options if description is the same

So I have a select that the options are froma laravel Foreach
#foreach($courseMonth as $cm)->groupBy('description');
<optgroup label="{{ nameMonth($cm->month) }}">
#foreach($days as $date)
#if(date('m', strtotime($date['date'])) == $cm->month)
<option value="{{ date('d/m/Y', strtotime($date['date'])) ." - ". $date['hours'] }}">{{ $date['description'] . ' - ' . date('d/m/Y', strtotime($date['date'])) . ' - ' . $date['hours'] . ' ('. $date['total'] . 'h)'}}</option>
#endif
#endforeach
</optgroup>
<?php $i++;?>
#endforeach
This is the options. And I want if $date['description'] is the same in two options, to combine both options. More detailed, if three options have their description as "Work", to combine all of the options.
I've tried a groupby in front of the foreach
#foreach($courseMonth as $cm)->groupBy('description');
But this didn't do anything. The table for the options only has one row (ex: One row has only one description but it can have multiple day options in the select) so I need to group those only when there is the same description in two options.
Table structure:
-----------------------------------------------
| id | description | date | hours | total |
|---------------------------------------------|
| 4 | work |2019-23-4 | 14h | 6h |
| 5 | work |2019-23-5 | 13h | 5h |
| 6 | school |2019-23-5 | 13h | 5h |
-----------------------------------------------
So the first two would appear in the same option and the last one as the second option of the select.

How to merge(group) xpath results

I Have following table
<table>
<tbody>
<tr>
<td>First Row | First Column</td>
<td>First Row | Second Column</td>
</tr>
<tr>
<td>Second Row | First Column</td>
<td>Second Row | Second Column</td>
</tr>
<tr>
<td>Third Row | First Column</td>
<td>Third Row | Second Column</td>
</tr>
<tr>
<td>Forth Row | First Column</td>
<td>Forth Row | Second Column</td>
</tr>
<tr>
<td>Fifth Row | First Column</td>
<td>Fifth Row | Second Column</td>
</tr>
<tr>
<td>Sixth Row | First Column</td>
<td>Sixth Row | Second Column</td>
</tr>
</tbody>
I need to use xpath expresion to receive grouped 3 records:
First Row | Second Column, Second Row | Second Column
Third Row | Second Column, Forth Row | Second Column
Fifth Row | Second Column, Sixth Row | Second Column
How I can achive that using only xpath 1.0?
Here you go, xpath expression:
concat(//tr[1]/td[2], ' , ', //tr[2]/td[2], codepoints-to-string(10), //tr[3]/td[2], ' , ', //tr[4]/td[2], codepoints-to-string(10), //tr[5]/td[2], ' , ', //tr[6]/td[2])
The above will return the following string:
First Row | Second Column , Second Row | Second Column
Third Row | Second Column , Forth Row | Second Column
Fifth Row | Second Column , Sixth Row | Second Column

Putting delay until next page is loaded fully in Selenium IDE

I am trying to automate a test case using selenium IDE. I have installed this add-on on firefox. I am trying to login using admin/admin credentials in one of the websites and after that it takes couple of seconds to load the next page fully.
<tr>
<td>type</td>
<td>id=username</td>
<td>admin</td>
</tr>
<tr>
<td>type</td>
<td>id=password</td>
<td>admin</td>
</tr>
<tr>
<td>click</td>
<td>css=button.btn</td>
<td></td>
</tr>
<tr>
<td>waitForPageToLoad</td>
<td>30000</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>css=span.ncs-tree-nav</td>
<td></td>
</tr>
waitForPageToLoad is not working, ideally it should wait for 30 seconds but as soon as login button gets clicked, test case throws error as it is not able to find next element on the page.
Here is the output from IDE :-
[info] Executing: |open | /login.html | |
[info] Executing: |type | id=username | admin |
[info] Executing: |type | id=password | admin |
[info] Executing: |click | css=button.btn | |
[info] Executing: |waitForPageToLoad | 30000 | |
[info] Executing: |click | css=span.ncs-tree-nav | |
[error] Element css=span.ncs-tree-nav not found
As soon as login is done, it moved for searching expected navigation and error came.
It will be helpful if someone can point out if I missed something.
A pragmatic approach,
If you have some visual condition that gets true just when the page is loaded, you can look for that.
Say that the text "Welcome User" gets displayed in a span with id=content somewhere when the page is loaded, you can replace your waitForPageToLoad with
<tr>
<td>waitForText</td>
<td>id=content</td>
<td>Welcome*</td>
</tr>
Possibly you can even be as blunt as
<tr>
<td>waitForText</td>
<td>//body</td>
<td>*Welcome*</td>
</tr>
though I haven't tested the latter myself.
If your page reloads after clicking, you have to use clickAndWait instead of click:
<tr>
<td>clickAndWait</td>
<td>css=button.btn</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>css=span.ncs-tree-nav</td>
<td></td>
</tr>
If the page does not reload, upper code won't work. In this case wait for a presence of an element that you are going to use on the next step:
<tr>
<td>click</td>
<td>css=button.btn</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>css=span.ncs-tree-nav</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>css=span.ncs-tree-nav</td>
<td></td>
</tr>

Resources