PHP脚本将MySQL数据返回到表中,需要回车

时间:2019-03-08 16:23:16

标签: php mysql

我有一个PHP脚本,它从一个MySQL数据库表中选择信息,并使用while循环将其流入一个表中,然后使用foreach循环为每个单元格创建信息列(日期,图像,句子)。我的问题是这些列并排排列,横穿我的页面,一直延伸到远处(http://dankirchoff.com/drawingweek.php)。我需要某种“回车票”才能将其保留在网页上。我正在尝试使用“索引”字段作为计数器的模数,但无法正常工作。有什么建议吗?

<?php
	include ("inc_connect.php");
	if ($link !== FALSE) {
    	$db = "dkfineart_01";
    	mysqli_select_db($db);
    	$TableName = "drawingoftheweek";
    	$sql = "SELECT * FROM $TableName";
		if ($res = mysqli_query($link, $sql))	{
			if (mysqli_num_rows($res) > 0)	{
				while ($drawresults = mysqli_fetch_array($res))	{
					$drawing[] = $drawresults;
				}
				echo "<table style='margin-left:190px; width:500px;'><tr>";
				foreach ($drawing as $draw)	{
					echo "<td style='padding:20px 20px 0 0'>" . date('m/d/Y', strtotime($draw['date'])) . "</td>";
				}
				echo "</tr><tr>";
				foreach ($drawing as $draw)	{
					echo "<td style='padding:0 20px 0 0'>" . "<img src='images/" . $draw['image'] . ".jpg' width='400px'>" . "</td>";
				}
				echo "</tr><tr>";
				foreach ($drawing as $draw)	{
					echo "<td width='400px' style='padding:0 20px 0 0'>" . $draw['drawingtext'] . "</td>";
				}
				echo "</tr>";
				foreach ($drawing as $draw)	{
					$index = $draw['index'];
						if ($index % 2 === 0)	{
							echo "<br />";
						}
					}
				echo "</table>";
			}
				mysqli_free_result($res);
			}
			else	{
				echo "No matching records are found.";
			}
		}
		else	{
			echo "Unable to select from table. " . mysqli_error($link);
		}
	mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:2)

我的建议是使用除表格以外的其他东西来进行布局,并使用CSS创建中断。这样做的好处是,您可以迭代一次结果,而不是将结果存储在数组中并在数组中多次迭代。例如:

<style>
#dodWrapper {
    margin-left:190px; 
}
.dod {
    width:500px;
    display: inline-block;
}
.dodDate {
    padding:20px 20px 0 0;
}
.dodImage {
    padding:0 20px 0 0;
}
.dodImage img {
    width:400px;
}
.dodText {
    padding:0 20px 0 0;
    width:400px;
}
</style>
<?php
	include ("inc_connect.php");
	if ($link !== FALSE) {
    	$db = "dkfineart_01";
    	mysqli_select_db($db);
    	$TableName = "drawingoftheweek";
    	$sql = "SELECT * FROM $TableName";
		if ($res = mysqli_query($link, $sql))	{
			if (mysqli_num_rows($res) > 0)	{
                echo "<div id='dodWrapper'>";
				while ($draw = mysqli_fetch_array($res))	{
                    echo "<div class='dod'>";
                    echo "<div class='dodDate'>";
                    echo date('m/d/Y', strtotime($draw['date']));
                    echo "</div>";
                    echo "<div class='dodImage'>";
                    echo "<img src='images/" . $draw['image'] . ".jpg'>";
                    echo "</div>";
                    echo "<div class='dodText'>";
                    echo $draw['drawingtext'];
                    echo "</div>";
                    echo "</div>";
                }
                echo "</div>";
				
			}
				mysqli_free_result($res);
			}
			else	{
				echo "No matching records are found.";
			}
		}
		else	{
			echo "Unable to select from table. " . mysqli_error($link);
		}
	mysqli_close($link);
?>

这使用div代替表,并使用display:inline-block基本上将每个块都视为一个单词,并在“行”的末尾断开。