I tried to make a coincidence in a combobox with a list of categories with the first similar word that find on a register, for example:
Input a quote: "the sun is great and nobody can see it directly"
explode each word in an array "the", "sun", etc...
find the word in a category with a similar name: category "great"
Until that it's ok, but the category has an ID, so... how can I make that coincidence with the ID because it finds the word, but always saves with the first category id.
Here's my code: (EDITED by @Mike)
<?php
$rows = mysql_num_rows($RecordsetCategory);
if($rows > 0) { mysql_data_seek($RecordsetCategory, 0);
while ($row_RecordsetCategory = mysql_fetch_assoc($RecordsetCategory)) {
$input = $row_RecordsetCategory['category'];
$words = str_word_count($quotes,1);
$shortest = -1;
foreach ($words as $word) {
$lev = levenshtein($input, $word);
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
if ($shortest == 0) { ?>
<option value="<?php echo $row_RecordsetCategory['id']?>"><?php echo $closest;?></option>
<?php }
}
}
?>
But what i get is the same category for all of those quotes, when each one must have a different >> category quotes
It seems that you want a while
loop, but are using a do
loop. Does this work better?
<?php
$rows = mysql_num_rows($RecordsetCategory);
if($rows > 0) {
mysql_data_seek($RecordsetCategory, 0);
while ($row_RecordsetCategory = mysql_fetch_assoc($RecordsetCategory)) {
$input = $row_RecordsetCategory['category'];
$chain = $quote;
$array = explode(" ", $quote);
$words = $array;
$shortest = -1;
foreach ($words as $word) {
$lev = levenshtein($input, $word);
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
?><option value="<?php echo $row_RecordsetCategory['id']?>"><?php echo $closest;?></option><?php
}
}
I can not say I fully understand what you are doing, but you will want to execute the query before operating off the query results.