how do I add the variables together - variables

I am really new at this probably can tell. I am trying to add my variables together and display on site. So i cannot get protein 1 and protein 2 added together. How can i get them to add. I would like the user to up arrow till he gets to his right amount of proteins for steak and for chicken. Then I would like to add the total number of proteins, fats, calories for steak and chicken
<h5>Chicken Breast
<button id="up1">+</button>
<button id="down1">-</button>
<b id= "chounces"></b>
</h5>
<h5>Flank Steak
<button id="up2">+</button>
<button id="down2">-</button>
<b id= "stkounces"></b>
</h5>
function updateQuantity(displayQuantity) {
let quantity1 = document.querySelector('#chounces')
quantity1.innerHTML = displayQuantity
}
// Start chicken variables at 0 since product is in cart
let quantity1 = 0
let protein1 =0
let fats1 =0
let calories1 = 0
// Buttons
const quantUp = document.querySelector('#up1')
// add event listener to increase quantity
quantUp.addEventListener('click', function(){
quantity1++
protein1= quantity1 * 6
fats1 = quantity1 * .35
calories1= 31 * quantity1
updateQuantity(`ounces: ${quantity1}, protein: ${protein1}, fats ${fats1}, calories ${calories1}`)
})
//steak
function updateQuantity2(displayQuantity2) {
let quantity2 = document.querySelector('#stkounces')
quantity2.innerHTML = displayQuantity2
}
// Start steak variables at 0 since product is in cart
let quantity2 = 0
let protein2 =0
let fats2 =0
let calories2 = 0
// Buttons
const quantUp2 = document.querySelector('#up2')
// add event listener to increase quantity
quantUp2.addEventListener('click', function(){
quantity2++
protein2= quantity1 * 6
fats2 = quantity1 * .35
calories2= 31 * quantity1
updateQuantity2 (`ounces: ${quantity2}, protein: ${protein2}, fats ${fats2}, calories ${calories1}`)
})

Related

How we display discount percentage badge and sale badge separately on WooCommerce product?

Want to display discount percentage badge on right hand side of product image and sale badge on left hand side of product image in products slider. So, Please Suggest some hooks for this functionality!
Tried to add the following hook but it will replace the existing sale badge with the discount percentage badge on shop page and also this hook is not working for the product slider on homepage.
add_filter('woocommerce_sale_flash', 'add_percentage_to_sale_bubble'); function add_percentage_to_sale_bubble( $html ) { global $product; $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); $output =' <span class="onsale">'.$percentage.'%</span>'; return $output; }
you can use the following hook
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && $product->is_type('simple') ){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
// Active price (the "Sale price" when on-sale)
$sale_price = (float) $product->get_price();
// "Saving Percentage" calculation and formatting
$precision = 0; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">%s</p>', 'woocommerce' ), $saving_percentage );
}
return $price;
}

Angular Gridster-2 Redraggable and Resize

I am new to angular gridster 2
Could you please let me know if we are able to display the already developed charts using angular gridster 2 , I am also looking to see the same approach . As my UI has 5 different div blocks and inside there is a card being displayed in each div block ..
How can I use angular gridster to disaply these 5 cards with draggable and resizable features after loading the page..
<gridster [options]="options">
<div class="col-md-12 ml-auto mr-auto" >
<div class="row" > <!--[item]="item" *ngFor="let item of dashboard" gridster-item-->
<gridster-item class="col-lg-3 col-md-6 col-sm-6" [item]="item" *ngFor="let item of dashboard" >
<card1></card1>
<card2></card2>
</gridster-item>
</gridster>
export class dashboard implements oninit{
options={
draggable: {
enabled: true},
resizable: {
enabled: true
}
};
dashboard = [
{cols: 1, rows: 1, y: 0, x: 0}
];
}
Please explain what does the rows,columns, x and y represents .. How does it effect the UI if these values are changed..
Cols and Rows are your gridster layout design to allow gridster-items. If cols = 1 and rows = 1, you can put one gridster item only except maxItemRows and maxItemCols set greater than 1. The y and x are col and row index for gridster-item.
Example, cols = 5, rows = 5, and x = 3, y = 2, gridster-item will display start from column 3 and row 2.
Here are some articles about gridster.
https://medium.com/javascript-in-plain-english/drag-and-drop-dashboard-builder-with-angular-and-gridster-a07592e54ce2
https://developer.aliyun.com/mirror/npm/package/helio-angular-gridster

Symfony/Doctrine: SUM and AVG score of players

I have in my database the tab: PLAYERS and a tab: SCORES.
In tab SCORES i have these rows: ID - IDPLAYER - SCORE
For example:
ID IDPLAYER SCORE
---------------------
1 1 5
2 2 4
3 1 3
4 2 1
5 1 9
I want put in a template this:
For "player 1" there are 3 scores.
The count of the scores is "17" (9+3+5).
The avg of the score of the player is "5.6" (17totscores / 3countScores).
I have an entity with ORM, it' ok.
I have a controller with this function:
public function avgScoreAction($id) {
$queryScore = $this->getDoctrine()
->getRepository('AcmeBundle:tabScores');
$queryAvgScore = $queryScore->createQueryBuilder('g')
->select("avg(g.score)")
->where('g.idPlayer = :idPlayer')
->setParameter('idPlayer', $id)
->getQuery();
$avgScore = $queryAvgScore->getResult();
$result = ("Score average: ".$avgScore);
return new Response($result);
But I have an error:
"Notice: Array to string conversion in this line:"
$result = ("Score average: ".$avgScore);
If I write this:
$response = new Response();
$response->setContent(json_encode(array($avgScore)));
$response->headers->set('Content-Type', 'application/json');
return $response;
I get this:
[[{"1":"5.6667"}]]
which is the correct avg, but what is: [[{"1":" and "}]] ?????
what is: [[{"1":" and "}]] ?
1 is the index of avg(g.score) in your query. To better understand why, try an echo of $queryAvgScore->getDql() before getResult().
Let's get back to the general question :
the SQL is :
SELECT AVG(SCORE) as AVG, COUNT(SCORE) as COUNT, IDPLAYER as PLAYER FROM SCORES GROUP BY IDPLAYER
and now with query builder :
$queryAvgScore = $queryScore->createQueryBuilder('g')
->select("avg(g.score) as score_avg, count(g.score) as score_count")
->where('g.idPlayer = :idPlayer')
->groupBy('g.idPlayer')
->setParameter('idPlayer', $id)
->getQuery();
Notice that i have added aliases, this is better than using indexes.
Hope it helps.
Symfony 2.6 is easy with DQL
$dql = "SELECT SUM(e.amount) AS balance FROM Bank\Entities\Entry e " .
"WHERE e.account = ?1";
$balance = $em->createQuery($dql)
->setParameter(1, $myAccountId)
->getSingleScalarResult();
Info:
http://doctrine-orm.readthedocs.org/en/latest/cookbook/aggregate-fields.html?highlight=sum

TYPO3: Select count(*) where the hidden rows are included

Im using this code to show the number of registers I have in tt_address in Page ID 68, and works fine, except that it doesnt show the hidden elements.
# Default PAGE object:
page = PAGE
page{
20 = CONTENT
20 {
table = tt_address
select{
selectFields = count(uid) AS count
pidInList = 68
where = deleted = 0
}
renderObj = COA
renderObj {
10 = TEXT
10 {
value = Status: {field:count}
insertData = 1
}
}
}
}
How could I count also the hidden records?
I think it is not possible to get hidden records using typoscript (no records from hidden, timed or access-protected pages can be selected!).
Reference link: http://wiki.typo3.org/TSref/select
You may need to use "userfunc" as given below:
Typoscript:
includeLibs.showHiddenElements = fileadmin/lib/showHiddenElements.php
page.20 =USER
page.20 {
userFunc =user_showHiddenElements->main
field = uid
table = tt_address
where = deleted = 0 and pid = 68
}
fileadmin/lib/showHiddenElements.php :
<?php
class user_ShowHiddenElements {
function main($content, $conf){
$res= $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
$conf[field], // SELECT ...
$conf[table], // FROM ...
$conf[where], // WHERE...
'', // GROUP BY...
'', // ORDER BY...
'' // LIMIT ...
);
return $res;
}
}
?>
You can't bypass the "enable fields" where clauses in a normal way, but TYPO3 allows to hack around that using a UNION query:
select {
selectFields = count(uid) AS count
pidInList = 68
where.data = TSFE:id
# we hack our way around the enablefields restriction
where.wrap = 0 UNION SELECT COUNT(*) as count FROM tt_address WHERE pid = 68
}
An alternative solution:
page{
24 = CONTENT
24.wrap = <div class="status_count">|</div>
24 {
table = tt_address
select {
selectFields = count(uid) AS count
pidInList = 68
where.data = TSFE:id
# we hack our way around the enablefields restriction
where.wrap = |0 UNION SELECT COUNT(*) as count FROM tt_address WHERE pid = 68
}
renderObj = COA
renderObj {
10 = TEXT
10.wrap = |</div><div style="display:none">
10 {
value = Status: {field:count}
insertData = 1
}
}
}
}
Thanks cweiske for giving me this idea.
This is the way: e.g. Showing deleted or hidden pages only
table = pages
select.pidInList = 1
select.where = deleted=1 or hidden=1 UNION SELECT * FROM pages WHERE 1=0
This works 'cause the enablefields are appended to the query. Union with 1=0 where clause returns nothing with the enablefields.

