473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
2 3857
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt Adams | last post by:
I want to move the following <PRE> defintion to a css file: <TABLE><TR><TD><PRE>sample text</PRE></TD> <TD> not predefined font</TD></TR></TABLE> should be <TABLE><TR><TD class=aaa>sample...
2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
3
by: RC | last post by:
I have a very sime html table like <html><head><title>My Table</title> <style> input { margin: 0; padding: 0; border-width: 0; text-indet: 0; text-align: left } </style></head><body> <table...
3
by: Henry Johnson | last post by:
Okay - I'm spinning my wheels on this one... can someone help me figure out how to programmatically populate a table cell as follows (from C# code-behind)? I've tried using a Literal control in the...
5
by: mahesr | last post by:
I want to match some particular text between <tr>and </tr> or <td>and </td>.... in PHP. like below............ <table><tr> CATEGORY: <td><font face="Verdana" size="1" color="#A000A0"> Wedding...
4
by: John | last post by:
I am attempting to make a table data row editable once clicked on but I am not sure how to show the <tdwith two different states. I've got it to the point where the <tdis hidden then appears within...
4
George Lft
by: George Lft | last post by:
I'm new at building table. Mostly PHP programming . Now I can't seem to fix the size of my table row and column. Any idea? <h4>Two rows and three columns:</h4> <table border="1"> <tr> ...
7
by: Xiaoyan | last post by:
Hi,everyone: I have a problem now. I can't get the information between the <tr><td> and </td></tr>. for example: I use this regular expression can't get it, I don't know why....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.