Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compare and find similar strings in 2 fields and insert into MYSQL

Posted on 2009-05-19
7
Medium Priority
?
422 Views
Last Modified: 2013-12-12
I need to compare 2 items find the similar string and insert that into a row in MYSQL.

Here is an example.
SKU = abcdx
KEY= 123abcdy
The common characters in this example would be value= abcd

I want to insert into MYSQL SKU, KEY and then the value which are the common characters.
$p="";
$i=0;
do
{
  if ($i) $p = "@".$i;
  if ($record["CLASS/COLORGROUP/PRODUCT/KEY".$p])
  {
    $key = $record["CLASS/COLORGROUP/PRODUCT/KEY".$p];
    $color_code = $record["CLASS/COLORGROUP/PRODUCT/COLOR_CODE".$p];
    $color_code_description = $record["CLASS/COLORGROUP/PRODUCT/COLOR_DESCRIPTION".$p];
    $sql_ModelGroups= "insert into ModelGroups set
			
			
		                          SKU= '".mysql_real_escape_string( $record["CLASS/SKU"])."',
					`key`= '".mysql_real_escape_string($key )."',
					color_code='".mysql_real_escape_string($color_code)."',
					color_code_description='".mysql_real_escape_string($color_code_description)."'
					";
				   mysql_query($sql_ModelGroups) or die(mysql_error());
				  
	
  }$i++;
} while($record["CLASS/COLORGROUP/PRODUCT/KEY".$p]);

Open in new window

0
Comment
Question by:danjen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24426754
See if this is what you had in mind.  Best regards, ~Ray
<?php // RAY_longest_common_substring.php
error_reporting(E_ALL); // THROWS A NOTICE IN THE FUNCTION
 
 
// TEST DATA FROM THE OP
$SKU = "abcdx";
$KEY = "123abcdy";
 
// FIND LONGEST COMMON STRING
$lcs = strlcs($KEY, $SKU);
var_dump($lcs);
 
// CREATE A QUERY, ETC...
$sql = "INSERT INTO ModelGroups SET lcs = \"$lcs\" WHERE sku = \"$SKU\"";
 
 
 
 
// FUNCTION FROM HERE: http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#PHP
function strlcs($str1, $str2)
{
   $m = strlen($str1);
   $n = strlen($str2);
   $L = array();
   $z = 0;
   $ret = "";
 
   for($i=0; $i<$m; $i++){
      $L[$i] = array();
      for($j=0; $j<$n; $j++){
         $L[$i][$j] = 0;
      }
   }
 
   for($i=0; $i<$m; $i++){
      for($j=0; $j<$n; $j++){
         if( $str1[$i] == $str2[$j] ){
            $L[$i][$j] = $L[$i-1][$j-1] + 1;
            if( $L[$i][$j] > $z ){
               $z = $L[$i][$j];
               $ret = "";
            }
            if( $L[$i][$j] == $z )
               $ret .= substr($str1, $i-$z+1, $z);
         }
      }
   }
   return $ret;
}

Open in new window

0
 

Author Comment

by:danjen
ID: 24427142
I get the following error.

Fatal error: Call to undefined function strlcs() in xml.php on line 119
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24427209
That function is defined at line 20 in the code sample (tested and working) I just posted.  Please post the code that you used to get the error message.
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:danjen
ID: 24427217
ok - maybe I don't understand. I thought the code at the bottom was just for reference. Which code listed should I use in the program?
0
 

Author Comment

by:danjen
ID: 24428329
I figured out the issue. Thank you so much for your help and for replying so quickly!!
0
 

Author Closing Comment

by:danjen
ID: 31583238
Thank you so much for your help!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24431558
Thanks for the points - it's a great question! ~Ray
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question