Jump to content

Changing Div Class In While Loop!?


jigsawsoul

Recommended Posts

Work's perfect and displays all the products, but i need the div class, productDetailsPH to change to productDetailsPH-1 on every other result. to end up with this result,

 

grey background

white background

grey background

white background

etc...

 

I'm quite new to php and i'm unsure how i would go about creating something like this, any help guys.. ?

 

$result = "SELECT * FROM products";

$result = mysql_query ($result) or die (mysql_error());
while($row = mysql_fetch_assoc($result)) 
{	
	$products .= '	
		<div class="postHoplder"> 
    			<div class="productName">'.$row['title'].'</div> 
    			<div class="productDetailsPH"> 
      				<div class="fltlft"><img src="_img/products/'.$row['image1'].'" alt="" width="122" height="81" /></div> 
      				<div class="PicDetailsPH"> 
        				<p>'.substr($row['description'], 0, 230).'... <a href="product.php?id='.$row['id'].'">read more</a></p> 
      				</div> 
      				<div class="payholderPH"> 
      					<a href="#">Add to Cart</a> 
        				<p>£ '.$row['price'].'</p> 
      				</div> 
    			</div>
		</div>
	';		
 }

:confused:

 

Link to comment
https://forums.phpfreaks.com/topic/196111-changing-div-class-in-while-loop/
Share on other sites

its very simple, add the below code definition above while loop

 

$i=0;

and add this code below the 'while {' code

 

if($i%2)
   $divclass = 'productDetailsPH';
else
   $divclass = 'productDetailsPH-1';

 

and replace this line <div class="productDetailsPH"> to

 

<div class="'.$divclass.'">

 

at the end of the while loop

 

$i++;

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.