...

View Full Version : PHP & HTML Table Help



cthulhu522
03-19-2010, 09:37 PM
Hello and thanks in advance. I am pretty much new to php, and I have been having fun until now learning. lol I am honestly lost on this. Anyway, I am trying to get some data from MySql using php into a table with a format similar to this with links...

Name1 Name2 Name3 Name4
Name5 Name6 Name7 Name8
Name9 Name10 Name11 Name12

What I get so far is either

Name1
name2
Name3
Name4

Or

Name1 Name1 Name1 Name1
Name2 Name2 Name2 Name2

The code is below. Any help or good tutorials is appreciated.

<?


/* DB Connect */
$dbh = mysql_connect ("localhost", "XXXXXXXXXX",
"XXXXXXXX") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXXXXXXX", $dbh);
$result = mysql_query('SELECT * FROM `smf_members` WHERE `fbname` != \'\' ORDER BY `date_registered` ASC') ;
$num = mysql_numrows($result);

echo '<table>' ;

for($i = 0; $i < $num; $i++) {
$row = mysql_fetch_assoc($result);
$member_name = stripslashes($row['real_name']) ;
$fbid = $row['fbname'] ;
$link = '<a href="http://apps.facebook.com/castle_age/party.php?twt=jneg&jneg=true&user='. $fbid .'&lka='. $fbid .'=nf" target="_blank">'. $member_name .'</a>' ;


echo '<tr>' ;
echo '<td>'. $link .'</td>' ;
echo '</tr>' ;
}

echo '</table>' ;


mysql_close($dbh);
?>

MattF
03-19-2010, 09:50 PM
Try this. You'll need to add your db open/close code back in at the top/bottom of the file.



$count = 0;

print('<table>'."\n");

while ($row = mysql_fetch_assoc($result))
{
$link = '<a href="http://apps.facebook.com/castle_age/party.php?twt=jneg&jneg=true&user='.$row['fbname'].'&lka='.$row['fbname'].'=nf" target="_blank">'.stripslashes($row['real_name']).'</a>';

if ($count == 0)
{
print('<tr>');
}
++$count;
print('<td>'.$link.'</td>');

if ($count == 4)
{
$count = 0;
print('</tr>'."\n");
}
}
print('</table>'."\n");

cthulhu522
03-19-2010, 10:01 PM
I thank you sir... It worked perfectly. I have spent 2 days trying to get this to work the way I wanted, but I am pretty new to all of this. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum