Improvements in sort feature.

H

Harit Nanavati

Guest
I love the script. I've just started using it (PostNuke module). One feature I had to have was to be able to order the columns in both direction. The problem with the original script was that it can only sort ascending and there was no way to reverse the sort order. I went ahead and modified the script so that it would be possible to sort in both ways. It turned out to be a pretty easy fix. Thanks to clean modular code. You can see the demo at http://harit.com/post click on database. I would think this addition would be a very desirable feature to be included in the script. I would post the patch here but this forum doesn't seem to allow attachments. If anyone is interested I would be happy to share the code just send me an email. Changes are mainly in two files form.php and business_logic.php. I also had to add two gifs for ascending and descending arrows.

 
H

Harit Nanavati

Guest
Here are the patch using diff -cN. You will need to get ascend.gif and descend.gif from http://harit.com/post and put them in images directory. This patches are for *PostNuke* module. I haven't tested them with regular script

*** form.php Sat Dec 13 21:57:38 2003
--- form.php.modified Wed Jan 14 20:12:36 2004
***************
*** 160,165 ****
--- 160,167 ----
from modules.php?op=modload&name=".$module_name."&file=form (from the search form or from the navigation tool), from 0......n
$order
the field used to order the form
+ $ascdsc
+ the field used to indicate whether to sort in ascending or descending order. values are 'asc' or 'desc'
$function
the function: search/update/insert/update......
***************************************************/
***************
*** 197,202 ****
--- 199,211 ----
$order = "";
} // end else

+ if (isset($HTTP_GET_VARS["ascdsc"])){
+ $ascdsc = stripslashes($HTTP_GET_VARS["ascdsc"]);
+ } // end
+ else{
+ $ascdsc = "";
+ } // end else
+
if (isset($HTTP_GET_VARS["function"])){ // from the homepage
$function = $HTTP_GET_VARS["function"];
} // end
***************
*** 310,316 ****
$results_type = "possible_duplication";
$select_without_limit = ""; // I don't need it here, I've just a fixed number of results.

! $results_table = build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, "", "", $action, $select_without_limit, "", "");

display_message ($normal_messages_ar["similar_records"], "", "", "");

--- 319,325 ----
$results_type = "possible_duplication";
$select_without_limit = ""; // I don't need it here, I've just a fixed number of results.

! $results_table = build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, "", "", $action, $select_without_limit, $order, $ascdsc);

display_message ($normal_messages_ar["similar_records"], "", "", "");

***************
*** 379,391 ****
$select_without_limit .= $sqldadabik;
} // end if
} // end else
!
$sqldadabik = $select_without_limit;

if ($order != ""){
$sqldadabik .= " order by ".$quote.$order.$quote;
} // end if
!
// execute the select without limit query to get the number of results
$res_contacts_without_limit = execute_db("$select_without_limit", $conn);

--- 388,403 ----
$select_without_limit .= $sqldadabik;
} // end if
} // end else
!
$sqldadabik = $select_without_limit;

if ($order != ""){
$sqldadabik .= " order by ".$quote.$order.$quote;
} // end if
!
! if ($ascdsc != ""){
! $sqldadabik .= " ".$ascdsc;
! } // end if
// execute the select without limit query to get the number of results
$res_contacts_without_limit = execute_db("$select_without_limit", $conn);

***************
*** 420,426 ****
$results_type = "search";

// build the HTML results table
! $results_table = build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, "", "", $action, $select_without_limit, $page, $order);

// display the HTML results table
echo $results_table;
--- 432,438 ----
$results_type = "search";

// build the HTML results table
! $results_table = build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, "", "", $action, $select_without_limit, $page, $order, $ascdsc);

// display the HTML results table
echo $results_table;
***************
*** 622,625 ****
// include footer
include("modules/".$module_name."/include/footer.php");
include("footer.php");
! ?>
\ No newline at end of file
--- 634,637 ----
// include footer
include("modules/".$module_name."/include/footer.php");
include("footer.php");
! ?>












*** config.php Sat Dec 13 23:06:36 2003
--- config.php.modified Thu Jan 15 14:13:49 2004
***************
*** 140,145 ****
--- 140,149 ----
$cms_printer_icon = "modules/".$module_name."/images/cms_printer.gif";
$cms_friend_icon = "modules/".$module_name."/images/cms_friend.gif";

+ //icons for displaying sort order
+ $order_asc_icon = "modules/".$module_name."/images/ascend.gif";
+ $order_desc_icon = "modules/".$module_name."/images/descend.gif";
+
// hack e-mail notification on data [entry / update] : (0/1)
// add the variable $mail_notification_on_entry as global in function insert_record()
// add the variable $mail_notification_on_update as global in function update_record()
***************
*** 169,172 ****



! ?>
\ No newline at end of file
--- 173,176 ----



! ?>















*** business_logic.php Sat Dec 13 17:28:56 2003
--- business_logic.php.modified Thu Jan 15 14:12:16 2004
***************
*** 1635,1648 ****
return $field_to_display;
} // function get_field_correct_displaying

