Clearing div inside a while loop

Hi ,

Im currently building an online shop , on the display products page i want to display 3 images across and then add a CSS/html clear. Ive tried counting the rows and then saying if the num_rows is = to say 3 put the clear in but its adding it after all images

[php]
if($wineType && $region ){ // check if both winetype & region are selected
$q = mysql_query(“SELECT * FROM wines WHERE wine_type = ‘$wineType’ AND region = ‘$region’”);

		while($row = mysql_fetch_assoc($q)){
		$id = $row['id'];
		$code = $row['product_code'];
		$name = $row['product_name'];
		$desc = $row['product_desc'];
		$wineType = $row['wine_type'];
		$wineRegion = $row['region'];
		$volume = $row['volume'];
		$price_1_6 = $row['price_1_6'];
		$price_1_6_b = $row['price_1_6_b'];
		$price_6_plus = $row['price_6_plus'];
		$price_6_plus = $row['price_6_plus_b'];
		$year = $row['vintage_year'];
		
	echo '<div>';
	
		echo "Product Code :$code"."<br />";
		echo "Product Name : $name"."<br />";
		echo "Product Description $desc"."<br />";
		echo "Wine Type : $wineType"."<br />";
		echo "Region :$wineRegion"."<br />";
		echo "Volume : $volume"."<br />";
		echo "Price 1-6 Bottles : $price_1_6"."<br />";
		echo "Price 1-6 bottles(bus) : $price_1_6_b"."<br />";
		echo "Price 6+ : $price_6_plus"."<br />";
		echo "Price 6 +(bus) : $price_6_plus"."<br />";
		echo "Vintage / Year : $year"."<br />";
		
		echo '<img src="../images/wine-images/'.$id.'.jpg"/>';
		
	echo '</div>';
	
		
	 }
	}

[/php]

The code above is my while loop , any help would be great thanks

I wasn’t really sure what you meant by using the css clear function. My guess is you are trying to go down a line? so i inserted a break statement, but you can change it to whatever you want

[php]
if($wineType && $region ){ // check if both winetype & region are selected
$q = mysql_query(“SELECT * FROM wines WHERE wine_type = ‘$wineType’ AND region = ‘$region’”);
$i = 0;
while($row = mysql_fetch_assoc($q)){
if($i % 3)
{
//add clear statement
echo “
”;
}
else
{
$id = $row[‘id’];
$code = $row[‘product_code’];
$name = $row[‘product_name’];
$desc = $row[‘product_desc’];
$wineType = $row[‘wine_type’];
$wineRegion = $row[‘region’];
$volume = $row[‘volume’];
$price_1_6 = $row[‘price_1_6’];
$price_1_6_b = $row[‘price_1_6_b’];
$price_6_plus = $row[‘price_6_plus’];
$price_6_plus_b = $row[‘price_6_plus_b’];
$year = $row[‘vintage_year’];

        echo '<div>';
        
            echo "Product Code :$code"."<br />";
            echo "Product Name : $name"."<br />";
            echo "Product Description $desc"."<br />";
            echo "Wine Type : $wineType"."<br />";
            echo "Region :$wineRegion"."<br />";
            echo "Volume : $volume"."<br />";
            echo "Price 1-6 Bottles : $price_1_6"."<br />";
            echo "Price 1-6 bottles(bus) : $price_1_6_b"."<br />";
            echo "Price 6+ : $price_6_plus"."<br />";
            echo "Price 6 +(bus) : $price_6_plus_b"."<br />";
            echo "Vintage / Year : $year"."<br />";
            
            echo '<img src="../images/wine-images/'.$id.'.jpg"/>';
            
        echo '</div>';
	
		}
	 }
	}

[/php]

Hi , thanks for your reply , but this hasnt worked for me , what i ment by a clear was using css to clear a floated element , so there will be 3 images across the page then i need a clear under the third image and then 3 more images then a clear again … and so on.

Any more help would be brillant

Thanks

could i see what elements are currently floating? because I don’t see anything in your code

Here is my second stab at your problem, I can’t help fix your problem with seeing what is wrong with it, so if you could supply a picture, or better yet, a link to your problem page that would help me help you better.

[php]

