Forum Moderators: coopster
I simplified my code below. Do I need to set 4 columns and 2 rows (4 items fit across the page with adequate spacing - fixed width) or is this code appropriate as is since the information should naturally wrap? Should this be coded differently? If so, how would you suggest and could you please explain the reason why? Let's say the town had a very long name. I guess that would misalign the information if I keep the code "as is" (the actual code has an image with information below it)? Thanks for you help.
<?php
include 'info.php';
$query = "SELECT firstname, lastname, town, state FROM members WHERE f = '1' AND fe > curdate() LIMIT 8";
$result = mysql_query ($query);
if($result){
echo '<table cellspacing="5" cellpadding="4"><tr>';
while ($row = mysql_fetch_array($result)) {
echo '<td><font face="arial" size="1">';
echo $row['firstname'].' '.$row['lastname']. '<br>''.$row['town'].' '.$row['state']. <br><br>
</td>';
}
echo '</tr></table>';
}
?>
I also find it better to add the \r\n in as when you view source it is easier to figure out where you went right and wrong.
This will give you 2 rows and 4 columns (assuming you have 8 results).
if($result){
echo '<table cellspacing="5" cellpadding="4">'."\r\n";
$tdcount = 1; //start cell count at one
while ($row = mysql_fetch_assoc($result)) {
if($tdcount % 4 == 1) echo "<tr>\r\n"; // check to see if new row needs to be started
echo '<td><font face="arial" size="1">';
echo $row['firstname'].' '.$row['lastname']. '<br>'.$row['town'].' '.$row['state']. '<br><br></td>'."\r\n";
if($tdcount % 4 == 0) echo "</tr>\r\n"; // check to see if new row needs to be ended
$tdcount++; // add one to cell count
}
echo "</table>\r\n";
}
or... if you might not have 8 results then you can fill the blanks cells with
$extra_cells = "";
$num_rows = mysql_num_rows($result); // get number of records from query
$extra_needed = 4-($num_rows % 4); //find out how many blank cells there will be
if($extra_needed!= 4 ){
for($i = 0; $i<$extra_needed;$i++){
$extra_cells .= "<td> </td>\r\n"; // loop the blank cells in
}
}if($result){
echo '<table cellspacing="5" cellpadding="4">'."\r\n";
$tdcount = 1; //start cell count at one
while ($row = mysql_fetch_assoc($query)) {
if($tdcount % 4 == 1) echo "<tr>\r\n"; // check to see if new row needs to be started
echo '<td><font face="arial" size="1">';
echo $row['firstname'].' '.$row['lastname']. '<br>'.$row['town'].' '.$row['state']. '<br><br></td>'."\r\n";
if($tdcount == $num_rows) echo $extra_cells; //echo extra cells into row
if($tdcount % 4 == 0) echo "</tr>\r\n"; // check to see if new row needs to be ended$tdcount++; // add one to cell count
}
echo "</table>\r\n";
}
$fidcheck = mysql_query("SELECT * FROM phpbb_posts
WHERE forum_id='2' ORDER BY `post_id` DESC LIMIT 36");
while($row = mysql_fetch_array($fidcheck))
{
$pid = $row['post_id'];
$posterid = $row['poster_id'];
$topicid = $row['topic_id'];
$first = mysql_query("SELECT * FROM phpbb_topics
WHERE topic_first_post_id='$pid'");
$vidnum = mysql_num_rows($first);
while($row = mysql_fetch_array($first))
{
$pid = $row['topic_first_post_id'];
$posterid = $row['topic_poster'];
$topicid = $row['topic_id'];
$getname = mysql_query("SELECT * FROM phpbb_users
WHERE user_id='$posterid'");
while($row = mysql_fetch_array($getname))
{
$author = $row['username'];
$authorid = $row['user_id'];
}
$pidcheck = mysql_query("SELECT * FROM phpbb_posts_text
WHERE post_id='$pid'");
while($row = mysql_fetch_array($pidcheck))
{
$text = $row['short_desc'];
$text = stripslashes($text);
$subject = $row['post_subject'];
$extra_cells = "";
$num_rows = mysql_num_rows($pidcheck); // get number of records from query
$extra_needed = 4-($num_rows % 4); //find out how many blank cells there will be
if($extra_needed!= 4 ){
for($i = 0; $i<$extra_needed;$i++){
$extra_cells .= "<td> </td>"; // loop the blank cells in
}
}
if($pidcheck){
echo "<table cellspacing='5' cellpadding='4'>";
$tdcount = 1; //start cell count at one
while ($row = mysql_fetch_assoc($pidcheck)) {
if($tdcount % 4 == 1) echo "<tr>"; // check to see if new row needs to be started
echo '<td>';
echo "<b><a href='videos.php?view=" . $topicid . "'></b><p>
<img src='images/videoimgs/" . $topicid . ".png' border='0' height='60px' width='60px' align='left'/><span class='genbig'>
" . $subject . "</a></span><br><span class='gensmall'>" . $text;
echo "</p></span><br><center>____________</center><br><td>";
if($tdcount == $num_rows) echo $extra_cells; //echo extra cells into row
if($tdcount % 4 == 0) // check to see if new row needs to be ended
$tdcount++; // add one to cell count
}
echo "</table>";
}
}
}
}
?>
</body>