Related
I'm trying to order a custom taxonomy by a custom meta key. That meta key is the number of edition. Weekly, my client adds a new edition (the term of the "edition" taxonomy), and this number increases. At the moment, it is at 124.
So, I'm trying to order the admin list by this numeric meta key, this way:
add_filter("get_terms_args", "mytheme_get_terms_args", 10, 2);
static function mytheme_get_terms_args($args, $taxonomies)
{
if(is_admin() && in_array("edition", $taxonomies)){
$args['order'] = "ASC";
$args['orderby'] = "meta_value";
$args['meta_key'] = "_edition";
$args['meta_type'] = "DECIMAL";
}
return $args;
}
The expected result is all editions ordered as follows:
1, 2, 3, [...], 9, 10, 11, 12, 13, [...] 19, 20...
But, I'm getting this ordering instead:
1, 2, 3, [...], 9, 10, 100, 101, 102 , [...] 109, 11, 110, 111, 112, [...], 118, 119, 12, 120, 121, 122, 123, 13, 14, 15...
After the 10, instead following to 11, this crazy ordering goes to 100, 101, 102.
How can I order this correctly?
Thanks!
So I try to send some output to a file but it doesn't look the way I want.
I have a file with some random integer values , it looks like this:
78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73
4, 6, 9, 11, 16
9, 10, 16, 18, 20
1, 3, 8, 10, 15
Now what I want to do is for each of these lines print the average, max and min number into another file.
My code for this part is:
while (!feof($aFile))
{
$cnt += 1;
$anArray = array();
$anArray = explode(",", fgets($aFile));
foreach ($anArray as $value)
{
$value = (int)$value;
}
$line = "Line $cnt: Average=" . array_sum($anArray) / count($anArray) . " Max=" . max($anArray) . " Min=" . min($anArray);
fwrite($resultsFile, $line);
fwrite($resultsFile, "\n");
}
The problem is that the resultsFile looks like this:
Line 1: Average=70.6 Max= 85 Min= 60
Line 2: Average=9.2 Max= 16
Min=4
Line 3: Average=14.6 Max= 20
Min=9
Line 4: Average=7.4 Max= 15 Min=1
I couldn't exactly copy paste this , because when I do it pastes it the right way
The problem is that it changes line and then prints Min .
Total Average=50.533333333333 Total Max= 85 Total Min=1
This is kinda tricky and is a combination of several moments.
First - you have new line symbol (\n) in each string of your file.
So, for example string 9, 10, 16, 18, 20 is really a 9, 10, 16, 18, 20\n string.
When you explode this string by ,, last element of your $anArray is not 20, but 20\n (with new line).
Now you're trying to cast each element to int, but as by default php works with copy of array, your initial values in $anArray are unchaged. As correctly stated in comments - to apply changes to original array, use $&value notation:
foreach ($anArray as &$value)
{
$value = (int)$value;
}
And last - all following operations on your $anArray silenlty cast 20\n to int (20). But as noticed in max manual, for example:
The actual value returned will be of the original type with no conversion applied
So, in array ['9', '10', '16', '18', '20\n'] max value will be 20, but returned value will be 20\n which creates a new line and moves Min=... output to next line.
I have an array with 4 elements
[1, 2, 3, 4]
So far I am printing several arrays, all with different elements in each array, to a limit set by me.
for($i = 0; $i<=100; $i++){//...
Output so far:
[11, 22, 32, 44]
[22, 33, 44, 45]
[12, 24, 25, 31]
[15, 16, 31, 41]
[22, 33, 44, 45]//already exist
[11, 22, 32, 44]//already exist
...
How can I compare an outgoing array to the next one going out and delete the new one if is equal to the previous array?
You could create a key for an array with the help of implode() and have a set array which has this key. If key is already present, then the current array in iteration is a duplicate one, else it's a new one. Remember to sort the current array as the order of the numbers matter here for proper key check.
<?php
$arr = [
[11, 22, 32, 44],
[22, 33, 44, 45],
[12, 24, 25, 31],
[15, 16, 31, 41],
[22, 33, 44, 45],
[44, 22, 32, 11]
];
$set = [];
foreach($arr as $curr_array){
sort($curr_array);
$hash = implode("|",$curr_array);
if(isset($set[$hash])) echo "Duplicate",PHP_EOL;
else{
print_r($curr_array);
$set[$hash] = true;
}
}
Demo: https://3v4l.org/EXXRu
Can someone teach me how to create a table side by side like this with fpdf:
I'm trying to combine the multiple column with the creating table tutorial but failed. Any help would be appreciated. Thank you.
If you got struggled with fpdf, you can try to do it with Debenu Quick PDF.
This C# code returns the exact result which is on your picture. It is done with merged cells, but also you can use zero border width - it depends on your needs. Of course you can change the width and the color of the borders.
DPL.LoadFromFile("blank.pdf", "");
DPL.SetOrigin(1); //the top left page corner will be used for the origin
DPL.SetMeasurementUnits(0); //the units are approximately the same as a "point"
string content1, content2, content3;
content1 = "1<br>2<br>3<br>4<br>5<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>20";
content2 = "21<br>22<br>23<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>30";
content3 = "31<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>40";
int tableID1 = DPL.CreateTable(20, 3); //20 rows, 3 columns
DPL.SetTableColumnWidth(tableID1, 1, 1, 20); //width of the first column
DPL.SetTableColumnWidth(tableID1, 2, 3, 40); //width of the second and third column
DPL.SetTableRowHeight(tableID1, 1, 20, 10); //height of all rows
DPL.MergeTableCells(tableID1, 2, 1, 20, 1); //merge the cells from second row to 20. row and from 1. column to 1. column - merge the cells of the first column
DPL.MergeTableCells(tableID1, 2, 2, 20, 2); //merge the cells of the second column
DPL.MergeTableCells(tableID1, 2, 3, 20, 3); //merge the cells of the third column
DPL.SetTableCellContent(tableID1, 2, 1, content1); //add the content
DPL.DrawTableRows(tableID1, 50, 50, 700, 1, 0); //draw the table to the page
int tableID2 = DPL.CreateTable(20, 3); // second table
DPL.SetTableColumnWidth(tableID2, 1, 1, 20);
DPL.SetTableColumnWidth(tableID2, 2, 3, 40);
DPL.SetTableRowHeight(tableID2, 1, 20, 10);
DPL.MergeTableCells(tableID2, 2, 2, 20, 2);
DPL.MergeTableCells(tableID2, 2, 3, 20, 3);
DPL.MergeTableCells(tableID2, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID2, 2, 1, content2);
DPL.DrawTableRows(tableID2, 160, 50, 700, 1, 0);
int tableID3 = DPL.CreateTable(20, 3); //third table
DPL.SetTableColumnWidth(tableID3, 1, 1, 20);
DPL.SetTableColumnWidth(tableID3, 2, 3, 40);
DPL.SetTableRowHeight(tableID3, 1, 20, 10);
DPL.MergeTableCells(tableID3, 2, 2, 20, 2);
DPL.MergeTableCells(tableID3, 2, 3, 20, 3);
DPL.MergeTableCells(tableID3, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID3, 2, 1, content3);
DPL.DrawTableRows(tableID3, 270, 50, 700, 1, 0);
DPL.SaveToFile("tables.pdf");
You can find the description of the functions on this site:
http://www.debenu.com/docs/pdf_library_reference/CreateTable.php
I have an array fetched from mysql database tables of type. I want to sort it in order of particular value.
$arr1=array(array(12, 8, 5, 34),
array(54, 87, 32, 10),
array(23, 76, 98, 13),
array(53, 16, 24, 19));
How can i sort it by value?
Like sorting by 2nd value should result to.
$arr1=array(array(12, 8, 5, 34),
array(53, 16, 24, 19),
array(23, 76, 98, 13),
array(54, 87, 32, 10));
I like to use usort to solve these problems.
$sortKey = 1;
usort($arr1, function($a, $b) use($sortKey){
return $a[$sortKey] - $b[$sortKey];
});
Got to agree with #RocketHazmat, array_multsort is a royal pain in the backside. usort is much easier to follow but I thought I'd have a go anyway:
$sortKey = 1;
array_multisort(array_map(function($v) use($sortKey){
return $v[$sortKey];
}, $arr1), $arr1);
It only took 20 minutes... :(
Here's a demo: http://ideone.com/2rZYIz