Multiple records in rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roamer
    New Member
    • Dec 2012
    • 23

    Multiple records in rows

    Multiple records in rows
    Hi I have a partial solution working to show so many records in a row.

    How to make it work so only, say, 2 rows show - I don't know how to do.

    file.txt
    Code:
    01|Aaaa|
    02|Bbbb|
    03|Cccc|
    04|Dddd|
    05|Eeee|
    06|Ffff|
    07|Gggg|
    08|Hhhh|
    09|Iiii|
    10|Jjjj|
    11|Kkkk|
    12|Llll|
    Perl script
    Code:
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";
    my $file="file.txt";
    my $num="3";
    my $rw="2"; ## this will be for number of rows shown
    my $pt="1";
    
    # Don't like the foreach, but couldn't get while loop to do this.
    open(INF,"$file") or die("Can't open $file: $!"); my @md = <INF>; close(INF);
    print qq ~<table border="1"><tr>~;
    
     foreach my $line (@md) {chomp($line);
     my @fd=split /\|/, "$line";
     if ($pt == $num) {
     print "<td>$fd[0]</td><td>$fd[1]</td></TR><TR>\n"; $pt=1;
     }
     else
     {
     if ($fd[0] ne "") {print "<td>$fd[0]</td><td>$fd[1]</td>\n"; $pt++;
     }
     }
     }
    
    print qq ~</tr></table>~;
    exit(0);
    I also don't want to use a module for this (can't understand most of them anyways).

    Please can someone help.

    Thanks beforehand
  • Roamer
    New Member
    • Dec 2012
    • 23

    #2
    Anybody out there got an answer?

    Comment

    • RonB
      Recognized Expert Contributor
      • Jun 2009
      • 589

      #3
      Code:
      use strict;
      use warnings;
      use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
      
      my $file = "file.txt";
      my $row  = 2;          ## this will be for number of rows shown
      
      open my $fh, '<', $file or die "Can't open $file: $!";
      
      print "Content-type: text/html\n\n";
      warningToBrowser(1);
      
      print qq ~<table border="1">\n~;
      
      while (my $line = <$fh>) {
          chomp $line;
          my @fd = split /\|/, $line;
          print "  <tr><td>$fd[0]</td><td>$fd[1]</td></tr>\n";
          last if $. == $row;
      }
      close $fh;
      
      print qq ~</table>\n~;
      exit(0);

      Comment

      • Roamer
        New Member
        • Dec 2012
        • 23

        #4
        Hi Ron, thanks for that, but it's kind of going backwards for me. I started with something like a month ago and got to the point where I posted the thread.

        I'm more an artist so programming is really hard for me. Let's go visual.

        It's based on the ID (once I have that working the rest can be done by myself). So here's the concept:

        01 02 03 (first row - 3 different records)
        04 05 06 (second row - another 3 different records)

        OR (could be several in a row)

        01 02 03 04 .....
        05 06 07 08 .....

        etc-etc.
        I probably would have no more than 4 rows of records (it's a blog script I wrote and this is for the homepage).

        Hope I'm not being too much trouble, this thing's doing my head in.....

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          I'm not sure what you're wanting/asking. Please rephrase your question and give an example of the desired output.

          Comment

          • Roamer
            New Member
            • Dec 2012
            • 23

            #6
            Hi again Ron, I'll try to explain.

            The initial script shows the entire file. 4 rows with 3 records each. If I want to only show 2 of those rows, then somehow those rows have to be broken up in such a way that I can define how many rows I want to show.

            I'm not even sure it can be done, but if so then good. If not, then I'll just have to leave it at only one row and be done with it.

            The database is tiny, just 4 fields per record, so even a foreach could handle a couple thousand records.

            I was thinking, open the file, in a while loop extract say 6 records and put them into an array. Then work from the array, using a foreach, rather than the whole file. I have looked all over the place and even tried PerlDoc (what a confusing site) to no avail.

            I've never tried anything like this before because, normally, each blog entry is on it's own line. Putting say 3 entries on a line was different. I have seen the multi-line thing done, but I think it was in some other programming language (not sure which).

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              You need pass parameters to the script that specify the number of rows to show and maybe even the starting point. For example, if you have 20 rows in the file and you want to show rows 5 starting with row 4, then you'd pass those numbers to the script via the query string (url).

              The script would use a while loop to read the file and skip over the first few lines and process the next 5 lines and exit the loop after the 5th line it processed.

              No need to load the file into an array.

              Comment

              • Roamer
                New Member
                • Dec 2012
                • 23

                #8
                Got it sorted - thanks

                Comment

                Working...