! function build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, $name_mailing, $page, $action, $sqldadabik, $page, $order)
// goal: build an HTML table for basicly displaying the results of a select query or show a check mailing results
// input: $conn, $db_name, $table_name, $table_internal_name, $res_contacts, the results of the query, $results_type (search, possible_duplication, check_mailing......), $name_mailing, the name of the current mailing, $page, the results page (useful for check mailing), $action (modules.php?op=modload&name=--module_name--&file=form or mail.php), $sqldadabik (the sql clause), $page (o......n), $order
// output: $results_table, the HTML results table
// global: $submit_buttons_ar, the array containing the values of the submit buttons, $edit_target_window, the target window for edit/details (self, new......), $delete_icon, $edit_icon, $details_icon (the image files to use as icons), $enable_edit, $enable_delete, $enable_details (whether to enable (1) or not (0) the edit, delete and details features
{

! global $submit_buttons_ar, $edit_target_window, $delete_icon, $edit_icon, $details_icon, $enable_edit, $enable_delete, $enable_details, $table_name, $quote, $module_name, $youareadmin, $function;

// hack added $module_name, $youareadmin, $function; as global values

--- 1635,1648 ----
return $field_to_display;
} // function get_field_correct_displaying

! function build_results_table($conn, $db_name, $table_name, $table_internal_name, $res_contacts, $results_type, $name_mailing, $page, $action, $sqldadabik, $page, $order, $ascdsc)
// goal: build an HTML table for basicly displaying the results of a select query or show a check mailing results
// input: $conn, $db_name, $table_name, $table_internal_name, $res_contacts, the results of the query, $results_type (search, possible_duplication, check_mailing......), $name_mailing, the name of the current mailing, $page, the results page (useful for check mailing), $action (modules.php?op=modload&name=--module_name--&file=form or mail.php), $sqldadabik (the sql clause), $page (o......n), $order
// output: $results_table, the HTML results table
// global: $submit_buttons_ar, the array containing the values of the submit buttons, $edit_target_window, the target window for edit/details (self, new......), $delete_icon, $edit_icon, $details_icon (the image files to use as icons), $enable_edit, $enable_delete, $enable_details (whether to enable (1) or not (0) the edit, delete and details features
{

! global $submit_buttons_ar, $edit_target_window, $delete_icon, $edit_icon, $details_icon, $order_asc_icon, $order_desc_icon, $enable_edit, $enable_delete, $enable_details, $table_name, $quote, $module_name, $youareadmin, $function;

// hack added $module_name, $youareadmin, $function; as global values

***************
*** 1675,1682 ****
for ($i=0; $i<count($fields_labels_ar); $i++){
if ($fields_labels_ar[$i]["present_results_search_field"] == "1"){ // the user want to display the field in the basic search results page
$results_table .= "<th class=\"results\">";
! if ($order != $fields_labels_ar[$i]["name_field"] and $results_type == "search" or $results_type == "check_mailing"){ // the results are not ordered by this field at the moment
! $results_table .= "<a class=\"order\" href=\"$action&table_name=".urlencode($table_name)."&function=$function&sqldadabik=".urlencode($sqldadabik)."&name_mailing=".urlencode($name_mailing)."&page=$page&order=".urlencode($fields_labels_ar[$i]["name_field"])."\">".$fields_labels_ar[$i]["label_field"]."</a></th>"; // insert the linked name of the field in the <th>
} // end if
else{
$results_table .= $fields_labels_ar[$i]["label_field"]."</th>"; // insert the name of the field in the <th>
--- 1675,1697 ----
for ($i=0; $i<count($fields_labels_ar); $i++){
if ($fields_labels_ar[$i]["present_results_search_field"] == "1"){ // the user want to display the field in the basic search results page
$results_table .= "<th class=\"results\">";
! $temp_ascdsc = "asc";
! $temp_order_img = "";
! if($order == $fields_labels_ar[$i]["name_field"])
! {
! if($ascdsc == "asc")
! {
! $temp_ascdsc = "desc";
! $temp_order_img= "<img border=\"0\" src=\"$order_asc_icon\">";
! }
! else
! {
! $temp_order_img = "<img border=\"0\" src=\"$order_desc_icon\">";
! }
! }
!
! if ($results_type == "search" or $results_type == "check_mailing"){ // the results are not ordered by this field at the moment
! $results_table .= "<a class=\"order\" href=\"$action&table_name=".urlencode($table_name)."&function=$function&sqldadabik=".urlencode($sqldadabik)."&name_mailing=".urlencode($name_mailing)."&page=$page&order=".urlencode($fields_labels_ar[$i]["name_field"])."&ascdsc=".$temp_ascdsc."\">".$fields_labels_ar[$i]["label_field"]." $temp_order_img</a></th>"; // insert the linked name of the field in the <th>
} // end if
else{
$results_table .= $fields_labels_ar[$i]["label_field"]."</th>"; // insert the name of the field in the <th>
***************
*** 2223,2226 ****

return $valid_file_name;

! } // end function get_valid_name_uploaded_file ($file_name)
\ No newline at end of file
--- 2238,2241 ----

return $valid_file_name;

! } // end function get_valid_name_uploaded_file ($file_name)
 
Top