Jump to content

Filter MYSQL Result


amwd07

Recommended Posts

I am trying to filter the results query before retuning the function

 

  
<?php    
$queryResults = '
        SELECT DISTINCT SQL_CALC_FOUND_ROWS p.*, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
        t.`rate`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name
        FROM `'._DB_PREFIX_.'category_product` cp
        LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
        LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON p.`id_product` = pa.`id_product`
        LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`	
        LEFT JOIN `'._DB_PREFIX_.'attribute` a ON (a.`id_attribute` = pac.`id_attribute`)
        LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')		
        LEFT OUTER JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
        LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
        LEFT JOIN `'._DB_PREFIX_.'tax` t ON (p.`id_tax` = t.`id_tax`)
        LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
	WHERE cp.`id_category` = '.intval($currentCategory).' AND p.`active` = 1 	
	AND '.(($range == false && $selection && sizeof($selection)) ? ' p.`id_product` IN ('.implode(',', array_map('intval', $selection)).')' : $whereBy).' 
	AND a.`id_attribute` IN (49,97,110) // this does not filter
	'.($orderBy ? 'ORDER BY  '.$orderBy : '').($orderWay ? ' '.$orderWay : '').'
        LIMIT '.intval(($pageNumber - 1) * $pageSize).','.intval($pageSize);
      $result = Db::getInstance()->ExecuteS($queryResults) or die(mysql_error());
      $total  = Db::getInstance()->getValue('SELECT FOUND_ROWS()');

$result = Db::getInstance()->ExecuteS($queryResults) or die(mysql_error());
?>

 

 

this would return 3 results which is correct but when I try to add a second array map into the above query this still returns 3 results, there should only be one result, so I have come to the conclusion this can not be done within SQL so I would like to filter my result from the DB query

 

$result = /// $result['id_attribute'] filter array('49','97','110','95');  =  1 product

 

then return $result;  (Previously used SQL)

Link to comment
Share on other sites

ok I now feel alone with this issue :(

 

although defferently moving forward, have now found the right solution to my problem but need assistance implementing. The problem is I can not loop through the product ID's, this is because it would seem I can only use 1 foreach loop for my resultRtn array, I need to be able to loop through the product ID's so I can set these as keys

for example  array(pID => attID)

 




//	$attArray = array();
//	foreach($newExp as $attID)
//	foreach($selection as $pID)

	// Method 1
	$attArray = array();
	foreach($newExp as $attID)
	{
	   $resultRtn = array(
	      id_product => 'return ( $att["id_product"] == "'.$selection.'" );',
	      id_attribute => 'return ( $att["id_attribute"] == "'.$attID.'" );',
   		   );
       $attUsr = create_function('$att', $resultRtn);
	   $filtered_results = array_filter($selAtt, $attUsr);
	   $attArray[] = $attID;
	   $count = count($filtered_results);
           echo "Attribute Found ".$attID.": (".$count.")<br \>";
	}
	print_r($attArray);

Link to comment
Share on other sites

ok I now feel alone with this issue :(

 

although defferently moving forward, have now found the right solution to my problem but need assistance implementing. The problem is I can not loop through the product ID's, this is because it would seem I can only use 1 foreach loop for my resultRtn array, I need to be able to loop through the product ID's so I can set these as keys

for example  array(pID => attID)

 

<?php
       //    $selAtt = FilterSearch::getProductsAttributesID($expr);
       //    example output of this array

       //    [0] => Array ( [id_product] => 31 [id_attribute] => 39 ) 
       //    [1] => Array ( [id_product] => 31 [id_attribute] => 48 ) 
       //    [2] => Array ( [id_product] => 31 [id_attribute] => 63 ) 
       //    [3] => Array ( [id_product] => 31 [id_attribute] => 96 )

       //    $attArray = array();
       //    foreach($newExp as $attID)
       //    foreach($selection as $pID)

	// Method 1
	$attArray = array();
	foreach($newExp as $attID)
	{
	   $resultRtn = array(
	      id_product => 'return ( $att["id_product"] == "'.$selection.'" );', 
                      // Notice: Use of undefined constant id_product - assumed 'id_product'

	      id_attribute => 'return ( $att["id_attribute"] == "'.$attID.'" );',
                      // This one works fine
   		   );
       $attUsr = create_function('$att', $resultRtn);
	   $filtered_results = array_filter($selAtt, $attUsr);
	   $attArray[] = $attID;
	   $count = count($filtered_results);
           echo "Attribute Found ".$attID.": (".$count.")<br \>";
	}
	print_r($attArray);
?>

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.