检查文章是否包含团队名称


Checking if article contain team names

我正试图将我的文章连接到不同的团队,这取决于它包含的团队。目前我有以下3张表:

teams(id, name, shortname)
news(id, title, text, url)
contain(team_id, news_id)

我的问题是在以下插入查询之后

mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");

我想检查变量$full_text_strip是否包含团队名称或shortname中的一个,如果是,它应该在contains表中创建一个新记录,其中包含刚刚创建的news_id和team_id。最简单的方法是什么?

这是一个未经测试的SQL解决方案:

<?php
$query = "select name, shortname, id from teams";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $ids[$row['id'][] = $row['name'];
    $ids[$row['id']][] = $row['shortname'];
}
foreach($ids as $id => $teams) {
    foreach($teams as $team) {
        if(strpos($full_text_strip, $team) !== false) {
            //mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
            echo 'We are inserting here because ' . $team . ' is present.';
        }
    }
}
?>

测试代码:

<?php
$title = 'this is input blue jays for the testing';
$ids = array(1 => array('blue jay', 'blues'), 2 => array('red sox', 'reds'), 3 => array('white sox', 'whites'));
foreach($ids as $id => $teams) {
    foreach($teams as $team) {
        if(strpos($title, $team) !== false) {
            echo 'Insert ' . $team . 'with id ' . $id;
        }
    }
}