Trouble with Tables

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
mcgeepj2
Forum Newbie
Posts: 1
Joined: Sun Dec 26, 2010 9:17 pm

Trouble with Tables

Post by mcgeepj2 »

Greetings all please forgive me for I am just beginning with php. My immediate problem is trying to get the below output to result in a table with the 2 cells for each field side by side. I can't get the printf line formatted correctly. Any help?

$result = mysql_query(
"SELECT * FROM `units` ORDER BY `field_1` ASC LIMIT 0, 30 ");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
printf("<P>" . " %s %s", $row["field_1"], $row["field_2"] . "</P>");
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Trouble with Tables

Post by social_experiment »

mcgeepj2 wrote:My immediate problem is trying to get the below output to result in a table with the 2 cells for each field side by side.

Code: Select all

<?php
$result = mysql_query(
"SELECT * FROM `units` ORDER BY `field_1` ASC LIMIT 0, 30 ");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
echo '<table>';
while ( $row = mysql_fetch_array($result) ) {
printf("<td>" . " %s %s", $row["field_1"] ."</td><td>". $row["field_2"] . "</td>"); 
}
echo '</table>';
?>
Just check the use of " with the printf() statement, mine might be prone to a syntax error.
Edit - Changed syntax.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply