jarvis Posted February 23, 2010 Share Posted February 23, 2010 Hi All, Am sure this is something straight forward, I've the following code which lists images from a db table: <?php foreach ($clientsRecords as $record): ?> <?php foreach ($record['client_logo'] as $upload): ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $record['client_name'] ?>" /> <?php endforeach; ?> <?php endforeach; ?> I'd like to convert this into a table with say 5 images per row. Is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/ Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 This was just quick but try this [color=rgb(0, 119, 0)] <?[/color][color=rgb(0, 0, 187)]php $i=0; [/color][color=rgb(0, 119, 0)]foreach ([/color][color=rgb(0, 0, 187)]$clientsRecords [/color][color=rgb(0, 119, 0)]as [/color][color=rgb(0, 0, 187)]$record[/color][color=rgb(0, 119, 0)]): [/color][color=rgb(0, 0, 187)]?> <?php [/color][color=rgb(0, 119, 0)]foreach ([/color][color=rgb(0, 0, 187)]$record[/color][color=rgb(0, 119, 0)][[/color][color=rgb(221, 0, 0)]'client_logo'[/color][color=rgb(0, 119, 0)]] as [/color][color=rgb(0, 0, 187)]$upload[/color][color=rgb(0, 119, 0)]): //check if $i is a multiple of 5 $float = ([/color][color=rgb(0, 119, 0)]$i % 5) !== 0[/color][color=rgb(0, 119, 0)]) ? ' style="float:left;" '; : ''; [/color][color=rgb(0, 0, 187)]?> [/color][color=rgb(0, 0, 0)]<img src="[/color][color=rgb(0, 0, 187)]<?php [/color][color=rgb(0, 119, 0)]echo [/color][color=rgb(0, 0, 187)]$upload[/color][color=rgb(0, 119, 0)][[/color][color=rgb(221, 0, 0)]'thumbUrlPath'[/color][color=rgb(0, 119, 0)]] [/color][color=rgb(0, 0, 187)]?>[/color][color=rgb(0, 0, 0)]" width="[/color][color=rgb(0, 0, 187)]<?php [/color][color=rgb(0, 119, 0)]echo [/color][color=rgb(0, 0, 187)]$upload[/color][color=rgb(0, 119, 0)][[/color][color=rgb(221, 0, 0)]'thumbWidth'[/color][color=rgb(0, 119, 0)]] [/color][color=rgb(0, 0, 187)]?>[/color][color=rgb(0, 0, 0)]" <?php echo $float; ?> height="[/color][color=rgb(0, 0, 187)]<?php [/color][color=rgb(0, 119, 0)]echo [/color][color=rgb(0, 0, 187)]$upload[/color][color=rgb(0, 119, 0)][[/color][color=rgb(221, 0, 0)]'thumbHeight'[/color][color=rgb(0, 119, 0)]] [/color][color=rgb(0, 0, 187)]?>[/color][color=rgb(0, 0, 0)]" alt="[/color][color=rgb(0, 0, 187)]<?php [/color][color=rgb(0, 119, 0)]echo [/color][color=rgb(0, 0, 187)]$record[/color][color=rgb(0, 119, 0)][[/color][color=rgb(221, 0, 0)]'client_name'[/color][color=rgb(0, 119, 0)]] [/color][color=rgb(0, 0, 187)]?>[/color][color=rgb(0, 0, 0)]" /> [/color][color=rgb(0, 0, 187)]<?php ++$i; [/color][color=rgb(0, 119, 0)]endforeach; [/color][color=rgb(0, 0, 187)]?> <?php [/color][color=rgb(0, 119, 0)]endforeach; [/color][color=rgb(0, 0, 187)]?> [/color] Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016826 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 Forum murdered it :s I'll try again below. <?php $i = 0; ?> <?php foreach ($clientsRecords as $record):?> <?php foreach ($record['client_logo'] as $upload): $float = ($i % 5) !== 0) ? ' style="float:left;" '; : ''; ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" <?php echo $float; ?> height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $record['client_name'] ?>" /> <?php endforeach; ?> <?php endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016830 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Thanks developerdave. I've already got it working with CSS but really need it working with a table (dont ask!) Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016839 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 Try this its rough round the edges but you can mess with it. <table width="800"> <tr> <td> <?php $i = 0; ?> <?php foreach ($clientsRecords as $record):?> <?php foreach ($record['client_logo'] as $upload): $float = ($i % 5) !== 0) ? '</td><td>'; : '</td></tr><tr><td>'; ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $record['client_name'] ?>" /> <?php echo $float; ?> <?php endforeach; ?> <?php endforeach; ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016846 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Thanks developerdave. That gives the error: Parse error: syntax error, unexpected ')' in which is line $float = ($i % 5) !== 0) ? '</td><td>'; : '</td></tr><tr><td>'; Should I remove the bracket from 0) or add another in front of ($i%5? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016850 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 Oops sorry about that, I'm at work so I'm just doing my bit (sneakily) Add one before the ($i % 5 So it reads $float = (($i % 5) !== 0) ? '</td><td>'; : '</td></tr><tr><td>'; Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016853 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Thought so, that however leads to: Parse error: syntax error, unexpected ';' Sorry to bother you (especially at work!) Thanks though!! Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016856 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 NOTE: That error refers to the same line - thanks. Changing the line to: $float = (($i % 5) !== 0) ? '</td><td>' : '</td></tr><tr><td>'; Displays the page without errors but then the images are just in a list down the page rather than a table. Sorry again! Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016857 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 No worries I'm going mad here this is my outlet! lol Sorry I left one in after the </td><td> fixed now $float = (($i % 5) !== 0) ? '</td><td>' : '</td></tr><tr><td>'; Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016861 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Thanks again developerdave. Sorry I'm sending you mad! I posted the same amend to the code as you. But as I mentioned above, it just lists the images down the page rather than in rows. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016866 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 Could you post the output HTML? I may take about an hour or so to reply to this as I'm leaving the office soon Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016872 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 p.s its not you driving me mad lol. This forum is keeping me sane. Its work driving me crackers haha Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016873 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Lol! Phew, thought it was me. The html it putputs is: <table class="client_category" width="600" align="center" border="1" cellpadding="0" cellspacing="0"> <tr> <td> <img src="/cmsAdmin/uploads/thumb/27_2.jpg" width="100" height="100" alt="AP Racing" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/68_2.jpg" width="100" height="100" alt="Autotorq" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/Axeon.jpg" width="100" height="100" alt="Axeon" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/Axtec.jpg" width="100" height="86" alt="Axtec" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/Bosal_001.jpg" width="100" height="24" alt="Bosal" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/bosch.jpg" width="100" height="100" alt="Bosch" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/capoco.jpg" width="100" height="100" alt="Capoco Design" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/7_2.jpg" width="100" height="100" alt="Chevrolet" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/chevron.jpg" width="100" height="100" alt="Chevron" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/38_2.jpg" width="100" height="100" alt="Connaught" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/37_2.jpg" width="100" height="100" alt="Continental Tyres" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/corus.jpg" width="100" height="100" alt="Corus" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/30_2.jpg" width="100" height="100" alt="Cummins" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/daftrucks_001.jpg" width="100" height="100" alt="DAF Trucks" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/56_2.jpg" width="100" height="100" alt="Ferrari" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/greenchem.jpg" width="100" height="100" alt="GreenChem" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/leyland.jpg" width="100" height="100" alt="Leyland Trucks" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/28_2.jpg" width="100" height="100" alt="Meguiar's" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/51_2.jpg" width="100" height="100" alt="Micheldever" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/Multipart_Colour.jpg" width="100" height="27" alt="Multipart" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/network-q_001.jpg" width="100" height="26" alt="Network Q" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/16_2.jpg" width="100" height="100" alt="Optare" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/65_2.jpg" width="100" height="100" alt="Revolve" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/Schrader.gif" width="100" height="39" alt="Schrader" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/stadco.jpg" width="100" height="100" alt="Stadco" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/58_2.jpg" width="100" height="100" alt="TomTom WORK" /> </td></tr><tr><td> <img src="/cmsAdmin/uploads/thumb/53_2.jpg" width="100" height="100" alt="Vauxhall Commercial Vehicles" /> </td></tr><tr><td> </td> </tr> </table> Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016878 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 try this I wasn't incrementing the $i var. My bad sorry mate <table width="800"> <tr> <td> <?php $i = 0; ?> <?php foreach ($clientsRecords as $record):?> <?php foreach ($record['client_logo'] as $upload): $float = ($i % 5) !== 0) ? '</td><td>'; : '</td></tr><tr><td>'; ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $record['client_name'] ?>" /> <?php ++$i;//my bad forgot to increment this! echo $float; ?> <?php endforeach; ?> <?php endforeach; ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016880 Share on other sites More sharing options...
jarvis Posted February 23, 2010 Author Share Posted February 23, 2010 Don't be sorry, I'm just grateful for the help! That's cerainly done the job nicely, thanks! One tiny question, why does the very first image sit on a line of it's own, then it starts the whole rows? Sorry! & thanks Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016884 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 Ahhh set the $i to 1 (where it says $i = 0;) and play with that its a number thing. The code basically says if the current value of $i is a multiple of 5 (which 0 is by default) then start a new row. So I'd play with the $i = 0; and if you need to the number 5 may need to be changed. But I doubt it Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016891 Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 When you solve it mind hitting the solved button too? its nicer to the forum peeps Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1016896 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 developerdave, you the man! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1017283 Share on other sites More sharing options...
developerdave Posted February 24, 2010 Share Posted February 24, 2010 Anytime its what I do Quote Link to comment https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/#findComment-1017289 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.