Multiple Column Output

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

    Multiple Column Output

    Hi,

    Trying to streamline my code to output a "SELECT ALL" query into
    multiple columns of the same table. Right now it's set to put values
    into certain columns based on the ID (INT, Autonumber).

    However, some of the items got deleted, creating gaps in IDs - messed
    everything up. Better way?

    Thank you in advance.

  • ws Monkey

    #2
    Re: Multiple Column Output

    Use a counter while outputting the records..

    The counter ($i) will take place of the recordset id's


    psuedo

    while(recordset ){
    $i++;
    if($i == $column_depth_c ount){
    //switch columns
    $i = 1;
    }
    }

    Hope that helps,
    -- Steve


    TristaSD wrote:[color=blue]
    > Hi,
    >
    > Trying to streamline my code to output a "SELECT ALL" query into
    > multiple columns of the same table. Right now it's set to put values
    > into certain columns based on the ID (INT, Autonumber).
    >
    > However, some of the items got deleted, creating gaps in IDs - messed
    > everything up. Better way?
    >
    > Thank you in advance.
    >[/color]

    Comment

    • TristaSD

      #3
      Re: Multiple Column Output

      Thank for the inspiration. I used this:

      $num_across = 2; // Two column layout

      echo ("<tr>\n"); // Start the row
      while ($row = mysql_fetch_arr ay($query)) {
      $i++; // Start the counter
      if (($i % $num_across) == 0) { // If the last column is reached
      echo "<td>$row[somename]</td></tr>\n<tr>\n"; // Close/open row
      }
      else {
      echo "<td>$row[id]</td><td>$row[artist]</td>"; // Otherwise just
      show some cells
      }
      }
      ?>

      </table>

      Thanks.

      Comment

      • ws Monkey

        #4
        Re: Multiple Column Output

        TristaSD wrote:[color=blue]
        > Thank for the inspiration. I used this:
        >
        > $num_across = 2; // Two column layout
        >
        > echo ("<tr>\n"); // Start the row
        > while ($row = mysql_fetch_arr ay($query)) {
        > $i++; // Start the counter
        > if (($i % $num_across) == 0) { // If the last column is reached
        > echo "<td>$row[somename]</td></tr>\n<tr>\n"; // Close/open row
        > }
        > else {
        > echo "<td>$row[id]</td><td>$row[artist]</td>"; // Otherwise just
        > show some cells
        > }
        > }
        > ?>
        >
        > </table>
        >
        > Thanks.
        >[/color]
        Looks good!
        You may want to account for odd recordsets..

        i.e.
        3 records in the db

        | td data | td data |
        | td data | ........

        The code would have to be set a variable that could be read outside the
        while, basically seeing if the last $i iteration was odd, then stuff an
        empty td line at the end.

        Some more code would need to be added if you wanted the $num_rows
        variable to be truly dynamic. The last iteration would have to determine
        how many empty cells there were based on the $num_rows variable. (i.e.
        4 records on a 3 column would result in 2 empty cells)

        Will keep your html pretty and not messed up w/ browsers interpreting
        strict html.

        Enjoy
        -- Steve

        Comment

        Working...