Ive got a while loop that searches through a database finding certain things. Now ive gotten it to actually find the thing its looking for, however it keeps looking and lists the same item 6 times before stopping.

How do i get it to stop after its found it the first time, so that it only lists each item once?

The code is as follows:

//get information on the specific category we want
$query = "SELECT * FROM products WHERE product_cat='$prodcat'";

$results = mysql_query($query)
or die(mysql_error());
//if(mysql_num_rows($results)>0){//there are results returned

while ($row = mysql_fetch_array($results)) {
extract($row);
echo "<p>$product_title";
echo "<br> $product_price";
}
}

    I imagine you can set a flag.

    use php code for easier..

    //get information on the specific category we want
    $query = "SELECT * FROM products WHERE product_cat='$prodcat'";
    
    $results = mysql_query($query)
    or die(mysql_error());
    //if(mysql_num_rows($results)>0){//there are results returned
    
    while ($row = mysql_fetch_array($results)) {
    extract($row);
    echo "<p>$product_title";
    echo "<br> $product_price";
    }
    }
    

      what might be happening is that it found it on the 6th row/column and that's the number of times you are looping. then it's gonna print it out six times. so the higher the number the bigger the results of the same thing you want you are going to get

        //get information on the specific category we want
        $query = "SELECT * FROM products WHERE product_cat='$prodcat'";
        
        $results = mysql_query($query)
        or die(mysql_error());
        //if(mysql_num_rows($results)>0){//there are results returned
        $count = 0;
        while ($row = mysql_fetch_array($results)) {
        	if ($count < 1) {
        		extract($row);
        		echo "<p>$product_title";
        		echo "<br> $product_price";
        		$count++;
        	}
        }
        }// Where'd this one come from? 
        

        You mean like this?

          Well not quite... all that does is list the SAME item 6 times... rather than the other items of the same category. There 3 other items of the same kind in the database and i need it to list them all but only once.

            Once you've found all the data you want while in a while loop, you can [man]break[/man] out of it.

              MrPotatoes wrote:

              what might be happening is that it found it on the 6th row/column and that's the number of times you are looping. then it's gonna print it out six times. so the higher the number the bigger the results of the same thing you want you are going to get

              So what do you suggest?

              Ive basicly got it to find the right items... it either lists them all but six times e.g.

              1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3

              or just lists the first one that matches the criteria six timese.g.

              1,1,1,1,1,1

              When all i want it to do is :

              1,2,3

                Write a Reply...