/    Sign up×
Community /Pin to ProfileBookmark

Need help with php mysql and displaying in table rows columbs etc

Hi i want to pull info from a database like in the following example

[CODE]<table width=”90%” border=”0″>
<?PHP
$query = “SELECT * FROM speakers WHERE featured = ‘yes’ ORDER BY lname ASC”;
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

while ($row) {
print “<tr>”;
print ” <td align=”left”>”;
print ” <b><a href=”speaker-“.$row[‘id’].”-“.$row[‘fname’].”-“.$row[‘lname’].”.html”>”;
print $row[‘fname’].’ ‘.$row[‘lname’];
print ” </b><br>”;
print ” <img width=”30%” height=”30%” src=”speakers-photos/”.$row[‘photo’].”” alt=””.$row[‘fname’].” “.$row[‘lname’].”” border=”0″/></a>”;
print ” </td>”;
print “</tr>”;

$row = mysql_fetch_array($result);
}
?>
</table>[/CODE]

The above takes my info from the database and displays a new row in the table for each entry

however i want it to display 3 database entries per row instead of just one but all i can seem to do is display one entry per row or one entry per colum

any help please asap

thanks

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — This is some quick-n-dirty code to do 3 per row:
[code=php]$counter = 0
while ($row = mysql_fetch_array($result)) {
if($counter==0) print "<tr>";
++$counter;

print " <td align="left">";
print " <b><a href="speaker-".$row['id']."-".$row['fname']."-".$row['lname'].".html">";
print $row['fname'].' '.$row['lname'];
print " </b><br>";
print " <img width="30%" height="30%" src="speakers-photos/".$row['photo']."" alt="".$row['fname']." ".$row['lname']."" border="0"/></a>";
print " </td>";

if($counter==3) {
$counter=0;
print "</tr>";
}
}
while ($counter>0) {
++$counter;
print ' <td>&nbsp;</td>';
if($counter==3) {
$counter=0;
print "</tr>";
}
}[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — Here is a smaller and alot cleaner way than so_is_this's

[code=php]<table width="90%" border="0">
<?PHP
$query = "SELECT * FROM speakers WHERE featured = 'yes' ORDER BY lname ASC";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result)){
echo"<tr>n";
for($c = 0;$c < 3; $c++){
echo'<td align="left"><b><a href="speaker-'.$row['id'].'-'.$row['fname'].'-'.$row['lname'].'.html">
'.$row['fname'].' '.$row['lname'].'
</b><br>
<img width="30%" height="30%" src="speakers-photos'.$row['photo'].'" alt="'.$row['fname'].' '.$row['lname'].'" border="0" /></a>
</td>';
}
echo"<tr>n";
}
?>
</table>[/code]
Copy linkTweet thisAlerts:
@BWWebDesignsauthorOct 25.2006 — hi the little guy

yours does not work

it produces 3 copies of each records per rew

i want 3 individual different records per row
Copy linkTweet thisAlerts:
@BWWebDesignsauthorOct 25.2006 — however so is this yours worked fine
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — Yeah, I just realized that, I there is a fix for it, if you want it
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — Here is my fix:
[code=php]<table width="90%" border="0">
<?PHP
$query = "SELECT * FROM speakers WHERE featured = 'yes' ORDER BY lname ASC";
$result = mysql_query($query) or die(mysql_error());
$nrow = mysql_num_rows($result)
$trow = 0;
while ($trow < $nrow){
echo"<tr>n";
for($c = 0;$c < 3; $c++){
$row = mysql_fetch_array($result);
echo'<td align="left"><b><a href="speaker-'.$row['id'].'-'.$row['fname'].'-'.$row['lname'].'.html">
'.$row['fname'].' '.$row['lname'].'
</b><br>
<img width="30%" height="30%" src="speakers-photos'.$row['photo'].'" alt="'.$row['fname'].' '.$row['lname'].'" border="0" /></a>
</td>';
}
echo"<tr>n";
$trow++;
}
?>
</table>[/code]
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — Here is a smaller and alot cleaner way than so_is_this's[/QUOTE]
No need to get nasty ? I admitted it was quick-n-dirty. However, it looks like yours will also potentially leave the last row short of three cells. That will mess up some browser's ability to correctly display the table.
Copy linkTweet thisAlerts:
@BWWebDesignsauthorOct 25.2006 — now that fixed the problem with regards repeated records but now i get alot of blank records displayed after the actual content

i only have 12 records in the database but its displaying over 20

i think ill just use the other guys for now as it works perfectly

thanks
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — Does it forget the last 3? I never tested it.
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — What I meant is that if the number of database rows is not evenly divisible by 3, then additional blank HTML table cells need to be added to fill out the HTML table row. My dirty code does that.
Copy linkTweet thisAlerts:
@BWWebDesignsauthorOct 25.2006 — yeah your code works great thanks
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — No es un problema.

Salud!
×

Success!

Help @BWWebDesigns spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...