BaconJuice
BaconJuice

Reputation: 3769

Dynamic HTML table creation with PHP

I'm trying to create a dynamic html table while querying the DB using PHP. I'm having a bit of difficulty understanding how to append two different loops to it to ensure the table I want to create is generated correctly.

<table><tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>
<table><tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>

As you can see I have to append two rows worth of information in to one HTML table row and on the 3rd row I need to append a


and create a new HTML table while continuing my query.

Currently my PHP code looks something like this.

    foreach($item_ID as $x => $x_value) {

        $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
        $query = $this -> db -> query($sql);

        foreach($query->result() as $row){
            $item_name = $row->item_name;
        }

        $html_output .= '<tr><td>';
        $html_output .= $x_value. " ".$item_name;
        $html_output .= '</td>';

        //Not sure how to proceed with closing the rows and opening new one on 3rd query item?

    }

Please note that I am using Codeigniter, however I am just looking for some help in logic.

Let me know if I can help clear things up.

Thank you for taking your time to read this. Any help would be greatly appreciated.

Upvotes: 0

Views: 1829

Answers (2)

Paul
Paul

Reputation: 9022

First, do just one query:

$sql = "SELECT id, item_name FROM item_table WHERE id IN ('" . implode("', '", $item_ID) ."')";
$query = $this -> db -> query($sql);

Set a counter

$i = 1;

Then start your table code

$html_output .= '<table>';

Now loop through the results, create the rows and look for that counter

foreach($query->result() as $row){
    if ($i % 2 == 1) {
        $html_output .= '<tr>';
    }
    $html_output .= '<td>' . $row->id .' and' . $row->item_name . '</td>';
    if ($i % 2 == 0) {
        $html_output .= '</tr>';
    }
    if ($i % 6 == 0) {
        $html_output .= '</table><hr><table>';
    }
    $i++;
}
  • The $i % 2 == 1 part starts a new table row before each odd item (1, 3, 5, ...).

  • The $i % 2 == 0 part ends the table row after each even item (2, 4, 6, ...).

  • The $i % 6 == 0 part adds a line after each third row (actually after each sixth item and since there are two items per row it's after three rows).

Finally close the table but first check if we have an even number of items. If not, add an empty table cell.

if ($i % 2 == 1) {
    $html_output .= '<td></td></tr>';
}
$html_output .= '</table>';

Upvotes: 1

faerysteel
faerysteel

Reputation: 143

To paraphrase, it sounds like you want to create a new table with each odd-numbered row. In addition, you want to separate the tables with the HR tag.

$i = 0; //initialize as 0 rows
foreach($item_ID as $x => $x_value) {

    $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
    $query = $this -> db -> query($sql);

    foreach($query->result() as $row){
        $item_name = $row->item_name;

        $row_output  = '<tr><td>';
        $row_output .= $x_value. " ".$item_name;
        $row_output .= '</td>';
       //presumably you would close the row here too, unless you have more table cells you aren't showing us

        $i++;//we have a row, so increment
        if ( !($i % 2) ) { // if this has a remainder, then the current row counter is odd
          if ( $i != 1 ) {
            //beginning of a table that isn't the first table, add the hr
            $html_output .= '<hr>'; 
          }
          $html_output .= '<table>'; // open table
          $html_output .= $row_output;
        } else { //this is an even numbered row, the last row of the table
          $html_output .= $row_output;
          $html_output .= '</table>'; // close table
        }
    }
}

Upvotes: 0

Related Questions