
FixItPete
Novice
Feb 22, 2008, 7:17 AM
Post #3 of 24
(10754 views)
|
Re: [KevinR] Where to put code that can sort information in my .cgi file?
[In reply to]
|
Can't Post
|
|
Thanks for the reply Kevin... I just wanted to make sure I put the question in the right area. :) I have a script that at one point displays the results of information that it gets from a flat-file database. I would like to sort the results by the third field, which is the price field. A sample of the database row is as follows:
12345|Category|9.99|Short Item Title|More Info|Extra Info|Information Expanded||Color Information The subroutine that creates the html from the database information is as follows: (I know it is sloppy, and we're working to fix that too... I hope you'll understand.) :)
####################################################################### # create_html_page_from_db Subroutine # ####################################################################### # create_html_page_from_db is used to genererate the # navigational interface for database-base stores. It is # used to create both product pages and "list of products" # pages. The subroutine takes no arguments and is called # with the following syntax: # # &create_html_page_from_db; sub create_html_page_from_db { # First, the script defines a few working variables which # will remain local to this subroutine. local (@database_rows, @database_fields, @item_ids, @display_fields); local ($total_row_count, $id_index, $display_index); local ($row, $field, $empty, $option_tag, $option_location, $output); # Next the script checks to see if there is actually a # page which must be displayed. If there is a value for # the page variable incoming as form data, (ie: list of # product page) the script will simply display that page # with the display_page subroutine and exit. if ($page ne "" && $form_data{'search_request_button.x'} eq "" && $form_data{'continue_shopping_button.x'} eq "") { &display_page("$sc_html_product_directory_path/$form_data{'page'}", "Display Products for Sale", __FILE__, __LINE__); exit; } # If there is no page value, then the script knows that it # must generate a dynamic product page using the value of # the product form variable to query the database. # # First, the script uses the product_page_header # subroutine in order to dynamically generate the product # page header. We'll pass to the subroutine the value of # the page we have been asked to display so that it can # display something useful in the <TITLE></TITLE> area. # # The product_page_header subroutine is located in # web_store_html_lib.pl and $sc_product_display_title is # defined in the setup file. ## Remarked by Jimmy # &product_page_header($sc_product_display_title); if ($form_data{'add_to_cart_button.x'} ne "" && $sc_shall_i_let_client_know_item_added eq "yes") { print "$sc_item_ordered_message"; } # Next the database is querried for rows containing the # value of the incoming product variable in the correct # category as defined in web_store.setup. The script uses # the submit_query subroutine in web_store_db_lib.pl # passing to it a reference to the list array # database_rows. # # submit_query returns a descriptive status message # if there was a problem and a total row count # for diagnosing if the maximum rows returned # variable was exceeded. if (!($sc_db_lib_was_loaded =~ /yes/i)) { &require_supporting_libraries (__FILE__, __LINE__, "$sc_db_lib_path"); } ($status, $total_row_count) = &submit_query(*database_rows, $form_data{'hits_seen'}); $hits_seen = $form_data{'hits_seen'} + $sc_db_max_rows_returned; if ($total_row_count == 0) { &search_not_found_bac; exit; } else { ## Added By Jimmy ## &product_page_header($sc_product_display_title); ##$product_category =~ tr/._/ /; ###################################### #Category Page Headers from flat file# ###################################### open(prod_page_title,"$ENV{'DOCUMENT_ROOT'}/cgi-local/titleinfo.data")||die "Can't Read file"; @prodpagetitle=<prod_page_title>; close(prod_page_title); foreach $ttrec(@prodpagetitle){ ($orig_cat,$title_desc,$keytry,$metadesc)=split(/\|/,$ttrec); if ($orig_cat eq $product_category) { $product_category = $title_desc; $key_try = $keytry; $meta_desc = $metadesc; } else { $product_category=$product_category } } if ($product_category eq "") { &product_page_header($sc_product_display_title); } else { &product_page_header($product_category, $key_try, $meta_desc); } ########################################### #End Category Page Headers (for SEs)####### ########################################### ## Removed by PMT $hits_seen = $form_data{'hits_seen'} + $sc_db_max_rows_returned; # Now that the script has the database rows to be # displayed, it will display them. # # Firstly, the script goes through each database row # contained in @database_rows splitting it into it's # fields. # # For the most part, in order to display the database # rows, the script will simply need to take each field # from the database row and substitute it for a %s in the # format string defined in web_store.setup. # # However, in the case of options which will modify a # product, the script must grab the code from an options # file. # # The special way that options are denoted in the database # are by using the format %%OPTION%%option.html in the # data file. This string includes two important bits of # information. # # Firstly, it begins with %%OPTION%%. This is a flag # which will let the script know that it needs to deal # with this database field as if it were an option. When # it sees the flag, it will then look to the bit after the # flag to see which file it should load. Thus, in this # example, the script would load the file option.html for # display. # # Why go through all the trouble? Well basically, we need # to create a system which will handle large chunks of # HTML code within the database that are very likely to be # similar. If there are options on product pages, it is # likely that they are going to be repeated fairly # often. For example, every item in a database might have # an option like tape, cd or lp. By creating one # options.html file, we could easily put all the code into # one shared location and not need to worry about typing # it in for every single database entry. #print @database_rows; foreach $row (@database_rows) { @database_fields = split (/\|/, $row); foreach $field (@database_fields) #pmt play #print "$field"; { #print "$field\n"; ######TRYTRYTRY##### if ($field =~ /%%cart_id%%/) { $field =~ s/%%cart_id%%/$cart_id/g } # End of if ($field =~ /%%cart_id%%/) if ($field =~ /%%session_id_place%%/) { $field =~ s/%%session_id_place%%/$session_id_place/g } # End of if ($field =~ /%%session_id_place%%/) if ($field =~ /%%detail%%/) { $field =~ s/%%detail%%/$detail/g } # End of if ($field =~ /%%detail%%/) #####KEEP THIS HINT print $field; ############# # For every field in every database row, the script simply # checks to see if it begins (^) with %%OPTION%%. If so, # it splits out the string into three strings, one # empty, one equal to OPTION and one equal to the location # of the option to be used. Then the script resets the # field to null because it is about to overwrite it. if ($field =~ /^%%OPTION%%/) { ($empty, $option_tag, $option_location) = split (/%%/, $field); $field = ""; # The option file is then opened and read. Next, every # line of the option file is appended to the $field # variable and the file is closed again. However, the # current product id number is substituted for the # %%PRODUCT_ID%% flag open (OPTION_FILE, "<$sc_options_directory_path/$option_location") || &file_open_error ("$sc_options_directory_path/$option_location", "Display Products for Sale", __FILE__, __LINE__); while (<OPTION_FILE>) { s/%%PRODUCT_ID%%/$database_fields[$sc_db_index_of_product_id]/g; $field .= $_; } close (OPTION_FILE); } # End of if ($field =~ /^%%OPTION%%/) } # End of foreach $field (@database_fields) # Finally, the database fields (including the option field # which has been recreated) are stuffed into the format # string, $sc_product_display_row and the entire formatted # string is printed to the browser along with the footer. # # First, however, we must format the fields correctly. # Initially, @display_fields is created which contains the # values of every field to be displayed, including a # formatted price field. ####################################################### ######################################################## @display_fields = (); @temp_fields = @database_fields; if ($form_data{'detail'} eq "yes") { @sc_db_index_for_display = @sc_db_index_for_display_detail; $sc_product_display_row = $sc_product_display_row_new; } foreach $display_index (@sc_db_index_for_display) { if ($display_index == $sc_db_index_of_price) { $temp_fields[$sc_db_index_of_price] = &display_price($temp_fields[$sc_db_index_of_price]); } push(@display_fields, $temp_fields[$display_index]); } # Then, the elements of the NAME field are created so that # customers will be able to specify an item to purchase. # We are careful to substitute double quote marks ("), and # greater and less than signs (>,<) for the tags ~qq~, # ~gt~, and ~lt~. The reason that this must be done is so # that any double quote, greater than, or less than # characters used in URL strings can be stuffed safely # into the cart and passed as part of the NAME argumnet in # the "add item" form. Consider the following item name # which must include an image tag. # # <INPUT TYPE = "text" # NAME = "item-0010|Vowels|15.98|The letter A|~lt~IMG SRC = ~qq~Html/Images/a.jpg~qq~ ALIGN = ~qq~left~qq~~gt~" # # Notice that the URL must be edited. If it were not, how # would the browser understand how to interpret the form # tag? The form tag uses the double quote, greater # than, and less than characters in its own processing. @item_ids = (); foreach $id_index (@sc_db_index_for_defining_item_id) { $database_fields[$id_index] =~ s/\"/~qq~/g; $database_fields[$id_index] =~ s/\>/~gt~/g; $database_fields[$id_index] =~ s/\</~lt~/g; push(@item_ids, $database_fields[$id_index]); } # Finally, $sc_product_display_row is created with the two # arrays using printf to apply the formatting. # { #SreeJune sree_printf ($sc_product_display_row, join("\|",@item_ids), @display_fields); } } # End of foreach $row (@database_rows) if ($form_data{'detail'} eq "yes") { &product_page_footer_detail($status, $total_row_count, $hits_seen); # replaced authors $total_rows_returned } else { &product_page_footer($status, $total_row_count, $hits_seen); # replaced authors $total_rows_returned } } exit; } sub sree_printf { #Sree added on 18th June due to width="50%" misleading variables for %s my (@sreev3); @sreev3=@_; $sreev1=$sreev3[0]; shift(@sreev3); $sreev1=~s/\%\"/Z~Z\"/g; $sreev2=sprintf($sreev1,@sreev3); $sreev2=~s/Z~Z/\%/g; print $sreev2; } If you need any of the other subs, or anything, please let me know... I'm a bit new and hope to learn more as we go. Thanks, Pete
|