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

时间:2022-10-21 23:37:24

I am trying to display the records that are returned from a query in a html table. 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. I would want a new row in the table to be generated

我试图显示从html表中的查询返回的记录。因此,当处理查询时,mysql数据库表中的第一条记录(包含图像和歌曲标题)将显示在下面的html表(第1行,第1列)的第一个块中,第2条记录将显示在表的第二个块(第1行,第2列)等等,但是在显示了三个记录之后。我希望生成表中的新行

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.?

如果我犯了任何错误/错误,或者如果我浪费了你的时间,我会事先道歉,我对这件事情有点新意。

6 个解决方案

#1


2  

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.

    声明一个变量计数器。在开始时将其初始化为零,并在读取一个DB行时将其增加1。

  2. Whenever your counter reaches 2 modulo 3 :

    每当你的计数器达到2模3:

    if($counter % 3 == 2)

    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>

    $ image

inconditionnaly. But opening a new line (with tr) depends on 2.

inconditionnaly。但是打开一条新线(带tr)取决于2。

#2


1  

I would suggest, using a modulo operator % to detect each 3rd entry.

我建议,使用模运算符%来检测每个第3个条目。

E.g. (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>';

#3


0  

Please try the below code. Please try use mysqli, I have used that here mysqli_fetch_assoc($queryset). For testing you may change it to mysql_fetch_assoc($queryset).

请尝试以下代码。请尝试使用mysqli,我在这里使用了mysqli_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>

#4


0  

$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;

#5


0  

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);

#6


-3  

<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>

#1


2  

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.

    声明一个变量计数器。在开始时将其初始化为零,并在读取一个DB行时将其增加1。

  2. Whenever your counter reaches 2 modulo 3 :

    每当你的计数器达到2模3:

    if($counter % 3 == 2)

    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>

    $ image

inconditionnaly. But opening a new line (with tr) depends on 2.

inconditionnaly。但是打开一条新线(带tr)取决于2。

#2


1  

I would suggest, using a modulo operator % to detect each 3rd entry.

我建议,使用模运算符%来检测每个第3个条目。

E.g. (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>';

#3


0  

Please try the below code. Please try use mysqli, I have used that here mysqli_fetch_assoc($queryset). For testing you may change it to mysql_fetch_assoc($queryset).

请尝试以下代码。请尝试使用mysqli,我在这里使用了mysqli_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>

#4


0  

$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;

#5


0  

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);

#6


-3  

<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>