[英]PHP/MYSQL: Compare cells in one column and overwrite matching substrings in another column or table
I have a bunch of item names I want to overwrite with links that have the name in them as a substring. 我有一堆项目名称,我想用其中包含名称作为子字符串的链接覆盖。 So I thought to put the data into columns and simply overwrite if the substring matches.
因此,我想将数据放入列中,如果子字符串匹配,则将其覆盖。 However this has become harder than I thought and I can't find any solution after a lot of trying.
但是,这变得比我想象的要难,经过大量尝试,我找不到任何解决方案。
Item Name | Link
==================================================
Ice Tea | <a href="www.nothing.com">Spiced Rum</a>
Spiced Rum | <a href="www.nothing.com">Peaches</a>
| <a href="www.nothing.com">Ice Tea</a>
And I want it to look like: 我希望它看起来像:
Item Name | Link
====================================================================================
<a href="www.nothing.com">Ice Tea</a> | <a href="www.nothing.com">Spiced Rum</a>
<a href="www.nothing.com">Spiced Rum</a> | <a href="www.nothing.com">Peaches</a>
| <a href="www.nothing.com">Ice Tea</a>
First I tried doing this with some PHP string functions, but I could not figure out how to loop through the ENTIRE Link column until a match is found, for each Item Name. 首先,我尝试使用一些PHP字符串函数执行此操作,但是在找到与每个项目名称匹配的对象之前,我不知道如何遍历ENTIRE Link列。 All I've managed to do is replace row by row.
我所要做的就是逐行替换。
<?php
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server.');
$query = "SELECT Item, Link FROM MyDB";
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
$i = 1;
while ($row = mysqli_fetch_array($data, MYSQLI_ASSOC)) {
$item = $row["Item"];
$link = $row["Link"];
$match = stripos($link, $item);
if ($match !== false) {
$query = "UPDATE Test SET Item = '$link' where ID = $i";
if (mysqli_query($dbc, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($dbc);
}
}
$i++;
}
mysqli_close($dbc);
?>
Then I tried various ways to use MySQL functions to replace a substring but I can't seem to get this to work. 然后,我尝试了各种方法来使用MySQL函数替换子字符串,但似乎无法正常工作。
SELECT column1, column2
FROM table
WHERE column1 LIKE CONCAT('%', column2, '%');
I could move the data to separate tables if that helps. 如果有帮助,我可以将数据移到单独的表中。
Use LOCATE
mysql function which finds a first position of a substring in a string, you'll need to find Item
in a Link
: 使用
LOCATE
mysql函数查找字符串中子字符串的第一个位置,您需要在Link
找到Item
:
UPDATE Test SET `Item` = `Link` WHERE 0 < LOCATE(`Item`, `Link`)
If you want to skip rows where Item
is NULL : 如果要跳过
Item
为NULL的行:
UPDATE Test SET `Item` = `Link` where `Item` IS NOT NULL AND 0 < LOCATE(`Item`, `Link`)
I was overthinking this a little. 我对此有点思考。 I didn't realise I could use strpos with arrays (if you simply extract each array value).
我没有意识到我可以将strpos与数组一起使用(如果您只是提取每个数组值)。 So now this will increment through Text and scan through Link until there is a match, then overwrite Text with Link.
因此,现在它将通过“文本”递增并通过“链接”进行扫描,直到找到匹配项,然后使用“链接”覆盖“文本”。
This is just for updating a large list of extracted data, not for "live" code. 这仅用于更新大量提取的数据,而不用于“实时”代码。
<?php
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server.');
$query = "SELECT Text, Link FROM Items";
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
$text = array();
$link = array();
$id = 1;
while ($row = mysqli_fetch_array($data)) {
$text[] = $row["Text"];
$link[] = $row["Link"];
}
for ($i = 0; $i < sizeof($text); $i++) {
$string_text = $text[$i];
for ($j = 0; $j < sizeof($link); $j++) {
$string_link = $link[$j];
$match = stripos($string_link, $string_text);
if ($match !== false) {
$link[$j] = $string_link;
$query = "UPDATE Items SET Text = '$link[$j]' WHERE ID = '$id'";
if (mysqli_query($dbc, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($dbc);
}
}
}
$id++;
}
mysqli_close($dbc);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.