Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

display array value in template

Quote Reply
display array value in template
  


sub {
my $output;
my $sth = $DB->table('tabletest')->select( { id => "idnumber" } );
while (my $rec = $sth->fetchrow_hashref ) {
$output .= Links::SiteHTML::display('8selecttest', $rec );
}
return $output;
}

there is a colum called orderdetail
its value looks like this {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}

it i the tempalate 8selecttest.html looks like this
<%column1%>
<%orderdetail%>

then i will get a array value of ordertail {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}


how could i get it displayed like this format?
<%column1%>
name=name111 colour= red
name=name222 colour= yellow



i do hope i could make it. but i dont how here to modify it.
i hope i could get help here. or i have to do it in a stupid way holding the <%orderdetail%> value in a hidden field and do a futher process

thanks in advance
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

This may or may not work - not had time to test.

get_values_from_order
Code:
sub {

# splits them up into stuff like "{name => 'name222',colour => 'yellow'}"
my @split = split /},{/, $_[0];

my @loop;
foreach (@split) {
my $hash;
my @tmp = split /',/, $_; # now lets split them at ',

my @subloop;
foreach my $tmp (@tmp) {
my ($name,$value) = split / => /, $tmp;
$hash->{name} = $name;
$hash->{value} = $value;
$hash->{value} =~ s/^\'//;
$hash->{value} =~ s/\'$//;
push @subloop, $hash;
}

push @loop, { subloop => \@subloop };
}

return { split_loop => \@loop };

}

Then call with:

Code:
<%get_values_from_order($orderdetail)%>
<%if split_loop.length%>
<%loop split_loop%>
<%loop subloop%>
<%name%> = <%value%> <%ifnot last%>, <%endif%>
<%endloop%>
<%endloop%>
<%endif%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] display array value in template In reply to
Thanks Andy. i will have a try later as now i am on laptop. i cant test it on laptop now.


just now i look through GT help file again it shows below





Consider the following more complex example, with a people variable set to the following loop:

Code:


[ { name => "John Doe", age => 35, hair => "no", phone => { work => "(555) 555-5678", home => "(555) 555-6789" } },
{ name => "Jane Doe", age => 25, hair => "brown", phone => { work => "(555) 555-5678", home => "(555) 555-1234" } } ]

The following template code:
<%loop person%><%row_num%>. <%name%>, <%age%> years of age, <%hair%> hair. Phone: work: <%phone.work%>, home: <%phone.home%>. <%endloop%>

The first person on the list, <%person.0.name%>, can be reached at either <%person.0.phone.work%> or <%person.0.phone.home%>.

Will display:
1. John Doe, 35 years of age, no hair. Phone: work: (555) 555-5678, home: (555) 555-6789.
2. Jane Doe, 25 years of age, brown hair. Phone: work: (555) 555-5678, home: (555) 555-1234.
The first person on the list, John Doe, can be reached at either (555) 555-5678 or (555) 555-6789.










From my understand , if i could i could it do in this way

Code:

sub {
my $output;
my $sth = $DB->table('tabletest')->select( { id => "idnumber" } );
while (my $rec = $sth->fetchrow_hashref ) {
$output .= Links::SiteHTML::display('8selecttest', $rec );
}
return $output;
}



let us say there are tree columns in table "tabletest" called id, column1 and orderdetailorderdetail
orderdetails value looks like this {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}

so i could called it later in the tempalate as

Code:
<%column1%>
<%orderdetail%> ####which will display unreadable data {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}




or call it
Code:
<%column1%>
<%loop orderdetail%><%row_num%>. <%name%>, <%colour%> <%endloop%>





is that correct?
how could could i creat a loop code in the sub??
Thanks in advance

Last edited by:

courierb: Jul 23, 2009, 9:52 AM
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

The other solution your suggested shouldn't work. UIt relies on the data being passed in, to actually be a hash (which it isn't, as its just a text string). If my global works, I would use that =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [courierb] display array value in template In reply to
dear Andy

i got a error when run your global

Variable 'get_values_from_order' is not a code reference
what is wrong with it?
Quote Reply
Re: [courierb] display array value in template In reply to
Do you have any spaces, and stuff before the:

sub {

..and the last:

}

?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [courierb] display array value in template In reply to
BTW, I've just tested the global - and it doesn't work how I expected it to.

Just trying to get it working now ;)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [courierb] display array value in template In reply to
Ok, just did some debugging - and found the problem. This should work fine:

Code:
sub {

# splits them up into stuff like "{name => 'name222',colour => 'yellow'}"
my @split = split /\Q},{/, $_[0];

my @loop;
foreach (@split) {
s/^{//;
s/}$//;
my $hash;
my @tmp = split /,/, $_; # now lets split them at ',

my @subloop;

foreach my $tmp (@tmp) {
my $hash;
my ($name,$value) = split / => /, $tmp;

$hash->{name} = $name;
$hash->{value} = $value;
$hash->{value} =~ s/^\'//;
$hash->{value} =~ s/\'$//;
push @subloop, $hash;
}

push @loop, { subloop => \@subloop };
}

return { split_loop => \@loop };

}

Tested it my end, and it gives me the data I was expecting now:

Code:
Dumped value of 'split_loop':

[
{
'subloop' => [
{
'name' => 'name',
'value' => 'name111'
},
{
'name' => 'colour',
'value' => 'red'
}
]
},
{
'subloop' => [
{
'name' => 'name',
'value' => 'name222'
},
{
'name' => 'colour',
'value' => 'yellow'
}
]
}
];

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] display array value in template In reply to
HI Andy, thanks for the hep. i have not yet get time tested. i will go for a test tonight.

Thanks again.
Quote Reply
Re: [courierb] display array value in template In reply to
HI andy

