CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Where to put code that can sort information in my .cgi file?

 



FixItPete
Novice

Feb 20, 2008, 5:32 AM

Post #1 of 24 (4826 views)
Where to put code that can sort information in my .cgi file? Can't Post

Hi,

I'm trying to figure out where (and what) to insert in a perl script that will allow the results to be sorted by price. The script calls other files... I can post the main .cgi file and post whatever is needed from there... is this the right area of the forum to do this?


Thanks,
Pete


KevinR
Veteran


Feb 20, 2008, 10:51 AM

Post #2 of 24 (4824 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Nobody can help you without seeing your code, so if you want to try and get help here you will have to post your code. Make sure to wrap your code in code tags.
-------------------------------------------------


FixItPete
Novice

Feb 22, 2008, 7:17 AM

Post #3 of 24 (4815 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:


Code
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.) :)


Code
 
#######################################################################
# 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


KevinR
Veteran


Feb 22, 2008, 10:09 AM

Post #4 of 24 (4811 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

I already tried to help you on Tek-Tips. Maybe someone here will give it a try.
-------------------------------------------------


FixItPete
Novice

Feb 22, 2008, 11:07 AM

Post #5 of 24 (4810 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Ah, yea... thanks... (I read your replies. From what I see, you gave me a new sub to add, but then didn't tell me where to call it.) - or did I miss that? ;)


FixItPete
Novice

Feb 22, 2008, 11:10 AM

Post #6 of 24 (4808 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

P.S. (I posted the data (flat-file) you asked for in this thread -- but I see you didn't reply).


KevinR
Veteran


Feb 22, 2008, 2:28 PM

Post #7 of 24 (4805 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Yes you missed it.
-------------------------------------------------


FixItPete
Novice

Feb 22, 2008, 6:06 PM

Post #8 of 24 (4802 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Actually I just re-read it again, for the fourth time, and had another person read it. We both agree, you provided a single subroutine... along with an unhelpful dose of superiority because you know how to do something that I don't.

I may be new here, but you, "friend" (as you like to say), don't seem to offer much in the way of help... yet you're mighty quick to have a sig that mentions donations. Why on earth would anyone give you a donation when you can't bite your lip and actually offer up some help.

I didn't know how or WHERE to call the sub. I posted a more organized and clearer explanation of what I wanted to do along with a sample of the flatfile record that is read.

YOU decided to be combative and offer little. Like I said, I may be new here, but I see you're not the type of person that is going to help with much.

Please leave the thread alone and perhaps a person that is willing to take a moment and lend a hand, a person that is more deserving of a donation for sure!, will come along.

Good Day!


KevinR
Veteran


Feb 22, 2008, 7:17 PM

Post #9 of 24 (4799 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Thanks, best laugh I've had in a few days. I'm printing this one out and saving it. Smile
-------------------------------------------------


KevinR
Veteran


Feb 22, 2008, 7:26 PM

Post #10 of 24 (4796 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

try over on devshed:

http://forums.devshed.com/perl-programming-6/

There are a couple of members there that like a challenge. No need to thank me for the suggestion you've already made my day with your last post. Still laughing at that one. Wink
-------------------------------------------------


FixItPete
Novice

Feb 23, 2008, 4:59 AM

Post #11 of 24 (4789 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

I'll post there... just do me a favor and don't bother stepping into that thread too... it seems when you step in it goes to trash. I read some of your other replies and it looks like you actually furnish a serious/complete answer about 10% of the time. Most of the time you tell the person one of two things:

If they gave too much code you tell them people don't want to see that much code.

If they didn't give any code you tell them you need more code.

The way you've responded to my threads proves that point.

You're right -- it has been a good laugh. At you -- and your previous replies prove it.

Thanks!


KevinR
Veteran


Feb 23, 2008, 10:59 AM

Post #12 of 24 (4784 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Sowwy that the free help I give is not up to you standards. Wink
-------------------------------------------------


FixItPete
Novice

Feb 23, 2008, 4:25 PM

Post #13 of 24 (4776 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Ha! That's just the point you're not getting. You don't offer any help. I just got read 3 or 4 threads on tek-tips (since you decided to bring it up!) and in all three, you replied... with nothing.

I one thread you asked a stupid question about code -- code the OP didn't have! Luckily, 2 other people replied and helped steer the person on the right track.

Friend, you're not offering help. When you first replied to this thread, you seemed like you were going to help -- and you didn't. That seems to be you MO -- but in the end -- you offer zero.

Just don't bother with my threads. Let me learn from a person who KNOWS, not a person who likes to think he does.


KevinR
Veteran


Feb 23, 2008, 7:18 PM

Post #14 of 24 (4774 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Heres your refund: $0.00, Sorry it's not more but thats what you paid. First refund I've had to give in 10 years but theres always a first time. Smile
-------------------------------------------------


FixItPete
Novice

Feb 24, 2008, 5:53 AM

Post #15 of 24 (4770 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

(Looks like Sir HelpsNot didn't get it the first time.)

Ha! That's just the point you're not getting. You don't offer any help. I just got read 3 or 4 threads on tek-tips (since you decided to bring it up!) and in all three, you replied... with nothing.

I one thread you asked a stupid question about code -- code the OP didn't have! Luckily, 2 other people replied and helped steer the person on the right track.

Friend, you're not offering help. When you first replied to this thread, you seemed like you were going to help -- and you didn't. That seems to be you MO -- but in the end -- you offer zero.

Just don't bother with my threads. Let me learn from a person who KNOWS, not a person who likes to think he does.


KevinR
Veteran


Feb 24, 2008, 12:10 PM

Post #16 of 24 (4768 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

word Wink
-------------------------------------------------


KevinR
Veteran


Feb 24, 2008, 6:28 PM

Post #17 of 24 (4762 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Come on perlgurus out there, can't you see PitiFulPete needs some help? I'd give it a try but I don't really know any perl, I just like to pretend I do because all the hot chicks dig perl coders. Cool
-------------------------------------------------


FixItPete
Novice

Feb 24, 2008, 7:37 PM

Post #18 of 24 (4761 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post


In Reply To
I don't really know any perl, I just like to pretend I do



KevinR
Veteran


Feb 24, 2008, 10:27 PM

Post #19 of 24 (4757 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Yes, you have discovered my secret, I'm actually a COBOL programmer in a Perlish disguise. Cool

But damn those Perl coders got all the good looking babes, so you can't blame me can ya? Wink

Here it is again for you to try and figure out:



Code
   @database_rows = map {$_->[0]} 
sort {$a->[1] <=> $b->[1]}
map {[$_,(split(/\|/))[0]]} @data_sorted;


Just put it anywhere in your code, your bound to find the right spot sooner or later, even a broken clock is right twice a day. Crazy
-------------------------------------------------


KevinR
Veteran


Feb 24, 2008, 10:39 PM

Post #20 of 24 (4755 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

I found just what you need PitiFulPete Perl for Dummies. No need to thank me mate, thats what friends are for. Smile
-------------------------------------------------


FixItPete
Novice

Feb 25, 2008, 4:37 AM

Post #21 of 24 (4748 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Kevin,

A) You never gave that code before... either on this forum on or on Tek-Tips.

B) I have tried to put that code between every line of code in that sub... and it either didn't change anything or caused the script to quit.

