Two Column MYSQL display + Alph Grouping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jikson26
Forum Newbie
Posts: 1
Joined: Sun Mar 25, 2007 6:13 am

Two Column MYSQL display + Alph Grouping

Post by Jikson26 »

Hi everyone, I'm still a beginner at PHP, so bear with me :D

I have this code that I compiled from several tutorials:

Code: Select all

$columns = '2';

            $letterlinks = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            echo '<a name="#top"></a>';
            echo '<a href="#number">0-9</a> ';
            for ($i = 0; $i < 26; $i++)
            {
                echo '<a href="#'.$letterlinks[$i].'">'.$letterlinks[$i].'</a> ';
            }
            $num_rows = mysql_num_rows($result);
			echo "<table border=\"0\" cellspacing\"0\" cellpadding=\"0\" width=\"100%\">\n";
            	for($i = 0; $i < $num_rows; $i++)
            	{
            		$list = $db->sql_fetchrow($result);
            		
            		$letter = strtoupper(substr($list['name'], 0, 1));
               		 if (@$prev_row != '0-9' && is_numeric($letter))
               		 {
               		 	echo "<tr>\n";
               		 	echo "<td colspan=\"2\" align=\"center\">\n";
             	      	 echo '<hr><br /><a name="#number"></a><b><u>0-9</u></b> ';
               		     echo '<a href="#top"><i>goto top</i></a><br /><hr><br /><br />';
               		     echo "<td>\n";
               		     echo "</tr>\n";
              		     $prev_row = '0-9';
               		 }
               		 if ($letter != @$prev_row && !is_numeric($letter))
               		 {
               		 	echo "<tr>\n";
               		 	echo "<td colspan=\"2\" align=\"center\"><hr>\n";
               		     echo '<a name=#"'.$letter.'"></a><b><u>'.$letter.'</u></b> ';
              		     echo '<a href="#top"><i>goto top</i></a><br /><hr><br /><br />';
              		     echo "</td>\n";
              		     echo "</tr>\n";
              		     $prev_row = $letter;
              		 }
            		if($i % $columns == 0)
            		{
       					//if there is no remainder, we want to start a new row
       					echo "<tr>\n";
  				 	}
              		echo "<td>" . $list['name'] . "</td>\n";
              		if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows)
              		{
	      	 			//if there is a remainder of 1, end the row
        				//or if there is nothing left in our result set, end the row
        				echo "</tr>\n";
    				}
            	}
			echo "</table>\n";

The output looks like this:

feyd | 65K image changed to link.
http://img124.imageshack.us/img124/3239/ssax6.jpg

So, I was wondering why it doesn't two-column it correctly and how I fix it?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Comparing the HTML source to the script will help uncover the bug. It's likely that a TR or TD element is being opened or closed at the wrong time.
Post Reply