<?php if($wineType && $region ) { // check if both winetype & region are selected $q = mysql_query("SELECT * FROM wines WHERE wine_type = '$wineType' AND region = '$region'"); $i = 0; while($row = mysql_fetch_assoc($q)) { if($i % 3) { if($i == 0) { echo '
'; } else { echo '
'; echo "
"; echo '
'; } } else { $id = $row['id']; $code = $row['product_code']; $name = $row['product_name']; $desc = $row['product_desc']; $wineType = $row['wine_type']; $wineRegion = $row['region']; $volume = $row['volume']; $price_1_6 = $row['price_1_6']; $price_1_6_b = $row['price_1_6_b']; $price_6_plus = $row['price_6_plus']; $price_6_plus_b = $row['price_6_plus_b']; $year = $row['vintage_year']; echo "Product Code :$code"."
"; echo "Product Name : $name"."
"; echo "Product Description $desc"."
"; echo "Wine Type : $wineType"."
"; echo "Region :$wineRegion"."
"; echo "Volume : $volume"."
"; echo "Price 1-6 Bottles : $price_1_6"."
"; echo "Price 1-6 bottles(bus) : $price_1_6_b"."
"; echo "Price 6+ : $price_6_plus"."
"; echo "Price 6 +(bus) : $price_6_plus_b"."
"; echo "Vintage / Year : $year"."
"; echo ''; } } } ?>

[/php]

Hi thanks again for your reply , the floated element is the div that is inside the loop , i will add a link to the site shottly thanks ian

I would like to clear some confusion up, the

tag is just a tag so we can separate the page into sections, it does not have any other functions to it, unless you assign different attributes to it, like set it part of a class and add styles to the class. If you put all
tags to float left, which is highly unrecommended, you should read up more about html class and id attributes, the internet is your friend on this part, there are a lot of html tutorial sites out there and I highly recommend you read up a little more on the basics of html.

here is a good place to start: http://www.w3schools.com/tags/ref_standardattributes.asp
they don’t have everything but they give you enough of the basics to get you going in the right direction

Sorry if that sounded harsh in any way,

Albert

hi albert , i havent added anything to it and understand its not a “function” just thats the best way i could word it , thanlks for the reply thou

  • i havent added anything to the
    yet i just put it there for an example

Oh, my bad then. I was looking over the code I wrote for you again, I forgot to add the closing

tag for the last line, so you want to insert [php]echo “
”;[/php] after it exits the while loop

Well, first, everyone, you CAN load things into DIV’s. You can also hide them and show them as needed.

To do the hide/show, it is much easier to do in Javascript. You can do it in PHP if the page is being built by PHP. So, I am not sure what your code here does:
if($i == 0)
{
echo ‘

’;
}
else
{
echo ‘
’;
echo “
”;
echo ‘
’;
}
Depending on the $i, it either starts off a DIV or it ends one and starts another? I am sure there must be some code missing here… Also, you are using $i for some value. You set it to zero and then check if it is 1 or 3 in the first code posted. It never is set to anything else. It always stays at zero, so those compares are useless.

Perhaps you could explain your code you posted. Here is a link on how it is done in Javascript. This site has some nice samples that make DIV’s vanish nicely… Good luck…
http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/

Ive been given this code in another forum and it still doesnt seem to work , the problem i have is im displaying shop images 3 across the page , some have longer descriptions than others so i want to add a clear after the third time the loop goes around so that the images are all inline .

[php]
if($foodType){ // check if noithing is set
$q = mysql_query(“SELECT * FROM products WHERE type = ‘Food’ AND product_type = ‘$foodType’”);
$i = 0;
$num_rows = mysql_num_rows($q);

	// if there are no products display this message
	if($num_rows == 0){
		echo " There are currently no products listed";	
	}
		
		while($row = mysql_fetch_assoc($q)){
		
		
		
		   if($i >= 2) {
        
            //add clear statement
            echo '<div class="clear"></div>';
        }
       								
		
		$id = $row['id'];
		$code = $row['product_code'];
		$name = $row['product_name'];
		$desc = $row['product_desc'];
		$foodType = $row['product_type'];
		$size = $row['product_size'];
		$weight= $row['product_weight'];
		$price = $row['price'];
		$price_b = $row['price_b'];
		
	
		
echo '<div class="clearfix">';
	echo '<div class="span-8">';
			echo '<div class="heading">'.$name.'</div>';
			
			echo '<div class="clearfix">';
				echo ' <div class="span-5 bottle"><img src="http://www.vino-italiano.co.uk/image.php?width=190&height=300&image=/images/product-images/'.$id.'.jpg" /></div>';
				
				echo '<div class="span-3 last">'.$code.'</div>';
				echo '<div class="span-3 last">'.$foodType.'</div>';
				echo '<div class="span-3 last">'.$size.'</div>';
				echo '<div class="span-3 last">'.$weight.'</div>';

				echo '<div class="span-3 last">1-6 Bottles: &pound;'.$price.'</div>';
				echo '<div class="span-3 last">6+ Bottles: &pound;'.$price_b.'</div> ';
    		echo '</div>';
                    
    echo  '<div>Description: '.$desc.'</div>';
			$i++;
			
		  echo '<a href="cart.php?product='.$id.'&action=add">Add to cart</a>';
	echo '</div>';
	
echo '</div>';
	
		
	 }
	}

[/php]

Well, I guess I do not understand what you are attempting to do. You are “creating” a DIV that is cleared on the 3rd pass, but, that does nothing for the ones that are already displayed. Also, in this “clear” you are only assigning a class NAMED “clear”. What is in your CSS for that class? There must be more code you are not showing us. Can you put the code online and send a link for us to go to that will show us what is not working?

Sponsor our Newsletter | Privacy Policy | Terms of Service