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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sturnoff Megantic

    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_l ine</TD></TR>\n";
    echo "<TR><TD COLSPAN=\"4\"> &nbsp; </TD></TR>\n";
    echo "<TR><TH>Ag ent #</TH><TH>Status</TH><TH>Calls Taken</TH><TH>Secs
    since last call</TH></TR>\n";

    foreach($agents 2d as $value){
    echo
    "<TR><TD>$v alue[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_l ine</TD></TR>\n";
    echo "<TR><TD COLSPAN=\"4\"> &nbsp; </TD></TR>\n";
    echo "<TR><TH>Ag ent #</TH><TH>Status</TH><TH>Calls Taken</TH><TH>Secs
    since last call</TH></TR>\n";

    foreach($agents 2d as $value1){
    echo "<TR>";
    foreach($value1 as $value2){
    echo "<TD>$value 2</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?

  • Geoff Berrow

    #2
    Re: 2D arrays, looping and displaying &lt;TD&gt;'s

    I noticed that Message-ID:
    <1110505403.464 975.18080@f14g2 000cwb.googlegr oups.com> from Sturnoff
    Megantic contained the following:
    [color=blue]
    >foreach($agent s2d as $value){
    >echo
    >"<TR><TD>$valu e[0]</TD><TD>$value[1]</TD><TD>$value[2]</TD><TD>$value[3]</TD></TR>";
    >}
    >echo "</TABLE>";[/color]

    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($agents 2d 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/

    Comment

    • Marcin Dobrucki

      #3
      Re: 2D arrays, looping and displaying &lt;TD&gt;'s

      Sturnoff Megantic wrote:
      [color=blue]
      > foreach($agents 2d as $value){
      > echo
      > "<TR><TD>$v alue[0]</TD><TD>$value[1]</TD><TD>$value[2]</TD><TD>$value[3]</TD></TR>";
      > }
      > echo "</TABLE>";[/color]

      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

      Comment

      Working...