Gossamer Forum
Home : Products : DBMan SQL : Development, Plugins and Globals :

Count and show number of records from loged in user

Quote Reply
Count and show number of records from loged in user
Hi,
I use the following global to look from one of my tables the records of loged in user and format the result in a table inside a table within a form, and this works very well.
Now I need to show some where in my page the number of these records as a digit . Does any one has an idea how I would count the result within my global or use another global to get this number.
Please if you have any suggestion , reply to this post.
Thank you

My global:

sub {
my $tags = shift;
my $output;
my $loged_user = $tags->{Username};
my $table = $DB->table('mydb');
my $sth = $table->select (['colum1', 'colum2' ,'colum3','colum4','colum5','colum6'], {'owner' => $tags->{Username} } );
while (my $row = $sth->fetchrow_hashref) {
($output .= "<table width=595 border=0 cellspacing=1 cellpadding=0><tr bgcolor=#FFFFFF><td class=text2 width=20><input type=radio name=column1's name value=");
($output .= "$row->{colum1} ");
($output .= "></td><td class=title2 width=200>");
($output .= "$row->{colum1}");
($output .= "</td><td class=text2 align=center width=60>");
($output .= "\$");
($output .= "$row->{colum2}");
($output .= "</td><td class=text2 align=center width=60>");
($output .= "\$");
($output .= "$row->{colum3}");
($output .= "</td><td class=text2 align=center width=70>");
($output .= "$row->{colum4}");
($output .= "MB");
($output .= "</td><td class=text2 align=center width=75>");
($output .= "$row->{colum5}");
($output .= " GB");
($output .= "</td><td class=text2 align=center>");
($output .= "$row->{colum6}");
($output .= "</td></tr></table>");
}
return $output;
}
Ali
Quote Reply
Re: [megaline] Count and show number of records from loged in user In reply to
You can get the number of records by using $sth->rows.

By the way, you should change the global as below which is much nicer and easier for changing the template later:

sub {
my $tags = shift;
my $loged_user = $tags->{Username};
my $table = $DB->table('mydb');
my $results = $table->select (['colum1', 'colum2' ,'colum3','colum4','colum5','colum6'], {'owner' => $tags->{Username} } )->fetchall_hashref;
return { loop_results => $results, max_hits => $#$results + 1 };

The template should be like:
<%global_name%>
Records: <%max_hits%>
<%loop loop_results%>
<table...>
<tr><td>Column 1:</td><td><%column1%></tr>
<tr>....
</table>
<%endloop%>

Hope that helps.

TheStone.

B.
Quote Reply
Re: [TheStone] Count and show number of records from loged in user In reply to
Thank you stone,
It is very clean and the same global I can use to show only record number or results together or seperatly. I just had to add the bracket ( shown in red ) at the end of global.

This global works with latest version of DBMan SQL ( 8 july 2004)
sub {
my $tags = shift;
my $loged_user = $tags->{Username};
my $table = $DB->table('mydb');
my $results = $table->select (['colum1', 'colum2' ,'colum3','colum4','colum5','colum6'], {'owner' => $tags->{Username} } )->fetchall_hashref;
return { loop_results => $results, max_hits => $#$results + 1
}
}
;
Ali