从单词中获取下一个和之前的单词

时间:2014-09-21 22:38:33

标签: php mysql

我在数据库中有单词:

=====================
= id = kata = posisi
=====================
= 1 = you   =  1         =
= 2 = should=  2         =
= 3 = eat   =  3 9 20    =
etc

列posisi显示一个句子中单词的位置,对于我在其coloumn中将其位置与空格分开的每个单词,可以有2个或更多位置。该表来自句子的索引过程:

You should eat two bananas before lunch and eat two before your dinner and then consume one banana. i eat banana

我想先获得5个单词,然后从关键字中获取5个单词。 例如,我提交关键字and我将得到结果:

eat two bananas before lunch and eat two before your dinner

eat two before your dinner and then consume one banana. i

这是我尝试过的代码:

require_once 'connection.php';

function getPosition ($keyword) {
    $key = strtolower($keyword);
    $query = mysql_query("SELECT * FROM words WHERE kata = '$key' ") or die(mysql_error());
    while ($row = mysql_fetch_array($query)) {
        $position = $row['posisi'];
    }
    return $position;
}

$key= 'And';
$and = getPosition($key);
$explode = explode (' ', $and);
foreach ($explode as $data => $datas){
    for ($i = 1 ; $i < 6 ;$i++){
        $nextdata[$i] = (int)$datas + $i;
        $databefore[$i] = (int)$datas - $i;

        $queryNext = mysql_query("SELECT kata FROM words WHERE posisi = '$nextdata[$i]' and posisi =$databefore[$i] ") or die(mysql_error());
        while ($row = mysql_fetch_array($queryNext)) {
            $position = $row['kata'];
        }
        //print_r($position);

    }
}

但它不起作用,也许问题是当它遇到具有多个位置的列时,它无法读取它。请帮我。谢谢:)

0 个答案:

没有答案