{"id":1060,"date":"2014-05-28T15:33:56","date_gmt":"2014-05-28T19:33:56","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1060"},"modified":"2016-12-15T21:55:44","modified_gmt":"2016-12-16T02:55:44","slug":"custom-sort","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1060","title":{"rendered":"Custom Sort"},"content":{"rendered":"<p><span style=\"background:yellow\"<strong>New<\/strong>: as of version 2.8, easier and more sorting options are available via <a href=\"http:\/\/cfdbplugin.com\/?page_id=1135\">sorting with Transforms<\/a><\/span>.<\/p>\n<p>Issue: how to create a custom sort when <a href=\"http:\/\/cfdbplugin.com\/?page_id=444\">creating your own short code<\/a>.<\/p>\n<p>This code requires PHP 4.2 or later.<\/p>\n<p>Example: sort form entries by &#8220;age&#8221; field, treating all the values as numbers.<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\/\/ first add a sorting callback function.\r\n\/\/ Set the $field to the field you want\r\n\/**\r\n * Custom sorting function for a particular form\r\n * to be used as in input to uasort()\r\n * Requires PHP 4.2.0 or later\r\n * @param $a array (associative)\r\n * @param $b array (associative)\r\n * @return int -1 for $a&lt;$b, 0 for $a==$b, 1 for $a&gt;$b\r\n *\/\r\nfunction mysort($a, $b) {\r\n  $field = 'age'; \/\/ Change to your field name to sort on\r\n  $aVal = floatval($a[$field]);\r\n  $bVal = floatval($b[$field]);\r\n  if ($aVal == $bVal) {\r\n    return 0;\r\n    \/\/ or sort by a secondary field here\r\n  }\r\n  \/\/ for DESC sort change &lt; to &gt; below\r\n  return $aVal &lt; $bVal ? -1 : 1;\r\n}\r\n\r\n\/\/ Add the regular code here from \"Create your own short code\" page\r\nrequire_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\n$rows = array();\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n  \/\/ instead of creating output, just save the data into an array\r\n  $rows[] = $row;\r\n}\r\n\/\/ Sort the array using the sort callback function\r\nuasort($rows, 'mysort');\r\nforeach($rows as $row) {\r\n  \/\/\u00a0Replace with your code to output data here\r\n  echo '&lt;pre&gt;';\r\n  print_r($row);\r\n  echo '&lt;\/pre&gt;';\r\n}&lt;\/pre&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":1577,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1060","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-h6","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1060"}],"version-history":[{"count":6,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1060\/revisions"}],"predecessor-version":[{"id":1208,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1060\/revisions\/1208"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1577"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}