PHP HTML Formatting

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
zimmo
Forum Newbie
Posts: 1
Joined: Wed Feb 26, 2003 11:18 am

PHP HTML Formatting

Post by zimmo »

Hi All,

I am new to this forum, and would really appreciate if someone can help me with this.

What it is I am using php to fetch the results and display only 8 on the page, now I know how to get the results etc.. but the formatting on the page is where my problem lies.

If you look at the code for my page here, just the section I need. You will see that if I limit the results to 8 they print straight across the page, what I need is for 4 columns to print across then to loop and print another row with 4 columns so we have two rows of four columns, but I am unsure how to do this, as i have always worked with rows and never having to do this in columns.

Here is my code:

<?
//BZDP PHP Connect Code:

$user = "*****";
$pass = "*****";
$db = "*****";
$link = mysql_pconnect( "localhost", $user, $pass );
if ( ! $link)
die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link )
or die ("Couldn't open $db: ".mysql_error() );

$sql= "SELECT * FROM clients ORDER BY id DESC LIMIT 8";
$sql_result = mysql_query($sql);

if (mysql_num_rows($sql_result) ==0)
{
echo (" <P></P>\n");
}
else {
?>
<TR VALIGN=TOP>
<TD></TD>
<TD>
<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=2>
<TR>
<?
while ($row = mysql_fetch_array($sql_result)){

echo (" <TD HEIGHT=137 WIDTH=194 VALIGN=TOP>\n");
echo (" <P><FONT FACE=\"Verdana\" SIZE=2><SPAN CLASS=\"style18\"><B>$row[clientname]</B></SPAN></FONT></P>\n");
echo (" <P><FONT FACE=\"Verdana\" SIZE=2><SPAN CLASS=\"style18\"><IMG SRC=\"Resources/portfo3.gif\" BORDER=0 WIDTH=189 HEIGHT=81 ALIGN=ABSBOTTOM HSPACE=0 VSPACE=0></SPAN></FONT><FONT FACE=\"Verdana\" SIZE=2><SPAN CLASS=\"style18\"></SPAN></FONT></P>\n");
echo (" <P><FONT FACE=\"Verdana\" SIZE=2><SPAN CLASS=\"style18\">$row[description]</SPAN></FONT></P></TD>\n");

}
}
?>
</TR>
<TR>
<TD HEIGHT=9 WIDTH=782 COLSPAN=4 VALIGN=TOP><IMG SRC="Resources/_clear.gif" BORDER=0 WIDTH=1 HEIGHT=1 ALT=""></TD>
</TR>
</TABLE>

END CODE -------

You can see that the 8 entries go across the page. I need to show 4 then another 4 underneath.

Appreciate if anyone can help.
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Looks to me like your continuously looping a through a single <td></td> and don't create a new row until you break out of the loop. Put $row[description] in a new row in the same table.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

As a rule, it's best to put as little html as possible in the php: ie you define $vars in php then include an html template with little snippets of embedded php like <?php echo $var; ?>. Sometimes it does make sense to have the odd html tag in php though.

This is a sort of nTier approach where you keep different layers separate as far as is practical:

formatting / layout (html)
data processing (php)
data access (php)
data storage (mysql etc)

The advantage of separating is that designers can do their thing and the programmers can do theirs. Even if you're doing it all yourself, it keeps things clearer by separating it out and, if you have something like Dreamweaver, it's much easier to create the html with that than it is writing it out by hand in php.

This doesn't answer you're question directly but you might find that a lot of problems simply disappear with this approach.
Post Reply