CSHTML/SQL SUM For A Row

I know in mysql its SUM(size), but for some reason building this in razor cshtml its not the same and i cant find anywhere that talks about adding or subtracting 2 numbers in cshtml. So what would be the right function to use to add up rows size?
Code:
#{
Page.Title = "Home #";
var PageTitle = "Home";
var db = Database.Open("PhotoGallery");
var shows = db.Query(#"SELECT * FROM Shows").ToList();
var seasons = db.Query(#"SELECT * FROM Seasons").ToList();
var episodes = db.Query(#"SELECT * FROM Episodes").ToList();
var comics = db.Query(#"SELECT * FROM Comics").ToList();
var artists = db.Query(#"SELECT * FROM Artists").ToList();
var albums = db.Query(#"SELECT * FROM Albums").ToList();
var comicsize = db.Query(#"SELECT SUM(size) FROM Comics").ToList();
var totalsizeb = comicsize;
}
<h1>#PageTitle</h1>
<p align="center">
#shows.Count TV Shows | #seasons.Count Seasons | #episodes.Count Episodes | #comics.Count Comics | #artists.Count Artists | #albums.Count Albums<br />
Bytes | MB | GB | TB
</p>
Error:
Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the
function is not valid. [ Argument # = 1,Name of function(if known) = SUM ]
If you're only interested in the Count of each table then you should SELECT COUNT(*) FROM Shows and so forth rather than pulling all the data from each table just to get a count.
#{
Page.Title = "Home #";
var PageTitle = "Home";
var db = Database.Open("PhotoGallery");
var shows = (int)db.QueryValue(#"SELECT Count(*) FROM Shows");
var seasons = (int)db.QueryValue(#"SELECT Count(*) FROM Seasons");
var episodes = (int)db.QueryValue(#"SELECT Count(*) FROM Episodes");
var comics = (int)db.QueryValue(#"SELECT Count(*) FROM Comics");
var artists = (int)db.QueryValue(#"SELECT Count(*) FROM Artists");
var albums = (int)db.QueryValue(#"SELECT Count(*) FROM Albums");
var comicsize = (int)db.QueryValue(#"SELECT SUM(size) FROM Comics");
var totalsizeb = comicsize;
}
<h1>#PageTitle</h1>
<p align="center">
#shows TV Shows | #seasons Seasons | #episodes Episodes | #comics Comics | #artists Artists | #albums Albums<br />
Bytes | MB | GB | TB
</p>
SUM only works with numeric types such as int, numeric, money, and float make sure that the column is one of those types in your table.