Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

dynamic website, putting data into right tablefield's

by ultibuzz (Monk)
on Apr 16, 2007 at 12:14 UTC ( #610329=perlquestion: print w/replies, xml ) Need Help??
ultibuzz has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
i've a problem putting the right data into the right place of a table,i think the problem is more in my head then in html.
The Table shoud look like this

  sup_1 sup_2
Product flag data_s data_f data_s data_f
P_Name U 10 36 2 106

The Table is build dynamicly from 3 arrays
the @sup array contains all sup's
the @product array contains all P_Names
the @data array contains the hashref for each row, can have blanks,for test purpose i have a normal array

___sup___ 1 2 3 __product__ Now Promo Special Indirect DATA fields PRODUCT,FLAG,SUP,DATA_S,DATA_F __data__ Promo;X;2;10;5 Promo;X;1;22;106 Direct;U;1;; Special;U;1;2;

The table shoud be build out of @product and @sup, and the informations from @data shoud be inserted if it matches,blanks also
my attemps so far are a forech loop for sup, then a for loop for product checking if sup is true or not and insert data.
this ends up in duplicated P_Names and lots of free tabs in the table,so its totals crap ^^
at the moment ims tuc and don't get an idear how to make it, at the end all tabs will be <inputs> to manipulate the data
the table format can not be changed

kd ultibuzz

Replies are listed 'Best First'.
Re: dynamic website, putting data into right tablefield's
by kutsu (Priest) on Apr 16, 2007 at 16:48 UTC

    If your having trouble with the hashrefs or arrays, I'd take a look at perldsc for some good examples. As for the html I'd use a template (either HTML::Template or Template Toolkit. I'll leave an example of how to do it in TT, though I might be misreading what your arrays contain.

    my $vars = { 'sup' => [ @sup ], 'products' => [ @products ], 'data' => { %data }, #data has the product names as keys ref. an array of data }; $tt->process('sometemplatefile', $vars); <table> [% foreach s = sup %] <tr><td>[% s %]</td></tr> [% end %] [% foreach p = products %] <tr><td>[% p %]</td> [% foreach cur = data.$p %] <td>[% cur %]</td> [% end %] </tr> [% end %] </table>

      thx for the nice example, but i got one question,
      do you have checked if you insert the data in the right spot ?
      i mean if this product and data belongs to sup 1 or 2 or whatever sup or to all the sups.
      i hope i can clearify what i mean with this example table build of the example __DATA__

        1 2
      Product flag data_s data_f data_s data_f
      Promo X 22 106 10 5
      Direct U 1      
      Special U 1 2    

      handling the data is not really a problem, but the right combination of it so it will be in the right place.
      i am thinking to build a row array that has 2 +(sups*2) fields,so the data is in the right place but i stuck at the moment in merging the data together so i dont have 2 rows with Promo for example.
      i don't use html template because i build my own template set in a package, for headers footers dropdowns forms and so on,but this template example looks good and im thinking of it to use it ;)

      kd ultibuzz

      UPDATE:i'm thinking to build a hash based data model to put in the data, somethink like:

      rowhash { Product => { SUP => { DATA_S,DATA_F } SUP => { DATA_S,DATA_F } } Product => { SUP => { DATA_S,DATA_F } SUP => { DATA_S,DATA_F } } }
      then i have all data in the right place.
      i woud start adding all products to this hash stucture then all existing sup's, after that fill in data for the product->sup or leave data blank if no data is there.
      so wahts your opinion
      kd ultibuzz

Re: dynamic website, putting data into right tablefield's
by agianni (Hermit) on Apr 16, 2007 at 12:45 UTC
    Try reversing the nested foreach loops. Go through the products first (rows) and then for each product go through the sups (columms).
    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
Re: dynamic website, putting data into right tablefield's
by ultibuzz (Monk) on Apr 17, 2007 at 12:35 UTC

    cheers all,
    this is my working solution :D,after clearify from bard how to write the hash thingy right ^^

    foreach my $prod (@total_pid) { foreach my $su (@total_sup) { $rowhash{$prod}{$su}{'DATA_S'} = ""; $rowhash{$prod}{$su}{'DATA_F'} = ""; } } foreach my $row (@ym_cty_data) { $rowhash{$row->{PRODUCT_ID}}{$row->{SUPPLIER_ID}}{'DATA_F'} = +$row->{VALUE_FE}; $rowhash{$row->{PRODUCT_ID}}{$row->{SUPPLIER_ID}}{'DATA_S'} = +$row->{VALUE_SOLID}; }

    first i build the hash with all options, then i fill the options up with data.
    with this hash structure you can be sure the data is in the right spot
    thx for the help and inspiration
    kd ultibuzz

      Your first step strikes me as unnecessary. I'm guessing that you're trying to avoid accidentally putting data in the wrong cells because some cells data is not there. But as long as you go through all of the products and sups when you output the data, you shouldn't have a problem.

      The benefit of your solution is that you could do something like:

      for my $prod ( keys %rowhash ){ for my $sup ( keys %{$rowhash{$prod}} ){ print "$rowhash{$prod}{$sup}{DATA_F}, $rowhash->{$prod}{$sup}{DA +TA_S}\t"; } print "\n"; }

      ...because all of the options are encapsulated in each row, but that is sort of treating a hash like an array. If you skip your first step of populating the hash with empty values, you should still be able to do something like:

      for my $prod ( @total_pid ){ for my $sup ( @total_sup ){ print "$rowhash{$prod}{$sup}{DATA_F}, $rowhash{prod}{$sup}{DATA_ +S}\t"; } print "\n"; }

      Because you always know what your complete list of products and sups are.

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

        well the script know of all active products at the now state and all historical active products, that may be inactive or still active.
        anyway i need to treat both of them, if someone wants to edit old data it shoud add the now active products to the historical ones.
        with prebuilding the array with total_pid i get sure all,historical and now, products are in there ;), gives me a better feeling ^^.
        when i only run ym_cty_product i may miss a product.
        when i update/ insert into the database without the first run (empty values i miss some data in db, can be my update/insert sub but with first run everything looks fine in db
        please correct me if i don't get somthing wrong because im quit new into these HoH and so on ^^

        kd ultibuzz

        PS: the data can not be entered in the wrong cell because each cell contains a sup and product, so every value is unique accesible.
        i'will post my table creation tomorrow from office, i think this makes it clearer then my PS ^^

        Update: here is my table code looks a bit messy but it works ^^

        foreach my $row (@total_sup) { my $sup = Get_Supplier_Name($row); print qq(<td align="center" bgcolor="#7E7E7E" colspan="2"><b>$ +sup</b></td>); } print qq(</tr><tr><td align="center" bgcolor="#7E7E7E"><b>Product Name +</b></td>); print qq(<td align="center" bgcolor="#7E7E7E"><b>Flag</b></td>); foreach my $row (@total_sup) { print qq(<td align="center" bgcolor="#7E7E7E"><b>Front End</b> +</td>); print qq(<td align="center" bgcolor="#7E7E7E"><b>Solid Sales</ +b></td>); } print qq(</tr>); foreach my $row (@total_pid) { my $sups = scalar(@total_sup); my $prod_name = Get_Product_Name($row,$group); print qq(<tr><td align="center" bgcolor="#FFFFFF"><input type= +"hidden" name="prod_id" value="$row">$prod_name</td>); print qq(<td align="center" bgcolor="#FFFFFF"><input type="hid +den" name="flag" value="$rowhash{$row}{'FLAG'}">$rowhash{$row}{'FLAG' +}</td>); for (my $i=1;$i<=$sups;$i++) { print qq(<td align="center" bgcolor="#FFFFFF"><input t +ype="text" name="sup_f_$now_sup[$i-1]" value="$rowhash{$row}{$now_sup +[$i-1]}{'DATA_F'}" size="10"></td>); print qq(<td align="center" bgcolor="#FFFFFF"><input t +ype="text" name="sup_s_$now_sup[$i-1]" value="$rowhash{$row}{$now_sup +[$i-1]}{'DATA_S'}" size="10"></td>); } print qq(</tr>); } my $col_span = (scalar(@now_sup)*2)+2; print qq(<tr><td align="center" bgcolor="#7E7E7E" colspan="$col_span"> +<input type="submit" value=" SAVE "></td></tr>);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://610329]
Approved by marto
Front-paged by monkfan
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2018-06-25 16:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.