I need to extract the pricing data from a form. The form contain several dropdown options. All combo of options must be extracted.
Example of the form dropdown:
size => 1, 2, 3, 4, 5
type => 1, 2, 3, 4
color => 1, 2, 3
units => 1, 2, 3, 4, 5, 6, 7
So this particular product has 420 possible configurations size * type * color * units
How do I write some some sort of loop which will get all possible combinations?
TAG POS=1 TYPE=SELECT FORM=ACTION:/food/getPrice.do ATTR=NAME:size CONTENT=#1
By the way trying to use imacros to select an option by index as shown in the example above does not work. I am forced to select by specifying the value (such as: %1)
I also tried SET !DATASOURCE and imported a CSV file but it didn't loop through as I need it to.
--Just to clarify -- I need this kind of output (doesn't have to be in this order, but MUST output all possible combinations):
size => 1 type => 1 color => 1 units => 1
size => 1 type => 2 color => 1 units => 1
size => 1 type => 2 color => 2 units => 1
size => 1 type => 2 color => 2 units => 2
size => 1 type => 2 color => 2 units => 2
size => 1 type => 3 color => 1 units => 1
. . .
--Side Note--
If you know of how I can run this imacro from a shared server (or EC2) please advise. Thanks for the help! :)
You're looking for the cartesian product, which is described here in PHP: PHP 2D Array output all combinations
Related
So I'm learning Php, so as I was messing around with arrays to see how they work, I stumbled into this when I made two arrays.
$TestArray1 = array( 1 => 1, "string" => "string", 24, "other", 2 => 6, 8);
$TestArray2 = array( 6 => 1, "string" => "string", 24, "other", 1 => 6, 8);
But when I print them out with print_r() this is what I get (this also happens with var_dump by the way)
Array ( [1] => 1 [string] => string [2] => 6 [3] => other [4] => 8 )
Array ( [6] => 1 [string] => string [7] => 24 [8] => other [1] => 6 [9] => 8 )
As far as I can tell, by putting the two in the second array it overwrites the next possible spot with no key and then keeps going, shortening the array. So I thought that meant that if I use a 1 it would put it at the start but that does not happen either.
Is this normal or is there something wrong with my php installation?
Im using Ampps in windows 10 with php 7.3.
Thanks in advance
Good question.
What's happening is that when determining automatic numeric indexes, PHP will look to the largest numeric index added and increment it (or use 0 if there are none).
The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
What's happening with your first array is that as it is evaluated left-to-right, 24 is inserted at index 2 because the last numeric index was 1 => 1.
Then when it gets to 2 => 6, it overwrites the previous value at index 2. This is why 24 is missing from your first array.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Here's a breakdown
$TestArray1 = [1 => 6]; // Array( [1] => 6 )
// no index, so use last numeric + 1
$TestArray1[] = 24; // Array( [1] => 6, [2] => 24 )
$TestArray1[2] = 6; // Array( [1] => 6, [2] => 6 )
When you manually add numeric indexes that are lower than previous ones (ie $TestArray2), they will be added as provided but their position will be later.
This is because PHP arrays are really maps that just pretend to be indexed arrays sometimes, depending on what's in them.
References are from the PHP manual page for Arrays
I have an array that contains an item's ID and it's quantity. The quantity is basically how many times an item has been placed in the collection. So item 1 is in the collection ten times, item 2 is in the collection two times, and item 9 is one time.
So this means that I have one full collection, because item 9 is only in there once.
$data = [
1 => 10,
2 => 2,
3 => 2,
4 => 2,
5 => 2,
6 => 2,
7 => 2,
8 => 2,
9 => 1
];
Below: I now have two collections, because item 9 is in twice.
$data = [
1 => 10,
2 => 2,
3 => 2,
4 => 2,
5 => 2,
6 => 2,
7 => 2,
8 => 2,
9 => 2
];
In the next instance I still only have two collections, because item 3 is only in there two times. hopefully it's making sense what I'm trying to do.
$data = [
1 => 10,
2 => 5,
3 => 2,
4 => 5,
5 => 8,
6 => 4,
7 => 4,
8 => 5,
9 => 6
];
The array isn't fixed to 9 items either, it can be any amount.
This should be fairly simple, but I've been racking my brain over this for quite some time, and it's just stuck; I'm too close to the problem now. I originally thought I could divide the total number of items by the quantity but that didn't work.
How do I do this so that I can loop through the array and see how many full collections I have?
Based on how you've described the issue, couldn't we simply get the lowest number in the array? Example:
$min = min($data);
In the event of a null/empty value, min() should return 0.
I have two models, Component and Group. A Group has many Components, and a Component can be in many Groups. Now, what I need is a view, in which there's a table, n times m field (or matrix) of checkboxes. The Columns would be all the Groups (which would be fewer) and the rows would represent the Component. Basically, when the HaBTM-Relationship exists, the checkbox is checked.
This should be editable, meaning it would be wrapped in a form.
| Group 1 | Group 2 | Group 3
C1 | x | | x
C2 | x | x |
C3 | | | x
What is the least stressful way to achieve this in CakePHP?
Alright.
First of course you load the data from your connecting table. So you get a lot of datasets like:
id => 4
component_id => 4
group_id =>3
Then you have an array of all existing components and another one which contains all existing groups.
Since you have the components in rows I would reorganise this array of arrays, so that you have a multidimensional array which as first key has the component_id and under this id you have an array containing the arrays of all datasets, which contain this group.
e.g.:
array(
1 => array(
0 => array(
'id' => 4,
'component_id' => 1,
'group_id' => 2,
),
1 => array(
'id' => 4,
'component_id' => 1,
'group_id' => 5,
),
),
2 => array(
0 => array(
'id' => 4,
'component_id' => 2,
'group_id' => 1,
),
1 => array(
'id' => 4,
'component_id' => 2,
'group_id' => 3,
),
),
);
After this reorganisation you can pass it to the view, where you can iterate through it to build the form, giving each checkbox the component-group info, which you can collect after sending the form.
Nicer than that (as a version 1.2) would be a AJAX call which saves the combination immediately after activating or deactivating the checkbox. Your choice ;)
Calamity Jane
Well I am trying to sort some data in PHP. Here is an example of the array(the first column representing ids I want to still be associated with their respective values in each row):
0 1 2.0
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
5 20 70.8
6 2 8.2
First I would like to arrange the rows in an order where the values in the second column are in decending order:
5 20 70.8
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
6 2 8.2
0 1 2.0
Then, keeping those with the second column still in its arrangement, by each set of rows with the same value in the second column, arrange the third values in ascending order:
5 20 70.8
2 15 5.5
1 15 20.0
3 15 55.1
6 2 8.2
4 2 22.3
0 1 2.0
I have tried some things with the array sorting functions for PHP, however I can't figure out how to do both those operations while still maintaining row association.
One way is to use the usort function to create a custom sorting routine. There is quite a large number of ways to sort arrays in PHP depending on how you want the data sorted, multi-dimensionality, key sorts, etc. This snippet sorts based on the input and output requirements stated above.
$vals = array(
array('id' => 0, 'val1' => 1, 'val2' => 2.0),
array('id' => 1, 'val1' => 15, 'val2' => 20.0),
array('id' => 2, 'val1' => 15, 'val2' => 5.5),
array('id' => 3, 'val1' => 15, 'val2' => 55.1),
array('id' => 4, 'val1' => 2, 'val2' => 22.3),
array('id' => 5, 'val1' => 20, 'val2' => 70.8),
array('id' => 6, 'val1' => 2, 'val2' => 8.2)
);
usort($vals, 'sortfn');
function sortfn($a, $b)
{
if($a['val1'] == $b['val1'])
return ($a['val2'] > $b['val2']);
else
return ($a['val1'] < $b['val1']);
}
var_dump($vals);
Why this does not work?
$stringhaha =" 1 => General,
2 => Business,
3 => Entertainment,
4 => Health,
5 => Politics,
6 => Sci/Tech,
7 => Sports,
8 => News";
$all_categories = array($stringhaha);
print_r($all_categories);
(will give an array with 1 item.)
While this works:
If I include the variable content like this it will create properly an array with 8 items:
$all_categories = array(1 => General,
2 => Business,
3 => Entertainment,
4 => Health,
5 => Politics,
6 => Sci/Tech,
7 => Sports,
8 => News);
print_r($all_categories);
What is happening is exactly what you should expect: you've declared an array that contains one string.
It doesn't matter that your string looks like an array to us humans, PHP is merely PHP, and can't magically detect that you want it to parse an array from a string.
giorgio79, meet PHP Docs, your new best friend.
It's called language syntax. You cannot do whatever you want. You have to speak the language how it was designed.
This doesn't work either
message = hello
Why? Because it's not syntactically correct. Same applies for your example with array.
This is correct
$message = 'hello';
Every language has rules and you have to respect them. Good luck.
I think the correct syntax is:
$all_categories = array(1 => "General",
2 => "Business",
3 => "Entertainment",
4 => "Health",
5 => "Politics",
6 => "Sci/Tech",
7 => "Sports",
8 => "News");
print_r($all_categories);
You do want an array of strings, right?