Jump to content

PHP in tables.


waverider303

Recommended Posts

Im trying to create a blog and I want to have top stories on the main page in a 2X3 table (2 columns x 3 rows). I have this code so far..

 

<?php
                    	$sql = "SELECT id, title, summary, main_image ";
					$sql .= "FROM top_stories ";
					$sql .= "ORDER BY id DESC ";
					$sql .= "LIMIT 4";
					$result = mysql_query($sql);

					while($row = mysql_fetch_assoc($result)) {
						$id = $row['id'];
						$title = $row['title'];
						$summary = $row ['summary'];
						$main_image = $row['main_image'];

						echo "<td width=\"150px\" height=\"115px\">";
     						echo "<img src=\"story_images/$small_file_name\" width=\"100\" height=\"100\" /><br /><a href=\"post.php?id=$id\"><font class=\"homehead\">$title</font></a><br /><font class=\"homecopy\">$summary</font><br /><a href=\"post.php?id=$id\">(Read More)</a>";
						echo "</td>";

 

The problem is how would I add in a second row without duplicating what is in the row above it?

Link to comment
https://forums.phpfreaks.com/topic/118977-php-in-tables/
Share on other sites

Basically u should have a $counter variable which is incremented and checks if the moduls 2 = 0 and creates a tr. The idea:

 

<?php
$counter = 0;
while($row = mysql_fetch_assoc($result)){
   if($counter % 2 == 0 or $counter == 0){ echo "<tr>"; $lastTR = '</tr>'; } else{ $lastTR = ''; }
   }
   //the tds
   echo $lastTR;
}
?>

 

That should get u a 2 columns table, while rows can be adjusted by the LIMIT clause in the query (for a 2x3 table u should have it LIMIT 6). Anyway ive always struggled with grids (and im not sure the above code will work :)) so i find it better using css and floats on a fixed width div.

 

[wrapper_div width=2*story_div+margins]

[story_div]:float (margin) [story_div]:float

[story_div]:float (margin) [story_div]:float

[/wrapper_div]

 

Hope it helped.

Link to comment
https://forums.phpfreaks.com/topic/118977-php-in-tables/#findComment-612671
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.