我有一个名为Table_I的表,有31行。我想将每一行显示到一个包含31个单元格的预定义(现有)表中。也就是说,每个表行进入每个单独的表格单元格。
Table_I
ID | Name | Rowcol |
.....................
1 | ABC | 3-1 |
2 | PQR | 3-2 |
3 | XYZ | 3-3 |
4 | MNO | 3-4 |
5 | DEF | 4-1 |
6 | QRS | 4-2 |
7 | UVW | 4-3 |
8 | IJK | 4-4 |
输出:
Predefined table
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
.....................................
| ABC | PQR | XYZ | MNO |
.....................................
| DEF | QRS | UVW | IJK |
代码:
<?php
...Connection to DB...
$query="SELECT name, rowcol
FROM Teble_I
";
$params = array($_REQUEST['query']);
$results = sqlsrv_query($conn, $query, array(), array( "Scrollable" => 'static' ));
if($results===false)
{ die( FormatErrors( sqlsrv_errors() ) ); }
if($results===false)
{ die( FormatErrors( sqlsrv_errors() ) ); }
$r=3;
$c=1;
while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
?>
<body>
<table width="200" border="1">
<tr>
<?php
if($row['rowcol']==$r ."-". $c)
{
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
}
$c++;
if ($r==3 && $c==5 || $r==4 && $c==5)
{
$r++;
$c=1;
}
?>
</tr>
<tr>
<?php
if($row['rowcol']==$r ."-". $c)
{
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
echo "<td>".$row['name']."<td>";
}
$c++;
if ($r==3 && $c==5 || $r==4 && $c==5)
{
$r++;
$c=1;
}
}
?>
</tr>
</table>
预期产出:
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
.....................................
| ABC | PQR | XYZ | MNO |
.....................................
| DEF | QRS | UVW | IJK |
真的需要和赞赏帮助。
提前致谢。
答案 0 :(得分:0)
使用动态SQL查询。
<强>查询强>
declare @sql as varchar(max);
select @sql = 'select ' + stuff(
(
select distinct ',max(case ID when ' + cast(ID as varchar(2)) + '
then Name end) as [Cell ' + cast(ID as varchar(2)) + ']'
from Table_I
for xml path('')
),
1, 1, ''
);
select @sql += ' from Table_I;'
execute(@sql);
答案 1 :(得分:0)
一个非常基本的例子,可能适合您的需求。 $data
仅用于演示目的,它应该是您的SQL数据。
$data = [
['ID' => 1, 'Name' => 'ABC', 'Rowcol' => '3-1'],
['ID' => 2, 'Name' => 'PQR', 'Rowcol' => '3-2'],
['ID' => 3, 'Name' => 'XYZ', 'Rowcol' => '3-3'],
['ID' => 4, 'Name' => 'MNO', 'Rowcol' => '3-4'],
['ID' => 5, 'Name' => 'ABC', 'Rowcol' => '4-1'],
['ID' => 6, 'Name' => 'PQR', 'Rowcol' => '4-2'],
['ID' => 7, 'Name' => 'XYZ', 'Rowcol' => '4-3'],
['ID' => 8, 'Name' => 'MNO', 'Rowcol' => '4-4']
];
$finalData = [];
foreach($data as $row) {
$position = explode('-', $row['Rowcol']);
$finalData[$position[0]][$position[1]] = $row['Name'];
}
print_r($finalData);
$output = "<table>";
$output .= "<tr>";
for($i=1;$i<=count(reset($finalData));$i++) {
$output .= "<th>Cell " . $i . "</th>";
}
$output .= "</tr>";
foreach($finalData as $row) {
$output .= "<tr>";
foreach($row as $column) {
$output .= "<td>" . $column . "</td>";
}
$output .= "</tr>";
}
$output .= "</table>";
print $output;
$finalData
将是一个映射数组,每个行和列都有一个条目:
Array
(
[3] => Array
(
[1] => ABC
[2] => PQR
[3] => XYZ
[4] => MNO
)
[4] => Array
(
[1] => ABC
[2] => PQR
[3] => XYZ
[4] => MNO
)
)
答案 2 :(得分:0)
修改你的代码:
<body>
<table width="200" border="1">
<tr>
<?php
$i=0
while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
$i++;
echo "<td>".$row['name']."</td>";
if ($i%4 == 0)
echo "</tr><tr>";
}
?>
</tr>
</table>
&#13;
答案 3 :(得分:0)
谢谢你们!我用数组计算出来了。以下是我的表现:
<?php
...Connection to DB...
$query="SELECT name, rowcol
FROM Teble_I
";
$params = array($_REQUEST['query']);
$results = sqlsrv_query($conn, $query, array(), array( "Scrollable" => 'static' ));
if($results===false)
{ die( FormatErrors( sqlsrv_errors() ) ); }
if($results===false)
{ die( FormatErrors( sqlsrv_errors() ) ); }
// set array
$array = array();
//select and display record from database
while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
// add each row returned into an array
$array[] = $row;
}
?>
<body>
<table width="200" border="1">
<tr>
<td><?php echo $array[0]['name']; ?></td>
<td><?php echo $array[1]['name']; ?></td>
<td><?php echo $array[2]['name']; ?></td>
<td><?php echo $array[3]['name']; ?></td>
</tr>
</table>
我很兴奋!