Combining a string and a variable value to access an existing variable - css

Imagine I have the following set of defined variables:
$sportmenu_1: #23765c;
$sportmenu_3: #5e6b34;
$sportmenu_4: #7d7e6c;
$sportmenu_6: #786857;
$sportmenu_8: #487a91;
I also have a list containing the "id" or the last digit of each variable as:
$list: 1 3 4 6 8;
I then want to the $sportmenu_ variables to both name the css selectors and populate the properties, I'm using a list and a foreach as follows:
#each $id in $list_id_sports {
#sportmenu_#{$id} { //this works fine
.menu-sport-item {
background-color: #{$sportmenu_}#{$id}; //here's the issue!
}
}
}
The problem is that I cannot find a way to combine a string and the $id from the foreach to generate a variable that the Sass compiler will understand. Basically, I want the foreach to create a new variable by combining $sportmenu_ and the value of $id. Not sure if this is possible.
My solution doesn't work, since $sportmenu_ doesn't exist. I've tried combining a string and the $id as: "$sportmenu_"#{$id} but this just creates a string followed by the value of $id.
Thanks!

Why can't you do something like this? Using maps
$list: (1: #23765c, 3: #5e6b34, 4: #7d7e6c, 6: #786857, 8: #487a91);
//$id grabs your map index, and $val grabs your hex color values
#each $id, $val in $list {
#sportmenu_#{$id} {
.menu-sport-item {
background-color: #{$val};
}
}
}
You can try it out on SassMeister. Which outputs :
#sportmenu_1 .menu-sport-item {
background-color: #23765c;
}
.....
All I am doing over here is nothing but using maps(kind of hash), looping over, and printing colors accordingly.
Also, what you were trying won't work. You are trying to interpolate two variables and trying to create a dynamic variable name on compile.
#{$sportmenu_}#{$id}; //will throw undefined $sprotsmenu_ error

Related

Laravel collection condition in sum

I have a collection of transactions with relations and I would like to sum column separated by condition of relation column. Right now I have this:
$delegatedProvision = 0;
$ownProvision = 0;
foreach ($transactions as $transaction) {
if ($transaction->discount->consider_improvement) {
$delegatedProvision += $transaction->stats->$column;
continue;
}
$ownProvision += $transaction->stats->$column;
}
$this->salesCollection->put('delegatedProvision', $delegatedProvision);
$this->salesCollection->put('ownProvision', $ownProvision);
It works but I would like to use Laravel collections. So far I have just this:
$provision = $transactions->sum(function ($transaction) use ($column) {
return $transaction->stats->$column;
});
And I don't know how to use condition in sum() method and according column $transaction->discount->consider_improvement (which is boolean) have sum in separated variables. I can use filter each for different consider_improvement but it means that I have to iterate all transactions twice.
Try this:
$collection->where(/* your condition */)->sum($column);

Can I just style decimals of a number in Angular?

I am trying to style just the decimals to look just like this:
Didn't had success, I guess that I need to make my own filter, tried but didn't had success either, I guess it is because I am using it inside a state.
Here the code I am using for the number:
<h2><sup>$</sup>{{salary | number:0}}<sub>.00</sub></h2>
Inside the .app iam using this scope:
$scope.salary = 9000;
Thing is, number can be whatever the user salary is, it get the number from an input, in other places I have more numbers with decimals too.
Possible solutions:
Extract only the decimals from value and print them inside de
tag.
Use a filter to do this?
Use a directive that will split the amount and generate the proper HTML. For example:
app.directive('salary', function(){
return {
restrict: 'E'
, scope: {
salary: '#'
}
, controller: controller
, controllerAs: 'dvm'
, bindToController: true
, template: '<h2><sup>$</sup>{{ dvm.dollar }}<sub>.{{ dvm.cents }}</sub></h2>'
};
function controller(){
var parts = parseFloat(this.salary).toFixed(2).split(/\./);
this.dollar = parts[0];
this.cents = parts[1];
}
});
The easiest solution would be to split out the number into it's decimal portion and the whole number portion:
var number = 90000.99111;
console.log(number % 1);
Use this in your controller, and split your scope variable into an object:
$scope.salary = {
whole: salary,
decimal: salary % 1
}
Protip: Using an object like this is better than using two scope variables for performance

PHP manipulating multiarray data

