www.webdeveloper.com
Results 1 to 2 of 2

Thread: Query Results in Extra Rows

  1. #1
    Join Date
    Dec 2006
    Posts
    115

    Query Results in Extra Rows

    Hey everyone,
    I'm running this query and displaying the results (products) on a site. The format of the results is great and is working great except there are multiple extra lines being generated and adding a lot of extra white space.

    Code:
    	$query ="SELECT * FROM PRODUCTS where ProdCategory = \"" . $category . "\"";
    
    	$result = mysql_query($query);
    	$num_results = mysql_num_rows($result);
    
    	//echo "<div class=\"padding\">";
    	
    for ($i = 0; $i < $num_results; $i++){
    	
    	
    	echo '<div id="parentdiv">';
    while($row = mysql_fetch_array($result)){
    	
        echo '<div style="width:220px;float:left;">';
            echo '<div class="itemname"> <a href="products.php?id=' . $row['ProductID'] . '"> ' .$row['ProdName'].'</a></div>';
            echo '<div class="brand">Maker: ' . $row['ProdBrand'] . '</div>';
            echo '<div class="recentproduct"><img src="' . $row['ProdImage'] . '" alt=' . $row['ProdName'] . '"></div>';
            echo '<div class="price">$' . $row['ProdPrice'] . '"</div>';
            echo '<div class="addtocart"><a href="cart2.php?action=add_item&id=' . $row['ProductID'] . '&qty=1">Add to Cart</a></div>';
            
       echo '</div>';
         }
         echo '</div>';
         }
     	
    }
    	mysql_close();
    		?>
    With using this code, if the num_results = 14 there would be the 14 products displayed in 3 columns, however many rows necessary but followed by whitespace that is seen as

    <div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div><div id="parentdiv"></div>
    </div>

    in the source code. This is 13 extra divs. There is 1 less extra <div> element than #of results for every set.

    Anyone have any idea what is causing this?
    Thanks in advance,
    NickG

  2. #2
    Join Date
    Apr 2007
    Posts
    1,664
    You're looping the while() loop inside the for() loop which are both looping $num_results times.
    As you're not resetting the SQL pointer each iteration of the for() loop the resource is only used the first time. If you reset it with mysql_data_seek() you'd see the results shown time and time again instead of just the empty divs.

    Loops inside loops are generally a bad idea and should be avoided whenever possible. As you result set grows, looping a dynamic number inside the same dynamic number will result in exponentially slower queries. You need to re think what it ts you're trying to accomplish.
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles