gpt4 book ai didi

php - Like 语句匹配字符串的一部分

转载 作者:行者123 更新时间:2023-11-29 05:28:37 25 4
gpt4 key购买 nike

我正在使用脚本从 CSV 更新 Magento 中的产品价格。

目前,CSV 中的 SKU 与 Magento 中的 SKU 不完全匹配。例如,CSV 中的一个可能是 33456,但在 Magento 中它将是 003456_01003456_02。所以我需要脚本只匹配 SKU 的中间部分,所以如果在 Magento 中的 SKU 之后有 00_01 它仍然会更新所有匹配该 SKU 的内容在移动到下一行之前。

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

set_time_limit(0);
ini_set('memory_limit','1024M');

/***************** UTILITY FUNCTIONS ********************/
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}

function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}

function _getAttributeId($attribute_code = 'price'){
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id
FROM " . _getTableName('eav_attribute') . "
WHERE
entity_type_id = ?
AND attribute_code = ?";
$entity_type_id = _getEntityTypeId();
return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}

function _getEntityTypeId($entity_type_code = 'catalog_product'){
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne($sql, array($entity_type_code));
}

function _getIdFromSku($sku){
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));

}

function _checkIfSkuExists($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}

function _updatePrices($data){
$connection = _getConnection('core_write');
$sku = $data[0];
$newPrice = $data[1];
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();

$sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql, array($newPrice, $attributeId, $productId));
}
/***************** UTILITY FUNCTIONS ********************/

$csv = new Varien_File_Csv();
$data = $csv->getData('price-update/PRDAH014.csv'); //path to csv
array_shift($data);

$message = '';
$count = 1;
foreach($data as $_data){
if(_checkIfSkuExists($_data[0])){
try{
_updatePrices($_data);
$message .= $count . '> Success:: While Updating Price (' . $_data[1] . ') of Sku (' . $_data[0] . '). <br />';

}catch(Exception $e){
$message .= $count .'> Error:: While Upating Price (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
}
}else{
$message .= $count .'> Error:: Product with Sku (' . $_data[0] . ') does\'t exist.<br />';
}
$count++;
}
echo $message;

最佳答案

如果只想匹配中间部分,使用%

$sql = "SELECT sku FROM table WHERE item LIKE '%$code%'";

关于php - Like 语句匹配字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com