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
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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.