Page 1 of 1

Having a bit of trouble with alternating table BG's

Posted: Wed Nov 06, 2002 7:31 pm
by Little Spy

Code: Select all

dbConnect();
   $result = mysql_query("SELECT * FROM userinfo");
   $content = "<table cellpadding="1" cellspacing="0" border="0" align="center" width="586" class="tbl_border">";
   $content = "  <tr>";
   $content = "    <td width="715">";
   $content = "    <table cellpadding="4" cellspacing="0" border="0" width="645" style="border-collapse: collapse" bordercolor="#111111">";
   $content = "      <tr>";
   $content = "        <td class="tbl_caption" colspan="2" height="12" width="661">Find User To";
   $content = "        Modify > Letter A</td>";
   $content = "      </tr>";
   while ($data = mysql_fetch_assoc($result)) &#123;
         $username = $data&#1111;'username'];
         $i = 0;
         if ($i % 1 == 0) &#123;
               $content = "      <tr class="tbl_light">";
               $content = "        <td height="18" align="left" valign="top" width="550">". $username ."</td>";
               $content = "        <td width="104" height="18" align="left" valign="top">";
               $content = "        <p align="center">&#1111;Modify]</td>";
               $content = "      </tr>";
         &#125; if ($i % 2 == 1) &#123;
               $content = "      <tr class="tbl_dark">";
               $content = "        <td height="18" align="left" valign="top" width="550">" . $username. "</td>";
               $content = "        <td width="104" height="18" align="left" valign="top">";
               $content = "        <p align="center">&#1111;Modify]</td>";
         &#125;
            $i++;
   &#125;
   $content = "      </tr>";
   $content = "    </table>";
   $content = "    </td>";
   $content = "  </tr>";
   $content = "</table>";
thats my code chunk maybe someone can tell me whats wrong

Posted: Thu Nov 07, 2002 3:03 am
by twigletmac
Maybe this will help:

Code: Select all

&lt;?php

dbConnect();

$sql = "SELECT username from userinfo";
@$result = mysql_query($sql) or die(mysql_error());
$content =&lt;&lt;&lt;END

&lt;table cellpadding="1" cellspacing="0" border="0" align="center" width="586" class="tbl_border"&gt; 
&lt;tr&gt;
	&lt;td width="715"&gt;
	&lt;table cellpadding="4" cellspacing="0" border="0" width="645" style="border-collapse: collapse" bordercolor="#111111"&gt;
		&lt;tr&gt;
			&lt;td class="tbl_caption" colspan="2" height="12" width="661"&gt;Find User To Modify &gt; Letter A&lt;/td&gt;
		&lt;/tr&gt;
END;
$bgclass = 'light';
while ($data = mysql_fetch_assoc($result)) { 
	$username = $data&#1111;'username']; 
	$bgclass = ($bgclass == 'light') ? 'dark' : 'light';
	$content .=&lt;&lt;&lt;END

		&lt;tr class="tbl_$bgclass"&gt;
			&lt;td height="18" align="left" valign="top" width="550"&gt;$username&lt;/td&gt;
			&lt;td width="104" height="18" align="left" valign="top"&gt;&lt;p align="center"&gt;&#1111;Modify]&lt;/td&gt;
		&lt;/tr&gt;
END;
} 
$content .=&lt;&lt;&lt;END

	&lt;/table&gt;
	&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
END;
?&gt;
The <<<END ... END; syntax is called heredoc and using it means your source code will be formatted with linebreaks and tabs.

Code: Select all

$content =&lt;&lt;&lt;END
This uses the "here document" (or "heredoc") syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator (ie. END;) must appear on a
line with just a semicolon no extra whitespace (which the forum so helpfully adds on)!
END;
It simplifies your HTML a bit and I've also added a new bit for doing the row colours.

Mac