If you *know* where it is suppose to go, could you please tell me, and I'll execute exactly what you say. If you don't know... then that is cool -- I *DO* appreciate the attempt, and no-harm-no-foul.

Thanks,
Pete


KevinR
Veteran


Feb 25, 2008, 10:19 AM

Post #22 of 24 (4742 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

It has been on Tek-Tips the whole time. There is no way to edit posts on Tek-Tips so it's not like I could have added it after the fact.

Feel free to call me any names or tell me how little I know about Perl because this is my last post to you. Thanks for the laughs.
-------------------------------------------------


FixItPete
Novice

Feb 25, 2008, 10:32 AM

Post #23 of 24 (4741 views)
Re: [KevinR] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Wonderful. I'm sure you had fun laughing at me. You know, especially how you got me to believe that you knew how to do something and you really didn't -- and then you got me to actually think that you would really help with a problem.

yeah -- I guess that must have been funny for you.

In case you needed a reminder... this is a forum for people to help other people with Perl, Here let me help more:



Home: Perl Programming Help: Beginner:


^^^^^^^^^^^^^^

That's the title of the forum.

There ya go.

(Now do what you said and DO NOT come back to a thread I start!)


FixItPete
Novice

Feb 25, 2008, 7:42 PM

Post #24 of 24 (4734 views)
Re: [FixItPete] Where to put code that can sort information in my .cgi file? [In reply to] Can't Post

Ahhh. now we can get back to code and no more of the nonsense...

So far I am able to insert this:


Code
 foreach $row (@database_rows) 
{
@tmp = split('\|',$row);
$modified_row = join("\|",$tmp[2],$row);
push (@price_first_rows, $modified_row); } @sorted_rows = sort {$a <=> $b} @price_first_rows; # for ascending numeric

foreach $sorted_row (@sorted_rows)
{
@tmp = split('\|',$sorted_row);
shift @tmp;
$final_ordered_row = join("\|",@tmp); # first element gone, now
push (@final_ordered_rows, $final_ordered_row); }

@database_rows = @final_ordered_rows;


and it tries to sort... what I mean is this. Say you have 3 pages of results...

The first page will be sorted in an of itself, but it won't take into consider what is going on on the other pages. So looking at price you get pages like this.

Page one:

$1
$1
$1
$1
$1
$2
$2
$2

Page two
$2
$2
$2
$1
$1
.
.
.

So logic would tell me that we need to sort the db data BEFORE it hits the code that arranges it into these pages... well... that is where I'm at, and having zero luck...

In case anyone hasn't been totally turned off by the nonesense of one of the members here... I would love to try to work on this.

Thanks for any help,
Pete

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives