0

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.

1
  • use a row counter and modulo so you have something like $html_output .= $iRowCounter % 2 == 0 ? "</tr><tr>" : ""; ... as a very rough approximation Commented Dec 3, 2014 at 16:59

2 Answers 2

1

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>';
Sign up to request clarification or add additional context in comments.

4 Comments

Hey Paul, is there something wrong with your query? syntax seems off.
I just noticed something else. One part of my requirement was that it also shows two items per row. The above code outputs one item per row. Thank you again. I like the one query idea! Wondering if you can help me with the second requirement too?
Sure, I misread your question. I edited my answer to reflect that requirement.
This renders perfectly. Thank you for the explanation as well! Appreciate it very much.
0

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
        }
    }
}

2 Comments

Hi faerysteel, the code above misses one part of my requirement where I have to output 2 items in <td> per row. Check output html example above. Thank you again for the help!
Please clarify: For each two results, is one row. Then for each two rows is one table. Correct? Why are you splitting up rows into multiple tables? Why are you putting multiple data pieces ($ID and $ITEM_NAME) into each cell, and why two of those per row? This seems to be an abuse of tables. What exactly are you trying to accomplish? There's probably a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.