Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cgi and tables again

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
GB
Hi,

related to a previous post - I have a bit of a cgi program that searches through an an array of arrays to return certain arrays depending on user input, these arrays then need to be displayed in a table in the browser. the code at the moment is as follows:

Code:
# Create an empty array
my @matching_arrays;

# for each result in @array 
foreach my $result (@array) { 
    # Append to @matching_arrays all elements of AoA matching $result
    push @matching_arrays, grep { ${$_}[0] =~ /$result/ } @AoA;
	
}

foreach (@matching_arrays) {    	
    print $query -> p("\"" . join("\", \"", @$_) . "\" ");
}

basically @array contains some elements that match the first element in the arrays in the AoA, when a match is made the array is stored in @matching_arrays. the output at the moment looks like this (e.g. if @array = ("val1", "val2", "val3"):

Code:
"val1", "sometext", "sometext", "id1"
"val2", "sometext", "sometext", "id2"
"val4", "sometext", "sometext", "id3"

the problem i'm having is displaying the results in a table. i am using cgi.pm the object-oriented way, so the above result should be displayed as a table with 4 columns and 3 rows, and one "element" in each cell.... any hits would be great as i'm pretty stuck :)
 
You have to create the HTML code in your script. Here's a snippet of code that cycles through an array and displays the data in a table:

Code:
<table width="100%" border="1">

while (@row = $sth->fetchrow_array){
  print <<EOT;
  <tr>
  <td>$row[1]</td>
  <td>$row[2]</td>
  <td>$row[3]</td>
  <td>$row[4]</td>
  <td>$row[5]</td>
  <td>$row[6]</td>
  </tr>
EOT

}
</table>

There's always a better way. The fun is trying to find it!
 
I saw this question on another forum the other day and have answered it there. For the benefit of others reading this thread, here's the example code I posted there:
Code:
my $q = new CGI;

my @AoA = (
    ["val1", "sometext", "sometext", "id1"],
    ["val2", "sometext", "sometext", "id2"],
    ["val3", "hey", "thanks", "mark"],
    ["val4", "sometext", "sometext", "id2"],
    ["val5", "sometext", "sometext", "id3"]
);

print $q->start_table;
print $q->Tr( [ map { $q->td( $_ )  } @AoA ] );
print $q->end_table;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top