Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 1, 2026.

Foreach loop output need help

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
NamecheapNamecheap
Watch

988

NoneTop Member
Impact
606
foreach ($samples AS $sample)

Right now it will output..

sample
sample
sample
sample
sample
sample



What I want is the output to be..

sample sample sample
sample sample sample
sample sample sample

Anybody know how I go about doing this?

A different query? perhaps.. Need help...

Thank you..
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I'm not totally sure what you are trying to achieve, you want a carriage return every three elements right ?
Then you would need an incremented counter in your loop along these lines (using modulus)
PHP:
$counter=1;
foreach ($samples AS $sample) {
...

	$counter++;

	if ( ($counter % 3) == 0) { // break
		echo "<br />";
	}
}
 
2
•••
Never mind
 
Last edited:
0
•••
thank you, ill try it when i get home.
 
0
•••
Try this. The widths decide how many show per row.


PHP:
<?php
$output = '<div style="width:330px;clear:both;overflow:hidden;">';
foreach ($samples AS $sample) {
	$output .= '
		<div style="width:100px;margin-left:10px;float:left;clear:none;">
			'.$sample.'
		</div>
	';
}
$output .= '</div>';
?>
 
Last edited:
2
•••
I'm not totally sure what you are trying to achieve, you want a carriage return every three elements right ?
Then you would need an incremented counter in your loop along these lines (using modulus)
PHP:
$counter=1;
foreach ($samples AS $sample) {
...

	$counter++;

	if ( ($counter % 3) == 0) { // break
		echo "<br />";
	}
}

best response so far, use auto increment
 
0
•••
How is that better? It is slower and mine offers the ability to have the number of columns you want without additional coding.
 
0
•••
Thank you all for the help, I decided to go with lampninjaman's idea.

the very simple code = { float:left; width: 33% }

Again.. Thank You.
 
1
•••
You are quite welcome bro.
 
0
•••
CatchedCatched

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back