0

我有一个返回以下示例结果的查询:

obs_id  person_id   concept_id  encounter_id    obs_datetime    
477205  2           6136        48915           2013-03-21 00:00:00
477206  2           6134        48915           2013-03-21 00:00:00
477204  2           976         48915           2013-03-21 00:00:00
477203  2           6135        48914           2013-03-15 00:00:00
477202  2           6136        48914           2013-03-15 00:00:00
477200  2           976         48913           2013-03-15 00:00:00
477201  2           6134        48913           2013-03-15 00:00:00
477192  2           5497        48912           2013-03-14 00:00:00
477191  2           887         48912           2013-03-14 00:00:00
477190  2           857         48912           2013-03-14 00:00:00

我在 HTML 表格中显示结果。共享遇到 id 的结果放在同一行中。并非行中的每个单元格都包含一个值。每次我有一个新的遭遇 id 时,我都需要创建一个新行。使用 CodeIgniter 的简单方法是什么?

4

2 回答 2

0
$this->db->group_by("encounter_id"); 
$this->db->order_by("encounter_id"); 

$previous_id = -1;
foreach($results as $row)
{
    if ($row->encouter_id != previous_id)
    {
        // create new tr in html-table
        $previous_id = $row->id;
    }
}

(我不太擅长CI和PHP,所以可能会有一些语法错误,但处理方式应该可以。)

于 2013-03-27T19:17:43.267 回答
0

我对 CI 不是很友好,但这里有一些关于普通 PHP 的代码,我相信它们可以很容易地适应 CI。

$someArray = array();
$someArray[] = array("477205", "2", "6136", "48915", "2013-03-21 00:00:00");
$someArray[] = array("477206", "2", "6134", "48915", "2013-03-21 00:00:00");
$someArray[] = array("477204", "2", "976", "48915", "2013-03-21 00:00:00");
$someArray[] = array("477202", "2", "6136", "48918", "2013-03-15 00:00:00");
$someArray[] = array("477203", "2", "6135", "48914", "2013-03-15 00:00:00");
$someArray[] = array("477202", "2", "6136", "48914", "2013-03-15 00:00:00");
$someArray[] = array("477200", "2", "976", "48913", "2013-03-15 00:00:00");
$someArray[] = array("477201", "2", "6134", "48913", "2013-03-15 00:00:00");
$someArray[] = array("477192", "2", "5497", "48912", "2013-03-14 00:00:00");
$someArray[] = array("477191", "2", "887", "48912", "2013-03-14 00:00:00");
$someArray[] = array("477190", "2", "857", "48912", "2013-03-14 00:00:00");


function CalcUnique($arr,$prop,$val) {
    reset ($arr);
    $count = 0;
    foreach ($arr as $sub) if ($sub[$prop]==$val) $count++;
    return $count;
}

echo "<table border=1>\n"; // starting table
$doMerge = $actualVal = false; // setting initial values
foreach ($someArray as $row) { // inerating array
    if ($row[3]!=$actualVal) { // if value changed from previous
        $mergeNum = CalcUnique($someArray,3,$row[3]); // check if there is more then one row with this value
        $actualVal = $row[3]; // store new val
        $doMerge = true; // setting flag to merge cells
    }

    echo "<tr>\n\t<td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td>";

    if ($doMerge && $mergeNum>1) { // prints out merged cell
        echo "<td rowspan=".$mergeNum.">".$row[3]."</td>";
        $doMerge = false;
    }
    if ($mergeNum==1) echo "<td>".$row[3]."</td>"; // prints out regular cell       
    echo "<td>".$row[4]."</td>\n";
    echo "</tr>\n";
}
echo "</table>";
?>
于 2013-03-27T19:24:01.767 回答