473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Eliminate duplicated values in an array that is being populated from a query looping

7
Hi,

Apologies for the long explanation I'm just trying to be as clear as possible.

Basically, I have a query that looks for products matching the criteria selected by the user through check boxes. The query is in a loop that will loop as many times as the number of criteria selected by the user.

E.g, if the user selected 2 criteria (criteria_id 11 and criteria_id 46), The query will loop the first time looking for products meeting criteria 11, and next time looking for products meeting criteria 46. Every match is assigned to an array and then arrays are the printed.

The problem is that I want to print just the products that meet all the selected criterias. At the moment as long as there is at least one product meeting the first criteria (even if it doesn't meet the second), all products are printed.

How can I print only the ones that match all the criteria selected?

Thanks any help will be very much appreciated as I'm really frustrated by now!



Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7. $queriesWithMatchs=0; $zero=0; $rowMatches=array(); $id=array(); $arrayOfId=array();
  8.  
  9.  
  10. //check that the form has been submitted and that values have been assigned. Then asign this values to variables used in queries.
  11.  
  12. if (isset($_POST['submitte'])) {
  13.  
  14.  
  15.  
  16. //check that a criteria was selected otherwise dont proceed.Open main div to print within the main body.
  17. if (!empty($_POST['epeat_checkbox'])) {
  18.  
  19.  
  20.  
  21.  
  22. if (isset($_POST['org'])) {
  23.  
  24.         $org=$_POST['org'];
  25.  
  26.                         }
  27.                         else {//do something
  28.                         }
  29.  
  30.  
  31. if (isset($_POST['prodCrit'])) {
  32.  
  33.     is_array($prodCrit=$_POST['prodCrit']);
  34.                         echo $prodCrit[0].'<br/>';
  35.  
  36.                         }
  37.                         else {//do something
  38.                         }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  //count how many criterias were selected to make the querie loop just as many times   
  46. $criteriaSelected= $_POST['epeat_checkbox'];
  47.  
  48.       $numberOfCriteriaSelected = count($criteriaSelected);
  49.  
  50.  
  51.             for($i=0; $i < $numberOfCriteriaSelected; $i++)
  52.                 {
  53.                  $criteriaId=$criteriaSelected[$i] ;
  54.  
  55.                   echo('criteria id:'.$criteriaId . "<br/> ");
  56.  
  57.  
  58.  
  59.  
  60.         require_once ('../mysqli_connect.php');
  61.  
  62.  
  63.  
  64.  
  65.  $q = "SELECT product.model AS model, product.id_product AS id,brand.brand_name AS brand, product_type.type_name AS types FROM brand, product_type, product, $prodCrit[0]  WHERE product.id_product = $prodCrit[0].id_product AND $prodCrit[0].id_criteria=$criteriaId  AND product_type.id_type=product.id_type AND product.id_brand=brand.id_brand;";
  66.         $r =mysqli_query ($dbc, $q) or die(mysqli_error($dbc)); // Run the query.
  67.  
  68.  
  69.  
  70.         if ($r) { // If it ran OK, get the records and increment $rowsPerCriteria by one every time there at least one match per query, that is when the selected criteria(i) is matched.
  71.  
  72.  
  73.           $rowsPerCriteria=0; 
  74.                   while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
  75.  
  76.                 //add query results to this array each time it loops. 'model' is the alias given in the query "AS"  
  77.                 $rowsPerCriteria++;
  78.                 echo 'count id'. count($id).'<br/>';
  79.  
  80.                  $id[] = $row['id'];
  81.                  $rowMatches[] = $row['model'];
  82.                 $brandMatches[] = $row['brand'];
  83.                 $typeMatches[] = $row['types'];
  84.  
  85.  
  86.                 }//end of while
  87.                 echo'1 query';
  88.  
  89.                 while(list($key, $val)=each($id)){
  90.                 echo $val." ";
  91.                 }
  92.  
  93.                 if (in_array($row['id'],$id)){
  94.                 echo'in array'.$row['id'] .'<br/>';
  95.  
  96.                 }
  97.  
  98.                 else{
  99.                 echo' not in array'.$row['id'] .'<br/>';
  100.  
  101.                 }
  102.  
  103.         //Check that the query return at least a record and if it did, increment the $querywithmatches by one. 
  104.  
  105.         if ($rowsPerCriteria>$zero)
  106.           {
  107.                   $queriesWithMatchs+=1;
  108.           }
  109.  
  110.  
  111.  
  112.           mysqli_free_result ($r);
  113.     }//en of if $r
  114.      else{
  115.              echo' didnt run';
  116.          }
  117.  
  118.  
  119.  
  120.     }//en of for loop    
  121.  
  122.  
  123.  
  124. //Compare the numb of queries with matches to the numb of selected criteria, if it is the same start printing.
  125.         if($queriesWithMatchs==$numberOfCriteriaSelected&&$numberOfCriteriaSelected>0){
  126.  
  127.  
  128.         sort($id);
  129.  
  130.  
  131.  
  132.  
  133.         echo '<Table cellpadding="5">';
  134.              echo'<th> Type</th> <th> Brand</th><th> Model</th>';
  135.       $values=array_map(null,array_values($typeMatches), array_values($brandMatches),array_values($rowMatches),array_values($id));
  136.  
  137.             foreach($values  as $v)
  138.         {
  139.             list($v1, $v2, $v3, $v4)=$v;
  140.  
  141.  
  142.             echo'<tr><td>'. $v1.' </td> <td>'. $v2.'</td><td>'. $v3.'</td><td> '.$v4.'</td></tr>';
  143.  
  144.  
  145.         }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.          echo'</table>';
  152.          echo '</div>';
  153.         } 
  154.  
  155.         else{    
  156.         echo '<h4> Products matching all your selected criteria</h4>';
  157.         echo 'Sorry no product matches all you selected criteria';
  158.         echo '</div>';
  159.          }
  160.  
  161.  
  162.  
  163.     }
  164.     else{ 
  165.  
  166.     echo'<h4>No criteria was selected</h4>
  167.  
  168.     exit;
  169.     }
  170.  
  171.  
  172. }
  173.  
  174. ?>
