我正在尝试显示从html表中的查询返回的记录。因此,当处理查询时,mysql数据库表中的第一条记录(包含图像和歌曲标题)将显示在下面的html表(Row 1, Column1)
的第一个块中,第二条记录将显示在第二个记录中表(Row 1, Column2)
的块,依此类推,但在显示三条记录之后。我希望生成表中的新行
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 |
------------------------------------------
这是我的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...
到目前为止,我只能在表中显示结果,但我不知道如何以我上面给出的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>';
}
如果我犯了任何错误/错误,或者如果我浪费了你的时间,我事先道歉,我对这件事情有点新鲜。?
答案 0 :(得分:2)
您需要的是:
声明变量计数器。在开始时将其初始化为零,并在读取一个DB行时将其增加1.
每当你的计数器达到2模3:
if($counter % 3 == 2)
然后它意味着您需要在html表中添加一个新行。
不要为每个元素创建一个表。你需要添加
<td> $image<br /></td>
inconditionnaly。但是打开一个新行(带有tr)取决于2.
答案 1 :(得分:1)
我建议,使用模运算符%
来检测每个第3个条目。
E.g。 (没有经过测试,但应该这样做):
$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>';
答案 2 :(得分:0)
请尝试以下代码。请尝试使用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>
答案 3 :(得分:0)
我喜欢使用函数(以及方法和类)来组织和拆分任务。这使得事情更整洁,更易于维护和扩展,并支持以后可能的重复使用。
还记得使用mysqli或PDO而不是mysql扩展名,deprecated:
// $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);
答案 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 :(得分:-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>