it's possible overwrite the values of each star?
i need define step = 3.75, min = 0 and max = 15, and prevent from selecting half a star.
the values I want are:
star 1 => 0; star 2 => 3.75; star 3 => 7.5; star 4 => 11,25; star 5
=> 15.
form.php
echo $form->field($model, 'rating')->widget(StarRating::classname(), [
'pluginOptions' => [
'stars' => 5,
'step' => 3.75,
'min' => 0,
'max' => 15,
]
]);
but when i make this, the selection of each star not display correctly, half of star is selected.
I thought you only want to display the client side (with manual numbers). And you will not get numbers from DB
the correct way is to overwrite the star-rating file. Which should be referred to the Kartik forum.
You must overwrite that the first star has a zero score (highlight first star = 0)
However, you can use the following code in your model.
public function beforeSave($insert)
{
// if ($insert) { // only for Save (No Update)
if (!empty($this->Your_field)) {
$this->setAttribute('Your_field', $this->Your_field-3.75);
}
// }
return parent::beforeSave($insert);
}
Instead of your_field, enter your field name (field of the rate).
You have made the settings incorrectly.
Refer the plugin documentation and demos for details.
quotation krajee:
The logic for highlighting stars depends on the stars, min, max, and
step configurations. The percentage of each star to be highlighted for
each step, will be evaluated using the following expression:
STAR_HIGHLIGHT_PERCENT = (max - min) * step * 100 / stars
For example:
If min = 0, max = 5, step = 0.5, and stars = 5, then
STAR_HIGHLIGHT_PERCENT will evaluate to 50% of each star for each
step.
If min = 1, max = 5, step = 0.5, and stars = 5, then
STAR_HIGHLIGHT_PERCENT will evaluate to 40% of each star for each
step.
So, for example 2 above, the stars will not be completely highlighted
as desired. It is therefore important you set the configuration of
stars, min, max, and step correctly.
Refer the plugin documentation and demos for details.
for You:
'stars' => 4,
'step' => 3.75,
'min' => 0,
'max' => 15,
or
echo StarRating::widget(['name' => 'rating',
'pluginOptions' => [
'stars' => 5,
'step' => 3.75,
'min' => 0,
'max' => 18.75,
'starCaptions' => new JsExpression("function(val){return val-3.75 + ' hearts';}")
]
]);
1
If your field values are integers 1 to 5
To display half stars and full stars, You must use round numbers.
After putting the numbers in the following formula:
(max - min) * step * 100 / stars
Equal to: 50% or 100%
And to display only a full star, Equal to: 100%
2
However, it is better that the rate and step are equal.
For example, if step is 2, is better than max equal to 10 (5 Stars)
3
All of this varies according to the value of your field:
for example:
values: .5, 1, 1.5 , ... 5
Default (stars=5,max=5,step=0.5)
values: 1, 2, 3 , ... 5
(stars=5,max=5,step=1)
values: 1, 2, 3 , ... 12
(stars=6,max=12,step=1) => Half star = one point
values: 2, 4, 6 , ... 12
(stars=6,max=12,step=1) => one star = 2 point
Related
Example: The shop has 3 different payment amount of promotion, if buy 10 get 1 credit and 20 for 2 credit...These promotions I set on config like this:
reward
0
10 amount
1 reward
1
20 amount
2 reward
2
30 amount
3 reward
So, how can I loop or foreach these can determine my purchased amount?
note: I am new to programming so please guide me to my homework.
I expect the output should:
if amount=20
then
array[0] true
array[1] true
array[3] wrong(require amount=30)
then get the final credit i get
so, all you need is to loop through your config and find if your curr amount is less then config amount:
$config = [
[
'amount' => 10,
'reward' => 1,
],
[
'amount' => 20,
'reward' => 2,
],
[
'amount' => 30,
'reward' => 3,
],
];
to loop through you can use foreach
when the condition is true there is no reason to loop more - so just break
try yourself before opening the demo
I am cracking my brain and can't find a good solution for my problem. I am trying to design a system that I can use for batch picking in our order system.
The point is that from a set of orders I want to pick 6 orders that are most equal to each other. In our warehouse most orders are them so we can safe a lot of time by picking some orders at the same time.
Assume I have the following array:
<?php
$data = [
156 => [
1,
2,
7,
9,
],
332 => [
3,
10,
6
],
456 => [
1,
],
765 => [
7,
2,
10,
],
234 => [
1,
9,
3,
6,
],
191 => [
7,
],
189 => [
7,
6,
3,
],
430 => [
10,
9,
1,
],
482 => [
1,
2,
7,
],
765 => [
1,
5,
9,
]
];
?>
The array key is the order id, and the values are the product ID's it contains. If I want to pick the top 3 orders which look at much like each other, where do I start?
Any help would be much appreciated!
1. Step
Sort productId inside order (ASC)
2. Step
In loop check difference (array_diff) in each order to each other.
Create array with defference. For example:
$diff = [
'156' => [ //order id
'234' => 4, // with order 234 has 4 differences
'332' => 7, // with order 332 has 7 differences
// and so on...
],
]
3. Step
Order $diff by ASC and receive order with less differences.
Improvement
Also you could add total size of products in order for compare with difference. For example, If you have an order with 100 products and 10 diffs - it's better than order with 10 products and 9 diffs.
Here is what i would do if I had the problem :
$topOrders = [];
foreach($data as $value):
foreach($value as $order):
if(isset($$order)):
$$order++;
else:
$$order = 1;
endif;
$topOrders[$order] = $$order;
endforeach;
endforeach;
print_r($topOrders);
In $topOrders, you have an array that contains as key the ID, and as value you got the number of orders. All you have to do is to sort your array to get your top 3.
I have a rating system on my Wordpress that came with the theme. The maximum rating possible is 10, so I wanted to edit this and make a 100 possible rating.
So I edited this part:
public static function max_rating( $listing_id = null ) {
$default = 100;
So now it understands that the max possible rating is 100.
But under the rating array, there were these lines:
$rating_options = array(
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'10' => 10,
Which understand that the maximum possible rating is 10. Now I want to make a maximum rating of 100, but adding '11' => 11, '12' => 12, '13' => 13 etc takes a lot of time and it consumes a lot of space in my file. Is there a possiblity to shorten this or do I really have to enter every rating up to 100?
The accepted answer points you in the right direction.
Additional I would advice using array_combine like so:
$range = range(1,100);
$rating_options = array_combine($range, $range);
// array(1=>1, 2=>2, ...)
This way, your keys will be the same as the values.
you can use PHP's range function:
$ratings = range(0, 100);
reference: https://secure.php.net/manual/en/function.range.php
I have an array of scores and each score is weighted for importance. I would like to get an overall score out of 100 depending on the score and the weight giving to each item.
$array_one = array(
array(
'score' => 1.23,
'max' => 10,
'weight' => 10
),
array(
'score' => 56.78,
'max' => 100,
'weight' => 20
),
array(
'score' => 7.56,
'max' => 10,
'weight' => 20
),
array(
'score' => 4.67,
'max' => 10,
'weight' => 30
)
);
So if an item has a larger weight, it is giving a bigger percentage in the score out of 100. i.e if I get 4 scores equaling 50% of the max value in the range, but they all increase in weight 10,20,30,40, the second score has 10% more weight than the first, the third 20% more weight than the first, and the 4th 30% more weight than the first and so on. Also, some weights will be the same 10,20,20,30 or 10,10,10,10 etc. I hope this makes sense. So in summary each items score should be a percentage of the max value, then scores are weighted against 100. Expected results should return one overall score for all 4 items.
Sum the scores divided by max multiplied by their weight. Take the sum of weights and divide by your max score (100). Then, divide the first number by the second number. This will be the total weighted score mapped to the [0, 100] range.
I'm trying to find the best way to calculate the box size needed for shipping.
I have 3 shipping containers with different sizes. I have the product's width, length, depth, and mass defined in the database.
I would like to know how to find the smallest amount of boxes needed to ship, and also the smallest dimensions of those boxes given the number of items in the cart.
My current 'idea' is to find the maximum width of the entire products array, the select a box according to it, and then split the order as needed... this doesn't seem like it would work.
My Box sizes are:
- 8 x 6 x 6 = 228 cubic inches
- 10 x 8 x 8 = 640 cubic inches
- 12.5 x 12.5 x 12.5 = 1953.125 cubic inches
A product is defined as such:
[Product] => Array
(
[STOCK_CODE] => 010003
[Product_Slug] => GABA_010003
[ItemName] => GABA
[WHOLESALE_PRICE] => 17.47
[RETAIL_PRICE] => 24.95
[Brand] =>
[ProductLine] =>
[image_name] => 705077000440
[MASS] => 0.313
[Height] => 4.625
[Width] => 2.375
[Depth] => 2.375
[cubic_inches] => 26.087890625
)
I've looked into knapsack problem, packing problem, etc and can't find a way to do this. Any help would be GREAT.
function shipping(){
$this->CartProduct->unbindModel(
array('belongsTo' => array('User'))
);
//find all cart products by current logged in user
$cartItems = $this->CartProduct->find('all', array('conditions' => array('CartProduct.user_id' => $this->Auth->user('id'))));
$i = 0;
//get the max width, height, depth
$maxHeight = 0;
$maxWidth = 0;
$maxDepth = 0;
foreach($cartItems as $c){
$cartItems[$i]['Product']['cubic_inches'] = $c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth'];
$cartItems[$i]['CartProduct']['total_cubic_inches'] = ($c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth']) * $c['CartProduct']['qty'];
if($c['Product']['Height'] > $maxHeight)
{
$maxHeight = $c['Product']['Height'];
}
if($c['Product']['Width'] > $maxWidth)
{
$maxWidth = $c['Product']['Width'];
}
if($c['Product']['Depth'] > $maxDepth)
{
$maxDepth = $c['Product']['Depth'];
}
$i++;
}
//possible containers
//8 x 6 x 6 = 228 ci
//10 x 8 x 8 = 640 ci
//12.5 x 12.5 x 12.5 = 1953.125
$possibleContainers = array(
1 => array(
'Height' => 8,
'Width' => 6,
'Depth' => 6,
'Cubic' => 228),
2 => array(
'Height' => 10,
'Width' => 8,
'Depth' => 8,
'Cubic' => 640),
3 => array(
'Height' => 12.5,
'Width' => 12.5,
'Depth' => 12.5,
'Cubic' => 1953.125)
);
$max = array(
'Height' => $maxHeight,
'Width' => $maxWidth,
'Depth' => $maxDepth,
);
pr($cartItems);
pr($possibleContainers);
die();
}
As for getting a optimal answer, that's NP-Hard... http://en.wikipedia.org/wiki/Bin_packing_problem
The greedy algorithm shown on Wikipedia, while it can be quite far off, might actually do for your case.
However as an estimate you could just sum up the volumes of the items and then apply a inefficiency factor and then use the smallest box(s) you can.
Alternately you could sort the items into decreasing volume and then see how much you can get into the current set of boxes, creating a new box when you can't fit the item in. Not sure how you would handle different box sizes though. You could also have a case where it changes the box size rather than creating a new box.
Food for thought.
Here is a low tech but possible solution:
We just ran into the same issue. I decided to take our box sizes and then give each product a percentage for how much space it took in each box size. Our products are free form and can be squished a bit so if yours are absolute in size you may need to reduce the percentages to account for products being put in the box at different angles ect... Also for us we are able to always put things in the boxes as the same angle to each other so this also helps make the below method work better.
This assumes there are 3 box sizes:
Product A
Box A = 48% (2 fit in a box)
Box B = 30% (3 fit in a box)
Box C = 12% (8 fit in a box)
Product B
Box A = 24%
Box B = 15%
Box C = 7%
Then just have your code add up those percentages for your cart items for box A, B and C ... obviously if any are below 100% everything should fit and if you start from top to bottom the first one to reach less than 100% will fit your products and be the smallest box. And if you run into any scenarios when packing that wont fit just slightly reduce the percentage you entered for that product.
For multiple box shipments you just need to decide what you want to do as for as combinations. The above works best for single box shipments but with some additional logic could easily work well for multiple box shipments.