0

I have a PHP script selecting information from a MySQL database table and flowing it into a table using a while loop and then a foreach loop for each cell creating columns of information (date, image, sentence). My problem is these columns are side-by-side, running across my page and off into the distance (http://dankirchoff.com/drawingweek.php). I need a "carriage return" of some sort to keep them on the web page. I'm trying a modulus using the 'index' field as a counter, which isn't working. Any suggestions?

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

5
  • 2
    You're creating a new table for each image, is that what you want to do? If not, put the opening and closing table tags outside of the loop and your table display issue should be fixed. Commented Mar 8, 2019 at 16:28
  • That's what I thought I was doing, creating a series of small tables running across the page. I just tried your suggestion — putting the opening and closing table tags outside my loop — but I didn't see any difference in the result. Commented Mar 8, 2019 at 16:37
  • It should not be the same result if you do that, please update your code with how you moved the table tags. Commented Mar 8, 2019 at 16:42
  • Probably not worthy of a full answer, but you can do the same thing without tables using some CSS. Much nicer IMO. jsfiddle.net/e198z3k6/1 Commented Mar 8, 2019 at 17:08
  • Better styling: jsfiddle.net/sqn3p7tj Commented Mar 8, 2019 at 17:24

1 Answer 1

2

My suggestion would be to use something other than a table to lay this out and use CSS to create the breaks. This has the side benefit of allowing you to iterate over the results once rather than storing them in an array and iterating over the array multiple times. For example:

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

This uses divs in place of tables and display:inline-block to essentially treat each block like a word and break at the end of a "line."

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Nate, now were's getting somewhere. I used your suggested code, and I'm getting the layout I want — but I somehow lost the links to my images and text. I'll work on that. Thanks for your help!
Okay, following up on my comment, I corrected a small error I made in my code (didn't change my variable $drawresults to $draw in the while loop. That's corrected and everything came in fine. Many thanks Nate!
Dan - And when you get really ambitious, look into laying out your divs with flexbox! That's when stuff really starts to get fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.