简体   繁体   English

如何在HTML表中显示MySQL查询的结果

[英]How to display results from a MySQL query in a HTML table

I am trying to display the records that are returned from a query in a html table. 我正在尝试在html表中显示从查询返回的记录。 So when the query is processed the 1st record in the mysql database table (containing the image and the song title) will be displayed in the first block in the html table (Row 1, Column1) below and the 2nd record will be displayed in the second block of the table (Row 1, Column2) and so on, but after three records have been displayed. 因此,当查询被处理时,mysql数据库表中的第一条记录(包含图像和歌曲名称)将显示在下面的html表(Row 1, Column1)的第一块中(Row 1, Column1)第二条记录将显示在表的第二个块(Row 1, Column2) ,依此类推,但是在显示了三个记录之后。 I would want a new row in the table to be generated 我想要在表中生成新行

HTML table output should look like this below: HTML表输出如下所示:

           column 1     column 2     column 3
         -----------------------------------------
         | Image1      | Image2      | Image3      |
row 1 -> | song title1 | song title2 | song title3 |      
         ------------------------------------------
         | Image 4     | Image 5     | Image6      |
row 2 -> | Song title4 | song title5 | song title6 |
         ------------------------------------------

And here is the structure of the table in my mysql database: 这是我的mysql数据库中表的结构:

search
         ------------------------------
        |  Id   |  image |    song     |
         ------------------------------
row 1-> |   1   | image1 | song title1 |
         ------------------------------
row 2-> |   2   | image2 | song title2 | 
         ------------------------------
row 3-> |   3   | image3 | song title3 |
         ------------------------------


row 4...

So far I can only display the results in a table but I don't know how to structure the table in the format of the html table example I gave above Here is the code I have so far: 到目前为止,我只能在表格中显示结果,但是我不知道如何以上面给出的html表格示例的格式来构造表格。这是到目前为止的代码:

while($row = mysql_fetch_assoc($queryset)) {
                        $id = $row['id'];
                        $image = $row['image'];
                        $song = $row['song'];

                        echo '<table style="width:100%">
                                  <tr>

                                    <td>

                                        $image
                                        <br>
                                        $song 

                                    </td>
                                  </tr>
                               </table>';





                    }

I apologies in advance if I have made any mistakes/errors or if I have wasted you time, I'm kind of new to this whole thing.? 如果我有任何错误/错误,或者如果我浪费了您的时间,我会事先道歉,我对这件事很陌生。

What you need is: 您需要的是:

  1. to declare a variable counter. 声明一个变量计数器。 Initializes it to be zero at start, and increases it by one when one DB row have been read. 在开始时将其初始化为零,并在读取一个数据库行时将其增加一。

  2. Whenever your counter reaches 2 modulo 3 : 每当您的计数器达到2模3时:

    if($counter % 3 == 2)

then it means you need to add a new line in your html table. 那么这意味着您需要在html表中添加新行。

  1. Do not create a table for each element. 不要为每个元素创建一个表。 You need to add 您需要添加

    <td> $image<br /></td>

inconditionnaly. inconditionnaly。 But opening a new line (with tr) depends on 2. 但是用(tr)打开新行取决于2。

I would suggest, using a modulo operator % to detect each 3rd entry. 我建议使用模运算符%来检测每个第三项。

Eg (not tested, but should do the trick): 例如(未经测试,但应该可以解决):

$entryCounter = 0;
$numberOfEntries = mysql_num_rows($queryset);

echo '<table style="width:100%"><tr>';

while($row = mysql_fetch_assoc($queryset)) {
    $id = $row['id'];
    $image = $row['image'];
    $song = $row['song'];

    echo "<td>$image<br />$song</td>";

    if (($entryCounter % 3) == 2 && $entryCounter != $numberOfEntries) 
    {
        echo '</tr><tr>';
    }

    $entryCounter++;

}

echo '</tr></table>';

Please try the below code. 请尝试以下代码。 Please try use mysqli, I have used that here mysqli_fetch_assoc($queryset). 请尝试使用mysqli,我在这里使用了mysqli_fetch_assoc($ queryset)。 For testing you may change it to mysql_fetch_assoc($queryset). 为了进行测试,您可以将其更改为mysql_fetch_assoc($ queryset)。

<table style="width:100%">
<?php $count = 0; /* Since you want 3 rows fetched from database in 3 columns of a row, If am using a count variable to print the column in table.*/
      while($row = mysqli_fetch_assoc($queryset)) { ?>
<?php if($count == 0){ /* will start a new row. */ ?> 
    <tr>
<?php } ?>

<?php if($count < 3){  /* will add columns in row. */ ?> 
    <td>
        <?php $row['image']; ?>
    </br>
        <?php $row['song'];  ?> 
    </td>
<?php } ?>


<?php if($count == 2){ /* will end row when 3 columns are added. */ ?> 
    </tr>
<?php } ?>

<?php $count = ($count++) % 3;  /* will reset the count for new row to start. */ ?> 

<?php } ?>
</table>

I like to use functions (and methods and classes) to organize and split up tasks. 我喜欢使用函数(以及方法和类)来组织和拆分任务。 This makes things neater, more maintainable and extensible, and supports possible re-use later on. 这使事情变得更整洁,更易于维护和扩展,并支持以后可能的重用。

Also remember to use mysqli or PDO instead of the mysql extension, which is deprecated : 还请记住使用mysqli或PDO而不是已弃用的mysql扩展名:

// $where and $order_by correspond to the sql WHERE and ORDER BY clauses
function getSongs($where = null, $order_by = null) {
    $tbl_songs = array();
    /* Put your specific mysqli or PDO code here, which should return a table (array of arrays) using $where: 
    Ex: $tbl_songs[0] = array("id" => 1, "image" => "image1", "song" => "title1");
        $tbl_songs[1] = array("id" => 2, "image" => "image2", "song" => "title2")
    */  
    return $tbl_songs;
}

function viewSong($id, $src, $title) {
    $html = "<img id='$id' src='$src' alt='$title'>\n";
    $html .= "<p>$title</p>\n";
    return $html;
}

function viewSongs($columns = 3, $where = null) {
    $html = null;
    $tbl_songs = getSongs($where);
    if ($tbl_songs) {
        $songs = count($tbl_songs);
        $html = "<table>\n<tr>\n";
        foreach ($tbl_songs as $i => $arr_song) {
            $html .= "<td>" . viewSong($arr_song["id"], $arr_song["image"], $arr_song["song"]) . "</td>\n";
            if (!(($i+1) % $columns) && (($i+1) < $songs)) {
                $html .= "</tr>\n<tr>\n";
            }
        }
        $html .= "</tr>\n</table>\n";
    }
    return $html;
}

$columns = 3;
echo viewSongs($columns);
$html=''; 
$htmlRow='';
$i=1;
while($row = mysql_fetch_assoc($result)) {
  $htmlRow.="<td>".$row[image]."<br>".$row[ song]." </td>"; 
  if($i==3){
    $html.="<tr>$htmlRow</tr>";
    $htmlRow='';
    $i=0;
  }
  $i++;
}

echo $html;
<table>

<?php 
while($row = mysql_fetch_assoc($queryset)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["image"]; ?></td>
<td><?php echo $row["song"]; ?></td>
</tr>
<?php
}
?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM