在while循环中更改id后添加新元素

I have while loop to get information from mysql table. But can't figure how to find las entry containing same factor_id and add append new element after it.

    if ($this->_db->numRows() > 0) {
        $i = 1;
        $j = '';
        $row = '';
        while ($row = mysqli_fetch_assoc($res_info)) {

            if ($row['task_factor_category'] != $j) {
                $title = '<tr><td colspan="4" class="title" style="text-align: left;">' . $row['task_factor_category'] . '</td></tr>';
                $comment = '<tr><td class="title" style="text-align: left" colspan="4">' . $this->_glan['comments'] . '</td></tr><tr><td colspan="4"><textarea cols="82" rows="3" name="factor_comment[' . $row['factor_id'] . ']" id="' . $row['factor_id'] . '"></textarea></td></tr>';
            } else {
                $title = false;
                $comment = false;
            }
            $this->reg['edit_fields']['factors'] .= $title . '<tr>
        <td>
        ' . $row['factors'] . '
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="1"  />
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="2" checked />
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="3" />
        </td>
        </tr>
        ';
            $j = $row['task_factor_category'];
            $i++;
        }
    }

output is like:

         //results with $row['factor_id'] = 10;
title
    1. entry
    2. entry
    3. entry
    4. entry
    5. entry
  //need new element here

  //results with $row['factor_id'] = 11;
 title
    1. entry
    2. entry
    3. entry
    4. entry
    5. entry
   //need new element here

    ......

I need to add additional element after the last entry containing same factor id

You may want to try this one.

$id = array();
$i = 0;

while ($row = mysqli_fetch_assoc($res_info))
{
    $id[$i] = $row['id'];

    if ($i != 0 && $id[$i-1] != $row['id']) {
        //new element
    } else {
        //same element
    }
    $i++
}

you can count total rows suppose there are 7 rows

Add a if code in while loop like

while(cond)
{
    if($i == 7 )
    {
      then echo("what ever you want");
    }
}