Hello I've just entered the PERL WORLD,a starter.I wrote a quick DataBound Grid using perl that you can use in any of your web applications.Just give the Data Grid the DataBase statement handle ,already executed , and it'll display all the data neatly.You can also set the styles for the Grid like the header forecolor and back color ,row back color and forecolor .To give a really catchy look i've also added an alternating style capability where each alternating row can have different forecolors and backcolor .Even turn on/off the headers .I'll be adding the paging mechanism to the grid later on for easing browsing through thousands of records. here is the code listing .just import the DataGrid module and get started.
####################My Simple Data Grid ################ package DataGrid; sub new { my $obj={}; $obj->{"Cols"}=2; $obj->{"Rows"}=2; $obj->{"SHOW_HEADER"}=1; $obj->{"AlternatingRowForeColor"}="black"; $obj->{"AlternatingRowBackColor"}="white"; $obj->{"RowForeColor"} ="black"; $obj->{"RowBackColor"}="white"; $obj->{"IsAlternating"}=0; $obj->{"HEADER_COLOR"}="black"; $obj->{"HEADER_BACKCOLOR"}="white"; bless $obj,'DataGrid'; return $obj; } sub AlternatingRowForeColor { my $obj=$_[0]; $obj->{"AlternatingRowForeColor"}=$_1; } sub RowForeColor { my $obj=$_[0]; $obj->{"RowForeColor"}=$_1; } sub RowBackColor { my $obj=$_[0]; $obj->{"RowBackColor"}=$_1; } sub AlternatingRowBackColor { my $obj=$_[0]; $obj->{"AlternatingRowBackColor"}=$_1; } sub IsAlternating { my $obj=$_[0]; $obj->{"IsAlternating"}=$_1; } sub HeaderColor { my $obj=$_[0]; $obj->{"HEADER_COLOR"}=$_1; } sub HeaderBackGround { my $obj=$_[0]; $obj->{"HEADER_BACKCOLOR"}=$_1; } sub HideHeader { my $obj=$_[0]; $obj->{"SHOW_HEADER"}=0; } sub ShowHeader { my $obj=$_[0]; $obj->{"SHOW_HEADER"}=1; } sub DataSource { my $obj=$_[0]; my $sth=$_1; $obj->{"STH"}=$sth; } sub Render { my $obj=$_[0]; my $html=""; my $rows=$obj->{"Rows"}; my $cols=$obj->{"Cols"}; my $alter=0; if($obj->{"STH"}) { my $sth=$obj->{"STH"}; my $arrRef=$sth->fetchrow_hashref; $html.="<table border=1>"; if($obj->{"SHOW_HEADER"}==1) { $html.="<tr style=\" color:$obj->{HEADER_COLOR};background:$obj->{HEAD +ER_BACKCOLOR} ; \">"; foreach my $colName (keys %{$arrRef}) { $html.="<td>"; $html.=$colName; $html.="</td>"; } $html.="</tr>"; } while($arrRef) { if($obj->{"IsAlternating"} && $alter==0) { $color=$obj->{"RowForeColor"}; $bgcolor=$obj->{"RowBackColor"}; $alter=1; } elsif($obj->{"IsAlternating"} && $alter==1) { $color=$obj->{"AlternatingRowForeColor"}; $bgcolor=$obj->{"AlternatingRowBackColor"}; $alter=0; } elsif(!$obj->{"IsAlternating"}) { $color=$obj->{"RowForeColor"}; $bgcolor=$obj->{"RowBackColor"}; } print $color; print $bgcolor; $html.="<tr style=\" color:$color; background:$bgcolor; \">"; foreach my $col (keys %{$arrRef}) { $html.="<td>"; $html.=$arrRef->{$col}; $html.="</td>"; } $arrRef=$sth->fetchrow_hashref; $html.="</tr>"; } $html.="</table>"; return $html; } else { $html.="<table border=1>"; for(my $i=0;$i<$rows;$i++) { $html.="<tr>"; for(my $j=0;$j<$cols;$j++) { $html.="<td>HELLO</td>"; } $html.="</tr>"; } $html.="</table>"; return $html; } } 1; __END__ #################### Client Code ####################### #In the client code i create a query statement handle , #execute it and pass it off to the DataGrid's DataSource #Method . In the client code im using an HTML Template #.The Render Method of th Data Grid basically spits #all the rendered data as HTML . # ###################################################### my $db="test"; my $host="localhost"; my $userid="test"; my $passwd="test"; my $connectionInfo="dbi:mysql:$db;$host"; # open the html template my $template = HTML::Template->new(filename => 'hello_template.tmpl'); my $cgiObj=CGI->new(); my $grid=DataGrid->new(); my $dbh = DBI->connect($connectionInfo,$userid,$passwd); my $query = "select * from test"; my $sth = $dbh->prepare($query); $sth->execute(); ###########Calling GRID CODE########################## $grid->DataSource($sth); $grid->HeaderColor("red"); $grid->HeaderBackGround("whitesmoke"); $grid->AlternatingRowForeColor("blue"); $grid->AlternatingRowBackColor("whitesmoke"); $grid->RowForeColor("white"); $grid->IsAlternating(1); $grid->RowBackColor("blue"); ##############GRID ################################# #######Add Grid to Template########################## $template->param("Grid_PlaceHolder",$grid->Render()); ##################################################### ########################################################
Let me have your feed back thanks

Edited by Chady -- removed html formatting, added code tags


In reply to A Simple DataBound Grid by BullFrog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.