By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

2D arrays, looping and displaying <TD>'s

P: n/a
This seems like such a stupid little problem, but it's been bugging me
for hours now. I have a 2D array called agents2d. I need to loop
through it and display the info. Here is my code:

echo "<TABLE BORDER=\"1\" ALIGN=\"center\">\n";
echo "<TR><TD COLSPAN=\"4\"
ALIGN=\"center\">$queue_name_line</TD></TR>\n";
echo "<TR><TD COLSPAN=\"4\"> &nbsp; </TD></TR>\n";
echo "<TR><TH>Agent #</TH><TH>Status</TH><TH>Calls Taken</TH><TH>Secs
since last call</TH></TR>\n";

foreach($agents2d as $value){
echo
"<TR><TD>$value[0]</TD><TD>$value[1]</TD><TD>$value[2]</TD><TD>$value[3]</TD></TR>";
}
echo "</TABLE>";
When I display this code, the lines in the loop come out staggered ..
kinda like this:
1345
statusA
45
445
1346
statusB
56
778
etc...

I also tried this:

echo "<TABLE BORDER=\"1\" ALIGN=\"center\">\n";
echo "<TR><TD COLSPAN=\"4\"
ALIGN=\"center\">$queue_name_line</TD></TR>\n";
echo "<TR><TD COLSPAN=\"4\"> &nbsp; </TD></TR>\n";
echo "<TR><TH>Agent #</TH><TH>Status</TH><TH>Calls Taken</TH><TH>Secs
since last call</TH></TR>\n";

foreach($agents2d as $value1){
echo "<TR>";
foreach($value1 as $value2){
echo "<TD>$value2</TD>";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
.... but all the info showed up in the first column only.

1345
statusA
45
455
1346
statusB
35
667

This seems like such a stupid thing. Almost like I'm missing something
in the HTML. But, maybe I just don't quite understand looping over a
2d array.

What am I doing wrong?

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I noticed that Message-ID:
<11*********************@f14g2000cwb.googlegroups. com> from Sturnoff
Megantic contained the following:
foreach($agents2d as $value){
echo
"<TR><TD>$value[0]</TD><TD>$value[1]</TD><TD>$value[2]</TD><TD>$value[3]</TD></TR>";
}
echo "</TABLE>";


Well the html works so the answer is your loop. Or lack of a loop. I'm
guessing a bit because I've never done one of these but I think you'll
have to do a nested loop here, something like:

foreach($agents2d as $value){
//start table row
foreach($value as$val){
//start table data
$val
// end table data
}
//end table row
}

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

P: n/a
Sturnoff Megantic wrote:
foreach($agents2d as $value){
echo
"<TR><TD>$value[0]</TD><TD>$value[1]</TD><TD>$value[2]</TD><TD>$value[3]</TD></TR>";
}
echo "</TABLE>";


How about getting a helping hand from PEAR:

$t = new HTML_Table();
$header = array("Agent",
"Status",
"Calls Taken",
"Secs since last call");
$t->addRow($header,null,"th");
foreach ($agents2d as $row) {
$t->addRow($row);
}
echo $t->toHtml();

And save yourself many sleepless hours when you decide to add one column
or change their order or something else.

/Marcin
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.