Link to home
Start Free TrialLog in
Avatar of t3chguy
t3chguyFlag for United States of America

asked on

Extra blank value checkmark displaying

I have an array derived from a query that is supposed to display in two columns; however, on the second row, there is an extra blank checkmark.  Maybe I'm overtired, but I can't for the life of me get it removed.

It's there because I only have three values right now, but because the row is cutting off after two, the extra one appears.

HELP!


                                                $sql9 = "SELECT id, subject FROM pr_email_templates WHERE  category = 'Templated Email' and active = '1'";
                                                $res9 = mysql_query($sql9) or die(mysql_error());
                                                $num = mysql_num_rows($res9);

                                                echo '<table width="100%" cellspacing="5" cellpadding="5" border = "0" align="center">';
            
                                                echo '<table width="100%" cellspacing="5" cellpadding="5" border = "0" align="center">';
                                    
                                                for ($a = 0; $a < $num;++$a)
                                                      {
                                                      echo '<tr>';
                                                      for($count=0;$count<2;$count++)
                                                            {
                                                            $emailid = mysql_result($res9, $a, 0);
                                                            $emailname = mysql_result($res9, $a, 1);
                                                            
                                                            echo '<td width="200px"><input type="checkbox" id="a" value="'.$emailid.'" name="emailtype">' . $emailname . '</td>';
                                                             $a++;
                                                            }
                                                             
                                                            echo '</tr>';
                                                            $a--;
                                                      }            

                                                echo '</table>';
picattach.png
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

The reason you're getting a problem is because your increment the $a variable (++$a) and then decrement it ($a--), so you loop is losing count.

There's a couple of tricks to getting it to work the way you want. You don't need the for loop for looping through a database - you can use mysql_fetch_array. Also, you can perform a modulus function to see if you should be adding the <TR> and </TR>. The modulus operator is %. Have a look at the following code, play around with it, and ask if you need anything explaining.

<table>
<?php
	$sqlStr = "SELECT id, subject FROM pr_email_templates WHERE category = 'Templated Email' and active = '1'";
	$result = mysql_query($sqlStr) or die(mysql_error());
	
	$numOfCols = 2; //used to make sure we add the <TR> and </TR> in the right place
	$a = 0; //a record counter, again for creating the <TR> and </TR>
	
	while ($row = mysql_fetch_array($result)):
		echo ($a % $numOfCols == 0) ? "<tr>" : "";
		printf('<td width="200px"><input type="checkbox" value="%s" name="emailtype">%s</td>', $row['id'], $row['subject']);
		echo ($a % $numOfCols == 1) ? "</tr>" : "";
		$a++;
	endwhile;
	
	echo ($a % $numOfCols == 1) ? "<td></td></tr>" : ""; //finish off the row if needed.
?>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of t3chguy

ASKER

Worked perfectly, thank you!
Thanks for the points and thanks for using EE, ~Ray