我有一个包含21列的数据库表,我想显示所有这些列,所以我决定尝试垂直放置表。
第一列是'Headers',后面是每列显示数据库中的一行数据。
|--------|------|------|------|
| Title1 | row1 | row2 | row3 |
|--------|------|------|------|
| Title2 | row1 | row2 | row3 |
|--------|------|------|------|
等
在视图中
echo '<table>';
echo '<h2>Schemes</h2>';
foreach ($scheme->result() as $schemerow)
{
echo '<tr><td class="app"><strong>Award 1</td><td class="app">'.$schemerow->Description.'</td></tr>';
echo '<tr><td class="app"><strong>Award 2</td><td class="app">'.$schemerow->Description.'</td></tr>';
echo '<tr><td class="app"><strong>Location</td><td class="app">'.$schemerow->LocationName.'</td></tr>';
echo '<tr><td class="app"><strong>Day</td><td class="app">'.$schemerow->DayName.'</td></tr>';
等
这给了我一张长桌,每行都在前一行之下。
但是我无法弄清楚如何将第二行放到第一行的右边等等?
答案 0 :(得分:1)
我与 codeigniter 并不相似,也不知道使用result()
功能是否可以实现以下功能。但是将结果提取到具有result_array()
的数组中,您应该能够通过以下方式转发该数组:
$transposedArray = array();
foreach ($scheme->result_array() as $schemerow)
{
foreach ($schemerow as $columnName => $value)
{
$transposedArray[$columnName][] = $value;
}
}
然后输出:
foreach ($transposedArray as $header => $values)
{
echo '<tr>';
echo "<td class='header'>{$header}</td>";
foreach ($values as $value)
{
echo "<td class='value'>{$value}</td>";
}
echo '</tr>';
}
或
// column1 in a row
echo '<tr>';
echo "<td class='header'>Readable Column Name1</td>";
foreach ($transposedArray['columnName1'] as $value)
{
echo "<td class='value'>{$value}</td>";
}
echo '</tr>';
// column2 in a row
echo '<tr>';
echo "<td class='header'>Readable Column Name2</td>";
foreach ($transposedArray['columnName1'] as $value)
{
echo "<td class='value'>{$value}</td>";
}
echo '</tr>';
// column3 in a row
...
答案 1 :(得分:0)
Thanx给Paul Spiegel,他给了我一个办法。
确实需要大量整理:)
查看
echo '<table>';
echo '<h2>Schemes</h2>';
$transposedArray = array();
foreach ($mscheme->result_array() as $mschemerow)
{
foreach ($mschemerow as $columnName => $value)
{
$transposedArray[$columnName][] = $value;
}
$value = form_open(base_url().'index.php/swim/mscheme_edit/'.$mschemerow['MainSchemeID']).form_submit('submit', 'Edit').form_close();
$transposedArray['Edit'][] = $value;
}
foreach ($transposedArray as $header => $values)
{
if($header == 'LocationName' || $header == 'Season' || $header == 'Award 1' || $header == 'Award 2' || $header == 'DayName' || $header == 'Week 1' || $header == 'Week 2' || $header == 'Week 3' || $header == 'Week 4' || $header == 'Week 5' || $header == 'Week 6' || $header == 'Week 7' || $header == 'Week 8' || $header == 'Week 9' || $header == 'Week 10' || $header == 'Week 11' || $header == 'Week 12' || $header == 'Week 13' || $header == 'Week 14' || $header == 'Week 15' || $header == 'Edit')
{
switch($header)
{
case 'LocationName':
$header = 'Location';
break;
case 'Season':
$header = 'Term';
break;
case 'DayName':
$header = 'Day';
break;
}
echo '<tr>';
echo "<td class='app'><b>{$header}</b></td>";
foreach ($values as $value)
{
echo "<td class='app'>{$value}</td>";
}
echo '</tr>';
}
}
echo '</table>';
模型
public function CRUD_read_mschemes()
{
return $this->db->select('*, p.Description AS Award 1, q.Description AS Award 2, a.MSchemeShort AS Week 1, b.MSchemeShort AS Week 2, c.MSchemeShort AS Week 3, d.MSchemeShort AS Week 4, e.MSchemeShort AS Week 5, f.MSchemeShort AS Week 6, g.MSchemeShort AS Week 7, h.MSchemeShort AS Week 8, i.MSchemeShort AS Week 9, j.MSchemeShort AS Week 10, k.MSchemeShort AS Week 11, l.MSchemeShort AS Week 12, m.MSchemeShort AS Week 13, n.MSchemeShort AS Week 14, o.MSchemeShort AS Week 15')
->from('schemesmain')
->join('schemesmaindetail AS a', 'MWeek1 = a.MSchemeID')
->join('schemesmaindetail AS b', 'MWeek2 = b.MSchemeID')
->join('schemesmaindetail AS c', 'MWeek3 = c.MSchemeID')
->join('schemesmaindetail AS d', 'MWeek4 = d.MSchemeID')
->join('schemesmaindetail AS e', 'MWeek5 = e.MSchemeID')
->join('schemesmaindetail AS f', 'MWeek6 = f.MSchemeID')
->join('schemesmaindetail AS g', 'MWeek7 = g.MSchemeID')
->join('schemesmaindetail AS h', 'MWeek8 = h.MSchemeID')
->join('schemesmaindetail AS i', 'MWeek9 = i.MSchemeID')
->join('schemesmaindetail AS j', 'MWeek10 = j.MSchemeID')
->join('schemesmaindetail AS k', 'MWeek11 = k.MSchemeID')
->join('schemesmaindetail AS l', 'MWeek12 = l.MSchemeID')
->join('schemesmaindetail AS m', 'MWeek13 = m.MSchemeID')
->join('schemesmaindetail AS n', 'MWeek14 = n.MSchemeID')
->join('schemesmaindetail AS o', 'MWeek15 = o.MSchemeID')
->join('location', 'LocationsID = MSchemeLocationID')
->join('termseason', 'SeasonsID = MSchemeSeasonID')
->join('awards AS p', 'MSchemeAwardsID1 = p.AwardID')
->join('awards AS q', 'MSchemeAwardsID2 = q.AwardID')
->join('days', 'DaysID = MSchemeDayID')
->get();
}