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

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.