Mar 22 '11 #1

✓ answered by Rabbit

In pseudocode, it'd be something like this
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM Table1 WHERE "
  2.  
  3. For Each criteria In criterion
  4.    strSQL = strSQL + criteria + " OR "
  5. End For
  6.  
  7. Remove last " OR " from strSQL
  8.  
  9. Execute strSQL
I assume it's an OR criteria from what I've heard. But for all I know, you mean to use an AND.

8 1501
Dormilich
8,658 Expert Mod 8TB
I’d say, construct an appropriate SQL query that contains all conditions to meet (i.e. if there are 2 conditions, then your WHERE clause needs to contain 2 conditions, etc.)
Mar 23 '11 #2
Yess
7
Thanks for the quick reply. The problem is that the values I will be looking for are coming from checkboxes and the user may select one to many boxes. So I may be wrong but I don't think that would solve my problem. Perhaps there is another way???
Mar 23 '11 #3
Rabbit
12,516 Expert Mod 8TB
There's no need to run a SQL query for each criteria. Combine the criterian into one query and then run that.
Mar 23 '11 #4
Yess
7
Forgive me if I'm misunderstanding what you said. How can I combine all the criteria into one query if I don't know which criteria and how many criterions the user will select? I'm a complete newbie and don't know how to do that.
Mar 23 '11 #5
Rabbit
12,516 Expert Mod 8TB
Let me ask you this, how do you know how many queries to run if you don't know how many criterion will be selected beforehand? It's the same thing. But instead of running a different query for each criteria, you are instead building a dynamic sql string. Appending the criteria for each one selected. And then running that at the end.
Mar 23 '11 #6
Yess
7
I think I know what you mean i just don't know how to do it. I'm a newbie to both php and sql. I would really appreciate it if you could provide me with an example. Thanks!
Mar 23 '11 #7
Rabbit
12,516 Expert Mod 8TB
In pseudocode, it'd be something like this
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM Table1 WHERE "
  2.  
  3. For Each criteria In criterion
  4.    strSQL = strSQL + criteria + " OR "
  5. End For
  6.  
  7. Remove last " OR " from strSQL
  8.  
  9. Execute strSQL
I assume it's an OR criteria from what I've heard. But for all I know, you mean to use an AND.
Mar 23 '11 #8
Yess
7
Thanks a lot, I'll give it a go and shall let you know the outcome.
Mar 23 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RBohannon | last post by:
I have a report with most fields populated by a query. However, some of the fields are variable in such a way that their values cannot be queried from a table. At present the values for these...
1
by: bdt513 | last post by:
I am trying to extract the values from a query using VBA. Specifically, I want to concatenate all the values of the "rosEmail" field from query "qselRosterEmailList" into one string (strEmails). I...
0
by: YuanYuan | last post by:
Thank you so much, guys. I really appreciate that. >-----Original Message----- >Hi, I am wondering if there is a nice way to elimate >duplicated values in an array, just like the "unique"...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
3
by: racquetballguy | last post by:
Hi I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query. SELECT TOP does not support parameters. Something like...
18
by: WilhelmAccess | last post by:
Hi, I am trying to automate the running of a SQL Query. I have a Table in Access 2003 that contains records with several fields, (member identifier, $ amount and months during the year they were...
1
by: bhaskar321 | last post by:
Hi all, i am new to access could any one tell me how can i eliminate duplicate records from the query Thanks, Bhaskar
0
VbaNewbee
by: VbaNewbee | last post by:
I have a form with a few filters. Once the user clicks "search button", the code first evaluates my filters, then shows the query results in a List Box" titled backschedule. I have a few text...
1
by: billypit | last post by:
Hi, In my project i have one table production.Now i have to make application in which i have to insert new data in table by fields of form made in access.I don't know how to use form field's values...
6
by: asp2 | last post by:
I have a cursor inside a program unit in Oracle Forms 6 and its supposed to get one row from this cursor .. but in some values (only some and others not) it gets the same row twice (duplicated) in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.