Jump to content

if statement help, excuting code once something has ended


jesushax

Recommended Posts

hi all below is my code

 

it works fine apart from if the loop of rows ended on a number less than 6

 

at the moment i get three columsn fine then the last colum only has 3 <li></li>s in

so its not putting the final, </ul> and </div> on it

 

i cant think of how to check if there is less than 6 at the end without making it put a </ul></div> after every record

 

anyone help?

 

$SQL = mysql_query("SELECT * FROM tblTrades ORDER BY 'trades' DESC") or die(mysql_error());

$col = 1;
$colMax = 6;
while($list=mysql_fetch_array($SQL))  {
 if ($col == 1) {
	echo '<div class="columns">'."\n";
	echo "<ul>\n";
	}

	echo '<li>'.$list["trades"].'</li>'."\n";

if ($col == $colMax) {
	echo " </ul>\n";
	echo " </div>\n";
	$col = 1;
} else {
	$col++;		
	}

if ($col > $colMax) {
	echo " </ul>\n";
	echo " </div>\n";
	}

}

i think this will work

<?php
$SQL = mysql_query("SELECT * FROM tblTrades ORDER BY 'trades' DESC") or die(mysql_error());

$col = 1;
$colMax = 6;
while($list=mysql_fetch_array($SQL))  {
    if ($col == 1) {
      echo '<div class="columns">'."\n";
      echo "<ul>\n";
      }
      
      echo '<li>'.$list["trades"].'</li>'."\n";
      
   if ($col == $colMax) {
      echo " </ul>\n";
      echo " </div>\n";
      $col = 1;
   } else {
      $col++;      
      }
}
   if ($col != $colMax && $col != 1) {
      echo " </ul>\n";
      echo " </div>\n";
      }

Put a statement outside of the while loop that checks to see if your current column is less than the column max.

 

Also, why are you putting newlines after each of the elements?  Browsers don't read newlines unless you're doing that strictly for the readability of the source (and I'm not even sure that works?).

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.