i got nothing printed o out i wonder if i am doing something wrong? i thing $orderdetail is not passing correctly to the code. i may missing something there. could you remind me of it?


array value of ordertail {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}
there is a column called ordertail on the table
the sub name called get_values_from_order
sub {
# splits them up into stuff like "{name => 'name222',colour => 'yellow'}"
my @split = split /\Q},{/, $_[0];
my @loop;
foreach (@split) {
s/^{//;
s/}$//;
my $hash;
my @tmp = split /,/, $_; # now lets split them at ',
my @subloop;
foreach my $tmp (@tmp) {
my $hash;
my ($name,$value) = split / => /, $tmp;
$hash->{name} = $name;
$hash->{value} = $value;
$hash->{value} =~ s/^\'//;
$hash->{value} =~ s/\'$//;
push @subloop, $hash;
}
push @loop, { subloop => \@subloop };
}
return { split_loop => \@loop };
}

i called below code on detail page. and it has no error now. but have nothing print out.
<%get_values_from_order($orderdetail)%>
<%if split_loop.length%>
<%loop split_loop%>
<%loop subloop%>
<%name%> = <%value%> <%ifnot last%>, <%endif%>
<%endloop%> <%endloop%> <%endif%>

Last edited by:

courierb: Jul 25, 2009, 8:18 PM
Quote Reply
Re: [courierb] display array value in template In reply to
What do you see if you put:

FOO TEST: <%orderdetail%>

..on that page?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Post deleted by courierb In reply to
Quote Reply
Re: [Andy] display array value in template In reply to
if i put <%orderdetail%> on template. i got unknow tag error


should i modified here to make it a loop ?

sub {
my $output;
my $sth = $DB->table('tabletest')->select( { id => "idnumber" } );
while (my $rec = $sth->fetchrow_hashref ) {
###$output .= Links::SiteHTML::display('8selecttest', $rec );
push (@output, $rec );

}
#return $output;
return { 8selectloop=> \@output };
}


?




Quote Reply
Re: [courierb] display array value in template In reply to
Andy.

Finally i test it out.


it is a loop under a loop.
or i will get nothing printed.


Thank you very much andy.
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

Cool, so its working now then?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] display array value in template In reply to
  

yes yes yes , it is working now.

thanks you Andy.

now i am tring to format the layout of output. it seems i still got headache on it

as some values has more options in it for example colour. size.
while some has less option.
Quote Reply
Re: [Andy] display array value in template In reply to
  
Andy i have one more question. how do i read data in this case?

Thanks in advance.


sub {

# splits them up into stuff like "{name => 'name222',colour => 'yellow'}"
my @split = split /},{/, $_[0]; # is it read first block of data?


#### how to read whole data ?

##i try to read all data {name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}
my @readall = $_[0]; ,,$_[1]; ##? what is the correct syntax? what if i have 3,4,5 blocks of data



}
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

I'm not exactly sure what you are asking. Do you want to split this:

{name => 'name111',colour => 'red'},{name => 'name222',colour => 'yellow'}

..into sections - i.e;'

{name => 'name111',colour => 'red'}
..and:
,{name => 'name222',colour => 'yellow'}

?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] display array value in template In reply to
Andy

what you do in the global above is split it into

{name => 'name111',colour => 'red'}
..and:
,{name => 'name222',colour => 'yellow'}

?




how to read it as a whole and defined it to $alldata




Cheers
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

Sorry, I still don't understand what you are asking :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [courierb] display array value in template In reply to
  


Hi Andy


i am had headache on how to format your golobal layout as well we calculation of the sub total and total




base on your concept. i modiy the cart script to make a global. if it workable? now i do not know how to pass
ordertail column string data to @orderdetail_column. could you take a look it for me?

that is the question i asked you early how to pass the whole orderdetail column data to the global .
if i replay @orderdetail_column by @partofcookie it seems work.

Code:

sub { ####sub 8mysqlorderdetail ####### mysqlorderdetail column data

my ($cgi, $session,$product,$tags,$itemmaxtemp,$itemID) = @_;
my @orderdetail_column; ###########????????????????? then how get the data from order column orderdetail?
#my @orderdetail_column = split /\Q},{/, $_[0]; ###????

my @partofcookie= ({name => 'ddd',quantity => 6,price => '15',colour => 'red',size => 'xxl',itemID => 15},{quantity => '1',name => 'gmailoknow',price => '8',itemID => '34'});
#my @partofcookie= @{$session->param('CART')}; ############## have to defined here or produce error:undefined value as an ARRAY reference
foreach my $product ( @orderdetail_column) { ############test
$product->{prod_subtotal} = $product->{price} * $product->{quantity};
$tags->{total_price} += $product->{prod_subtotal};
$tags->{itemmaxtemp}=$itemmaxtemp;
$tags->{itemID}=$itemID;
push @{$tags->{cart_loop}}, $product;
}
my @xxx;
@xxx = @{$tags->{cart_loop}};
return { orderdetail_loop => \@xxx};
}


<%8mysqlorderdetail($orderdetail)%>
<%loop orderdetail_loop%> <br>
price=<%price%> <br>
name= <%name%> <br>
quantity=<%quantity%> <br>
colour=<%colour%><br>
itemID=<%itemID%><br>
subtotal=<%prod_subtotal%>

<br><br><br><%ifnot last%>, <br><br><br>
<%endif%>
<%endloop%>
Quote Reply
Re: [courierb] display array value in template In reply to
Hi,

Sorry, I really can't dedicate any more time to this :( (already spent ages getting that other global working for you)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] display array value in template In reply to
HI andy

Anyway. thanks for your help.

i appreciated your helping.

have a nice day