Mysql将行显示为列

时间:2015-08-30 16:19:05

标签: php mysql

我想将行显示为列。

这是名为Term

的mysql表
|Name  ||Year      ||HTML ||CSS ||Js  ||
|Year1 ||2013-08-30||90   ||70  ||70  ||
|Year2 ||2014-08-30||100  ||65  ||80  ||
|Year3 ||2015-08-30||80   ||95  ||90  ||

我想要的是显示为像这样的列

 |Subject||Year1 ||Year2 ||Year3 ||
 |HTML   ||90|   ||100    ||80   ||
 |CSS    ||70|   ||65     ||95   ||
 |JS     ||70|   ||80     ||90   ||

代码

<tr>
    <th>Code</th><th>Subject</th><th>year1</th>
    <th>year2</th><th>year3</th>
    </tr>
        <?php 

        $query = mysql_query("SELECT * FROM term WHERE Stdid='$id'");
        while ($row = mysql_fetch_assoc($query)) {
    foreach($row as $key => $value) {

?>
    <tr>
    <th><?php echo $key ?></th> 
    <th><?php echo $value?></th>

    <th>99</th><th>00</th>
    </tr>
<?php  } }  } ?>

</table>

我的结果仅适用于第一年

 |Subject||Year1 ||Year2 ||Year3||
 |HTML   ||90|   ||?     ||?    ||
 |CSS    ||70|   ||?     ||?    ||
 |JS     ||70|   ||?     ||?    ||

2 个答案:

答案 0 :(得分:1)

首先需要将所有数据转置到临时数组中,然后才能再次输出。我将为您提供两种方法来实现此目的。

方法1:只获取每一行并按列索引切换行索引:

<table>
    <tr>
        <th>Subject</th>
        <th>year1</th>
        <th>year2</th>
        <th>year3</th>
    </tr>

    <?php
    $mysqli = new mysqli('localhost', 'user', 'pass', 'database');
    $id = 1;
    $report = array();
    $columnIndex = 0;
    $query = $mysqli->query("SELECT HTML, CSS, Js FROM term WHERE Stdid='$id'");
    while ($results = $query->fetch_assoc()) {
        foreach ($results as $course => $score) {
            $report[$course][$columnIndex] = $score;
        }
        $columnIndex++;
    }

    foreach ($report as $course => $results) { ?>
        <tr>
            <th><?php echo $course; ?></th>
            <?php foreach ($results as $score) { ?>
                <th><?php echo $score; ?></th>
            <?php } ?>
        </tr>
    <?php } ?>
</table>

方法2:获取数组中的所有行,使其成为数组数组,并使用带有回调array_map的{​​{1}}来转置它(参见http://php.net/manual/en/function.array-map.php处的示例4)。 您需要在初始数组中添加课程名称,以将其包含在最终结果中。

NULL

答案 1 :(得分:0)

试试这个。

<table width = "1093" align="center" bgcolor="pink">
<tr align="center">
<td colspan="6"><h2>View Term</h2></td>
</tr>

<tr align="center" bgcolor="">
<th>S.N</th>
<th>Subject</th>

<th>Year 1</th>
<th>Year 2</th>
<th>Year 3</th>

</tr>


<?php 


$db = mysqli_connect("localhost","root","YOURPASSWORD","YOURDATABASE");
if (mysqli_connect_errno())

{
    echo"The Connection was not established" . mysqli_connect_error();
    exit();
}


$get_term = "SELECT * FROM term WHERE Stdid='$id'";

$run_term = mysqli_query($db,$get_term);
$i =0 ;


while ($row=mysqli_fetch_array($run_term ))

{
// Get  data from database.
// Change the input in $row[''] if the name of column is different in your tble "term".
    $subject=$row['subject'];

    $year1=$row['year1'];
    $year2=$row['year2'];
    $year3=$row['year3'];

    $i++;   



?>

<tr align="center">

<td> <?php echo $i;?></td>
<td><?php echo $subject;?></td>
<td><?php echo $year1;?></td>
<td><?php echo $year2;?></td>

<td><?php echo $year3;?></td>

</tr>
<?php }?>

</table>