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";
	}

}

Link to comment
Share on other sites

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";
      }

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.