FlyingIsFun1217 Posted May 28, 2008 Share Posted May 28, 2008 Hey! Right now, I'm creating a listing of files in a certain directory as such: foreach(glob('*.jpg') as $picFileJPG) { echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$picFileJPG.'</a>'.'<br>'; } What I want to do though, is to run through, and echo out html tables so that the first ten results are each in a separate row in a table, and the next ten are in separate rows in a new table, etc., so that it goes ten down per column. I've tried doing the start of this, limiting it to one table with ten rows; heres the code I've got: echo '<center>'; echo '<table>'; foreach(glob('*.jpg') as $picFileJPG) { $tableRowNumber = 1; if($tableRowNumber <= 10) { echo '<tr>'; echo '<td>'; echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$picFileJPG.'</a>'.'<br>'; echo '</td>'; echo '</tr>'; $tableRowNumber++; } } echo '</table>'; echo '</center>'; Now, it *should* only go through ten and print out, no? But it prints out all results. Even if I got that right, I still have the issue of starting a new table for the next ten results. Can anyone help me on this? Possibly with some psuedo-code? Thanks so much! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/ Share on other sites More sharing options...
sasa Posted May 28, 2008 Share Posted May 28, 2008 move line $tableRowNumber = 1; before foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-551841 Share on other sites More sharing options...
Gighalen Posted May 28, 2008 Share Posted May 28, 2008 Hope this helps. $col = 0; echo "<table>"; $q = mysql_query("SELECT * FROM chat"); while($data = mysql_fetch_array($q)){ if($col === 4){ echo "<tr>"; } echo "<td>".$data['id']."$col</td>"; $col++; if($col === 4){ echo "</tr>"; $col = 0; } } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-551843 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 28, 2008 Author Share Posted May 28, 2008 Cool, thanks for the tip on the variable location, I didn't think of that strangely enough. And definitely thanks on the example, I'll look it over in a little while (right now, I'm working on an upload form). Thanks again! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-551855 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 3, 2008 Author Share Posted June 3, 2008 Two posts up, it was suggested to do all work in one table. Thing is, I want to construct as many tables as needed (with each holding one ten rows and one column), so that the listing goes down alphabetically, like the following: tbl1 tbl2 abc klm bcd lmn cde mno def nop efg opq fgh pqr ghi qrs hij rst ijk stu jkl tuv So every ten entries, I need to start a new table (ideally), and give each result a new <tr><td> entry. Any examples of this somewhere? Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556774 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 3, 2008 Author Share Posted June 3, 2008 Alright, I'm creating some psuedo-code, here's what I have so far: echo '<table>'; echo '<tr>'; $rowNumber = 1; foreach(image as $imgVar) { if($rowNumber <= 10) { if($rowNumber == 1) { echo '<td>'; echo '<table>'; } // Insert data, up until the tenth row echo '<tr><td>'.$imgVar.'</td></tr>'; if($rowNumber == 10) { //Alread inserted the data to the table, now we just need to end the table echo '</table>'; echo '</td>'; $rowNumber == 1; } } } echo '</tr>'; echo '</table>'; Do you see anything wrong with this? I would expect it to create one nested table containing tables like the ones that I showed above, each being in their own column of the master table. FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556797 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 3, 2008 Author Share Posted June 3, 2008 Alright, I've got it mostly worked out. Here is my current code: $tableRowNumber = 1; echo '<center>'; //========================================================================================================================================================================// echo '<table border="1">'; echo '<tr>'; foreach(glob('*.jpg') as $picFileJPG) { $simplePicName = substr_replace($picFileJPG,'',-4); if($tableRowNumber == 1) { echo '<td>'; echo '<table border="1">'; } echo '<tr>'; echo '<td>'; echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$simplePicName.'</a>'; echo '</td>'; echo '</tr>'; if($tableRowNumber == 10) { echo '</table>'; echo '</td>'; $tableRowNumber = 1; } if($tableRowNumber != 10) { $tableRowNumber++; } } echo '</table>'; echo '</td>'; echo '</tr>'; echo '</table>'; //========================================================================================================================================================================// echo '</center>'; My only problem is that at the start of a new nested table, I don't get the "<td><table>" printout as I should. I looked through, and it seems I should, since I am setting $tableRowNumber back to '1' when it reaches ten. See anything that I'm not? FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556838 Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 Change: if($tableRowNumber == 10) { echo '</table>'; echo '</td>'; $tableRowNumber = 1; } if($tableRowNumber != 10) { $tableRowNumber++; } To: if($tableRowNumber == 10) { echo '</table>'; echo '</td>'; $tableRowNumber = 1; } else { $tableRowNumber++; } Because you're setting $tableRowNumber to 1 right before it, so obviously it won't equal 10, and it'll increase past one. Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556848 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 3, 2008 Author Share Posted June 3, 2008 Geeze, your good! Thanks so much, I really do appreciate your time! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556852 Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 No problem. Please click "Solved" when you get a chance. Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556856 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 3, 2008 Author Share Posted June 3, 2008 Alright, it is all working. Solved FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107651-solved-table-output-help/#findComment-556859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.