How do I return 3 results and print </tr>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron Reimann

    How do I return 3 results and print </tr>

    I am trying to build a table using results from a database query.
    Right now (code below), the code displays each result in a new <tr>. I
    am wanted to display 3 results in one <tr> </tr>, and then create a new
    <tr> with another 3, etc.

    Here is human code:
    print a <tr>
    if only 1, 2 or 3 results have been returned, show the results and
    print a </tr>
    if 4, 5, and or 6 results have been returned, print <tr> and show
    results
    if no more results have been returned, print </tr>

    Does that make sense? Can anyone help?

    Thank you


    Here is my PG query (function):
    ###############
    function section_get_sec tions() {
    $dbconn = sys_db_connect( );
    if ($dbconn == FALSE) {
    return NULL;
    }

    $dbquery = <<<EOB
    SELECT section.id, section.name
    FROM section
    ORDER BY section.name

    EOB;

    $dbresult = sys_db_query ($dbconn, $dbquery);
    if ($dbresult == FALSE) {
    return NULL;
    } else {
    return $dbresult;
    }
    }
    ###############


    Here is my PHP current code displaying the results:
    ###############
    EOB;
    $r = section_get_sec tions();
    for ($i = 0; $i < pg_numrows($r); $i++) {
    $t = pg_fetch_assoc( $r, $i);
    print <<<EOB
    <tr>
    <td class="fe_label " nowrap>
    {$t['name']}
    </td>
    <td class="fe_widge t">
    <input name="sections[]" type="checkbox" value="{$t['id']}"
    EOB;
    if (in_array($t['id'], $fd['sections'])) {
    echo(" checked");
    }
    print <<<EOB[color=blue]
    >[/color]
    </td>
    </tr>
    ###############

  • Alvaro G Vicario

    #2
    Re: How do I return 3 results and print &lt;/tr&gt;

    *** Aaron Reimann wrote/escribió (31 May 2005 07:55:35 -0700):[color=blue]
    > I am trying to build a table using results from a database query.
    > Right now (code below), the code displays each result in a new <tr>. I
    > am wanted to display 3 results in one <tr> </tr>, and then create a new
    > <tr> with another 3, etc.[/color]

    $position=0;

    while( ... ){
    if($position==0 ){
    echo '<tr>';
    }

    echo '<td>' . $data . '</td>';

    if($position==2 ){
    echo '</tr>';
    }

    $position=($pos ition+1) % 3;
    }


    We leave completing last line as an exercise for the reader ;-)

    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Tony

      #3
      Re: How do I return 3 results and print &lt;/tr&gt;

      "Aaron Reimann" <aaron.reimann@ gmail.com> wrote in message
      news:1117551335 .836152.149610@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      >I am trying to build a table using results from a database query.
      > Right now (code below), the code displays each result in a new <tr>. I
      > am wanted to display 3 results in one <tr> </tr>, and then create a new
      > <tr> with another 3, etc.
      >
      > Here is human code:
      > print a <tr>
      > if only 1, 2 or 3 results have been returned, show the results and
      > print a </tr>
      > if 4, 5, and or 6 results have been returned, print <tr> and show
      > results
      > if no more results have been returned, print </tr>
      >
      > Does that make sense? Can anyone help?[/color]


      Try something like this:

      echo "<tr>";
      for ($i = 0; $i < pg_numrows($r); $i++) {
      echo "<td>";
      // output your info here
      echo "</td>";
      if ( ( $i + 1 ) % 3 == 0 ) { echo "</tr><tr>"; }
      }
      echo "</tr>";




      Comment

      • Aaron Reimann

        #4
        Re: How do I return 3 results and print &lt;/tr&gt;

        [color=blue]
        > Try something like this:
        >
        > echo "<tr>";
        > for ($i = 0; $i < pg_numrows($r); $i++) {
        > echo "<td>";
        > // output your info here
        > echo "</td>";
        > if ( ( $i + 1 ) % 3 == 0 ) { echo "</tr><tr>"; }
        > }
        > echo "</tr>";[/color]

        Thank you. That works for me.

        Sorry about the late reply.

        Comment

        • Tony

          #5
          Re: How do I return 3 results and print &lt;/tr&gt;


          "Aaron Reimann" <aaron.reimann@ gmail.com> wrote in message
          news:1117657191 .253706.63050@g 14g2000cwa.goog legroups.com...[color=blue]
          >[color=green]
          >> Try something like this:
          >>
          >> echo "<tr>";
          >> for ($i = 0; $i < pg_numrows($r); $i++) {
          >> echo "<td>";
          >> // output your info here
          >> echo "</td>";
          >> if ( ( $i + 1 ) % 3 == 0 ) { echo "</tr><tr>"; }
          >> }
          >> echo "</tr>";[/color]
          >
          > Thank you. That works for me.
          >
          > Sorry about the late reply.[/color]

          Glad I could help :)


          Comment

          Working...