putting database results in a 5x1 table

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

    putting database results in a 5x1 table

    I have a db of tens of thousands of entries. It's not too hard to pull
    all the entries and build a table that displays them one at a time, but
    in my case, that will be a huge waste of space. What I want to do is
    put one record in each of the columns. I thinks it's possible to do
    because I have seen very elaborate thumbnail gallery scripts to do this.
    I just want to be able to put two or three strings of text from each
    record into each row, and a checkbox. Here's sample of what I mean:

    <table width="76%" border="1" cellspacing="1" cellpadding="1" >
    <tr>
    <td>$record1Lin e1<br>$record1$ Line2<br>
    <input type="checkbox" name="$record1c heckbox" value="checkbox "></td>
    <td>$record2Lin e1<br>$record2$ Line2<br>
    <input type="checkbox" name="$record2c heckbox" value="checkbox "></td>
    <td>$record3Lin e1<br>$record3$ Line2<br>
    <input type="checkbox" name="$record3c heckbox" value="checkbox "></td>
    <td>$record4Lin e1<br>$record4$ Line2<br>
    <input type="checkbox" name="$record3c heckbox" value="checkbox "></td>
    <td>$record5Lin e1<br>$record5$ Line2<br>
    <input type="checkbox" name="$record5c heckbox" value="checkbox "></td>
    </tr>
    </table>

    I want to pull about 200 records at a time from the db and display as
    described above. How can I tell php to take five records from my result
    set and display above, then loop through the result set until I'm done?

    Thanks much for any guidance you can offer.

    Ed
  • Justin Koivisto

    #2
    Re: putting database results in a 5x1 table

    Eddie Biscomb wrote:
    [color=blue]
    > I have a db of tens of thousands of entries. It's not too hard to pull
    > all the entries and build a table that displays them one at a time, but
    > in my case, that will be a huge waste of space. What I want to do is
    > put one record in each of the columns. I thinks it's possible to do
    > because I have seen very elaborate thumbnail gallery scripts to do this.
    > I just want to be able to put two or three strings of text from each
    > record into each row, and a checkbox. Here's sample of what I mean:
    >
    > <table width="76%" border="1" cellspacing="1" cellpadding="1" >
    > <tr>
    > <td>$record1Lin e1<br>$record1$ Line2<br>
    > <input type="checkbox" name="$record1c heckbox" value="checkbox "></td>
    > <td>$record2Lin e1<br>$record2$ Line2<br>
    > <input type="checkbox" name="$record2c heckbox" value="checkbox "></td>
    > <td>$record3Lin e1<br>$record3$ Line2<br>
    > <input type="checkbox" name="$record3c heckbox" value="checkbox "></td>
    > <td>$record4Lin e1<br>$record4$ Line2<br>
    > <input type="checkbox" name="$record3c heckbox" value="checkbox "></td>
    > <td>$record5Lin e1<br>$record5$ Line2<br>
    > <input type="checkbox" name="$record5c heckbox" value="checkbox "></td>
    > </tr>
    > </table>
    >
    > I want to pull about 200 records at a time from the db and display as
    > described above. How can I tell php to take five records from my result
    > set and display above, then loop through the result set until I'm done?
    >
    > Thanks much for any guidance you can offer.
    >
    > Ed[/color]

    Set $numRows to how many records you have retrieved total...

    for($i=0;$i<$nu mRows;$i=$i+5){
    echo '<tr>';
    for($j=$i;$j<($ i+5);$j++){
    $line1='record' .$j.'Line1';
    $line2='record' .$j.'Line2';
    $checkbox='reco rd'.$j.'checkbo x';
    echo '<td>',${$line1 },'<br>',${$lin e2},'<br>',
    ${$checkbox},'</td>';
    }
    echo '</tr>',"\n";
    }

    Try it - maybe I'm just too tired, but it looks ok from here (not tested
    of course)... That is assuming that you actually have the variable names
    set up as you had indicated in that block of HTML...

    I would be more inclined to have the SQL return 3 columns per row so I
    could just use something like: $row[0], $row[1], $row[2] for the
    variables...

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • chrislive

      #3
      Re: putting database results in a 5x1 table

      another idea even though its a bit messy would be...

      if(($count_td == "1")OR($count_t d == "")){ echo "<tr>"; }

      echo "<td>$conte nt</td>";

      if($count_td == "5"){ echo "</tr>"; $count_td = "1"; }else{
      $count_td++; }

      Comment

      • Marcin Dobrucki

        #4
        Re: putting database results in a 5x1 table

        Eddie Biscomb wrote:[color=blue]
        > I have a db of tens of thousands of entries. It's not too hard to pull[/color]
        ....[color=blue]
        > I want to pull about 200 records at a time from the db and display as
        > described above. How can I tell php to take five records from my result
        > set and display above, then loop through the result set until I'm done?[/color]

        This will stretch the last column to fill the missing space.

        <?php
        require_once ("HTML/Table.php");
        $sample_data = array ("john", "fred", "bob", "willy", "angie",
        "frank", "betty", "george", "wilma", "dexter",
        "jack", "ziggy", "flo", "shrek", "gork", "nobody");

        function build(&$input, $cols = 5) {
        $t = new HTML_Table(arra y("border" => 1));
        $t->setAutoGrow(tr ue);

        $array_size = count($input);
        $line = 0;

        for ($offset = 0; $offset < $array_size; $offset = $offset + $cols) {
        $row = array_splice($i nput, 0, $cols);
        $row_size = count($row);
        $t->addRow($row) ;
        if ($row_size < 5) {
        $t->setCellAttribu tes($line, $row_size - 1,
        array("colspan" => 5 - $row_size +1));
        }
        $line++;
        }
        return $t;
        }

        $res =& build($sample_d ata);
        $res->display();
        ?>

        You can also remove the lines that handle the "stretch", and just add
        the result of the splice. The non-existant cells will be handled by
        setAutoGrow then.

        /m

        Comment

        Working...