hdbrandon Posted June 15, 2011 Share Posted June 15, 2011 I'm building a photo gallery table of X rows and 8 columns/pictures wide based on the numer of images in the database. Having a brain-freeze though and can't get it to create additional rows. Also, the ending table row tag is nesting properly. Logic problem maybe? Thanks for any help. $sql="SELECT * FROM uploads WHERE (userID LIKE '%$searchString%') OR (alt LIKE '%$searchString%') OR (date LIKE '%$searchString%') OR (store LIKE '%$searchString%');"; $result=mysql_query($sql); $num=mysql_numrows($result); $i=0; $rows=0; while($rows<=$num/8){ echo"<tr>"; while ($i < $num && $pictureCount <= 7) { $fname=mysql_result($result,$i,'filename'); $username=mysql_result($result,$i,'userID'); $store=mysql_result($result,$i,'store'); $date=mysql_result($result,$i,'date'); $altText=mysql_result($result,$i,'alt'); echo" <td> <img src='$url/contribute/upload/image.php?width=75&height=75&cropratio=1:1&image=$url/contribute/upload/$fname' alt='$altText' /> </td> "; $i++; $pictureCount++; } echo"</tr>"; $rows++; } Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/ Share on other sites More sharing options...
wildteen88 Posted June 15, 2011 Share Posted June 15, 2011 Have a look at the code examples in this post for producing multi column results Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/#findComment-1230091 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 You do something like this: $cols = 8; $i=0; echo '<table>'; while($row = mysql_fetch_assoc($result)) { if ($i % $cols == 0) echo '<tr>'; echo '<td><img src="' . $row['image'] . '" /></td>'; if ($i % $cols == echo '</tr>'; $i++; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/#findComment-1230093 Share on other sites More sharing options...
hdbrandon Posted June 16, 2011 Author Share Posted June 16, 2011 This is what I've got so far. $result=mysql_query($sql); $num=mysql_numrows($result); $i=0; for($rowNum=1; $rowNum<=$num/8;$rowNum++){ echo"<tr>"; for($colNum=1; $colNum<=8;$colNum++){ while ($i < $num){ $fname=mysql_result($result,$i,'filename'); $altText=mysql_result($result,$i,'alt'); echo" <td> <img src='$url/...alt='$altText' /> </td> "; $i++; } echo"</tr>"; } } However, the table row tags are being put at the end of code-and not with 8 column tags within them. Shouldnt this way work as well? Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/#findComment-1230622 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 You got like 4 loops going on there, which is unnecessary. Also, mysql_numrows is not a function. It is mysql_num_rows. Did you try my code? Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/#findComment-1230624 Share on other sites More sharing options...
hdbrandon Posted June 16, 2011 Author Share Posted June 16, 2011 Great, thanks. Works like a charm now. Link to comment https://forums.phpfreaks.com/topic/239447-building-a-table-from-loop/#findComment-1230631 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.