I have a problem. I don't know how to show the exactly data I want.
My array is like this:
$races[$numRace][$finalPosition]
$races[1][1] = array ('stephan','1:27,895');
$races[1][2] = array ('george', '1:29,075');
$races[1][3] = array ('peter', '1:29,664');
$races[1][4] = array ('benson', '1:29,915');
$races[2][1] = array ('benson', '1:41,113');
$races[2][2] = array ('stephan','1:41,434');
$races[2][3] = array ('george', '1:43,654');
foreach ($races as $v1) {
foreach ($v1 as $v2) {
foreach ($v2 as $v3) {
echo "$v3\n";
}
}
}
This one shows me every data of $race array.
My question is: How can I do for showing just results for race 2?
Important: We don't know how many runners have participated on each race (So, we need a "foreach").
I would like a result like this:
benson
stephan
george
Simply iterate through only the relevant array. Based on your code your foreach should be:
foreach ($races[2] as $pos => $info){
echo $pos.': '.$info[0];
}
I've hard-coded the value 2 in the code but you could easily use a variable (foreach ($races[$raceNum]).
If you don't already know, => makes it possible for us to use both the key and the value as variables in our loop.

Very complicated XQuery transformation

I have a very complex XQuery to write (at least by my standards).
Here is my input xml:
<testRequest>
<request1>
<Line>1</Line>
<action>addtoName</action>
</request1>
<request2>
<Line>2</Line>
<action>addtoSpace</action>
</request2>
<request3>
<Line>3<Line>
<action>addtospace</action>
</request3>
</testRequest>
In my output xml, the actions should be attached as attributes to the "request1" elements. So, based on the action element under a request1 element, the attribute for the request1 element should be one of the following:
if action = IgnoreCase(addtoName), the request1 element should be <request1 action=insertingname>
if action = IgnoreCase(addtoSpace), the request1 element should be <request1 action=updatingspace>
Not only this, but also, I need to add an attribute to the element, based on the action values underneath it.
So, I have to traverse each of the elements under a element and see if any of the elements are equal to "addtospace" if yes, then I need to get the corresponding values of the elements and make up the attribute for the element. From the above xml, my attribute for the element should be,
<testRequest lineFiller="Line Like 2_* AND Line Like 3_*>, where 2 and 3 are the respective line numbers.
and if there are no elements with element= addtoSpace, then the attribute for the element should be "changed".
So, in summary, my transformed xml should look like this:
<testRequest lineFiller="Line Like 2_* AND Line Like 3_*>
<request1 action=insertingname>
<Line>1</Line>
<action>addtoName</action>
</request1>
<request2 action=updatingspace>
<Line>2</Line>
<action>addtoSpace</action>
</request2>
<request3 action=updatingspace>
<Line>3<Line>
<action>addtospace</action>
</request3>
</testRequest>
Any help to accomplish this humungous task will be greatly appreciated.
thanks!!!
You should define functions to generate the attributes that you need to add to your elements.
For adding to the "request" element, this should work:
declare function local:getaction($x) {
if (lower-case($x/action) = "addtoname") then attribute action {"insertingspace"} else
if (lower-case($x/action) = "addtospace") then attribute action {"updatingspace"} else
()
};
The linefiller attribute can be created similarly:
declare function local:getfiller($x) {
attribute lineFiller {
if ($x/*[lower-case(action) = "addtospace"]) then
string-join(
for $r in $x/*[lower-case(action) = "addtospace"]
return concat("Line Like ",$r/Line,"_*")
, " AND ")
else "change"
}
};
Then to put it all together, fun a simple for loop over your original document, adding in the attributes where needed:
let $doc:=<<your original document>>
return
<testRequest>
{ local:getfiller($doc) }
{ for $r in $doc/* return
element { name($r) } {
local:getaction($r),
$r/*
}
}
</testRequest>
EDIT: enhanced getfiller function to return "change" if there are no actions

assigning value in razor difficulty

I am having the following pice of code which is firing error
Error 1 Invalid expression term '='
#{
int Interest;
}
<td>#if (#item.interest.HasValue)
{
#Interest= #item.interest.Value.ToString("F2");
}
When declaring a variable, this variable needs to be assigned:
#{
string Interest = "";
}
and then:
#if (item.interest.HasValue)
{
Interest = item.interest.Value.ToString("F2");
}
This being said doing something like this in a view is a very bad design. I mean things like declaring and assigning variables based on some condition is not a logic that should be placed in a view. The view is there to display data. This logic should go to your controller or view model.
Inside your #if block you can address variables without the # sign.
#if (#item.interest.value) {
#item= #item.interest.Value
}
Is interpreted as:
#if (#item.interest.value) {
Write(item=);
Write(#item.interest.Value);
}
As you can see Write(item=) is not valid C# code.
You should use:
#if (item.interest.value) {
item = item.interest....
}
The reason your if (#item....) statement compiles, with the # sign. Is because you can prefix an identifier with the # to use reserved words as identifier names.
Try this:
#{
string Interest;
}
<td>#if (#item.interest.HasValue)
{
Interest= #item.interest.Value.ToString("F2");
}
By the way you are trying to assign a string (the result of ToString()) to an integer. This will not work.

Resources