Understanding Loop in PHP / mysql

Please help me understand how to append the results into the following variable $getAllCategory as dinstinct items when going through thise loop:

$CategoriesArray = array(“Womens”,“Mens”,“Childrens”);
$CategoriesCount = count($CategoriesArray);

$sqlAllCategory = “”;
$getAllCategory = “”;
while($k < $CategoriesCount)
{
$sqlAllCategory = “SELECT DISTINCT mydb FROM table1 WHERE (category1 = '”.$CategoriesArray[$k]."’ or category2 = ‘".$CategoriesArray[$k]."’ or category3 = ‘".$CategoriesArray[$k]."’ or category4 = ‘".$CategoriesArray[$k]."’) ORDER BY shirt;";
$k++;
}

$getAllCategory = @mysql_query($sqlAllCategory) or die(mysql_error());
$num3 = mysql_num_rows($getAllCategory);

if(mysql_num_rows($getAllCategory) > 0)
{
$h=0;
while ($row = mysql_fetch_array($getAllCategory))
{
$h++;

	  	  echo '"',$row['shirt']; 
	       if ($h<$num3) {  echo '",'; }
	       else{  echo '"';  }
	   
	   }

}

//I appreciate the help very much

This loop in your code make no sense, because on each iteration you are just re-assigning the same variable: $sqlAllCategory (only last iteration will take effect):
[php]while($k < $CategoriesCount)
{
$sqlAllCategory = “SELECT DISTINCT mydb FROM table1 WHERE (category1 = '”.$CategoriesArray[$k]."’ or category2 = ‘".$CategoriesArray[$k]."’ or category3 = ‘".$CategoriesArray[$k]."’ or category4 = ‘".$CategoriesArray[$k]."’) ORDER BY shirt;";
$k++;
}[/php]

If I understand, you have category1 - category4 fields in your table? How about this query:
[php]$CategoriesArray = array(“Womens”,“Mens”,“Childrens”);

$s = implode("’,’",$CategoriesArray);
$getAllCategory = mysql_query(“SELECT DISTINCT shirt FROM table1 WHERE category1 IN (’”.$s."’) OR category2 IN (’".$s."’) OR category3 IN (’".$s."’) OR category4 IN (’".$s."’) ORDER BY shirt");
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service