Jump to content

Displaying matching results using PHP


dont_be_hasty

Recommended Posts

Hi im working on a project using php and mysql and could do with some help.

 

In the project the user enters two different id's and it displays the appropriate terms from the database, however i want to only displays terms that match from each id.

Here is the code i have so far.

 

$id1 = mysql_real_escape_string($_POST['id1']);

$id2 = mysql_real_escape_string($_POST['id2']);

 

$query1="SELECT term FROM table WHERE id = '$id1'";

$query2="SELECT term FROM table WHERE id = '$id2'";

$result1=mysql_query($query1) or die(mysql_error());

$result2=mysql_query($query2) or die(mysql_error());

$row1 = mysql_fetch_array($result1);

$row2 = mysql_fetch_array($result2);

 

echo $row1['term'];

echo $row2['term'];

 

When i display the results it displays the following

 

Row 1:

term1

term2

term3

term4

 

Row2:

term2

term4

term6

term8

 

What i want to do is only display the terms that match is each row (Eg, i only want to display term2 and term4 as they are in both rows). Is there any way of doing this using php?

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/144612-displaying-matching-results-using-php/
Share on other sites

Yeah its a hard one to explain.

 

I have the following tables:

 

term(id, term)

product(id, product)

join_table(term_id, product_id)

 

And the follwing code:

 

$product1 = mysql_real_escape_string($_POST['product1']);
$product2 = mysql_real_escape_string($_POST['product2']);

$query1="SELECT * FROM term INNER JOIN join_table ON join_table.term_id=term.id INNER JOIN product ON join_table.product_id=product.id WHERE product = '$product1'";

$query2="SELECT * FROM term INNER JOIN join_table ON join_table.term_id=term.id INNER JOIN product ON join_table.product_id=product.id WHERE product = '$product2'";
            
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
            
$row1 = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);

while($row1 = mysql_fetch_array($result1)) {
echo $row1['term'];
}

while($row2 = mysql_fetch_array($result2)) {
echo $row2['term'];
} 

 

The user inputs 2 products, the code basically get the id of this product then uses the join table to get the terms id/ids that match/link to this product id, then gets the terms that match the term id/ids.

 

Whenever you display the results from row1 and row2, you usually get serveral terms being displayed (i think a product_id may have serveral term_ids).

 

An example of this is:

Row1 will display - term1, term2, term3, term4

Row2 will display - term2, term4, term6

 

In both of the rows term2 and term4 are display. How do i get it so that i only display the terms that match in each row (ie, only display term2 and term4).

 

This is for a similarity project, so i need to display the similar results. If this cant be done is there away out counting how many terms displayed are the same. Like in the example above there is 2 similaritys, they are term2 and term4. So even if i can some how display the number of similar/match terms from each query/row would be great.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.