Jump to content

php foreach loop into a table


jarvis

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/193081-php-foreach-loop-into-a-table/
Share on other sites

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]

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; ?>

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>

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

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!

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!

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.