thara Posted June 8, 2021 Share Posted June 8, 2021 I need to create the unique SKU (Stock Keeping Unit) name for each products mixed with product.id,product.name,product.category and product.brand. There is no problem to do it, i.e.: SKU for a product with id=13, name=Bio Clean green and category=INSECTS KILLERS brand=HARPIC becomes : BI-IKHA0013 This is how I tried: $inm = "Bio Clean green500ml"; $cnm = "INSECTS KILLERS"; $bnm = "HARPIC "; echo SKU_gen($inm, $cnm,$bnm,20).'<br>'; function SKU_gen($pname, $cat=null, $brand=null, $id = null, $l = 2){ $results = ''; // empty string $str1 = array_shift(explode(' ',$pname)); $str1 = strtoupper(substr($str1, 0, $l)); $str2 = array_shift(explode(' ',$cat)); $str2 = strtoupper(substr($str2, 0, $l)); $str3 = array_shift(explode(' ',$brand)); $str3 = strtoupper(substr($str3, 0, $l)); $id = str_pad($id , 4, 0, STR_PAD_LEFT); $results .= "{$str1}-{$str2}{$str3}{$id}"; return $results; } But this function is not working as expected when category of brand values become NULL. Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/ Share on other sites More sharing options...
Barand Posted June 8, 2021 Share Posted June 8, 2021 For the benefit of the non-clairvoyant among us, what are you wanting it to look like when there are NULL values? Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587102 Share on other sites More sharing options...
thara Posted June 8, 2021 Author Share Posted June 8, 2021 Actually sir, category is not null, only brand would be NULL. then SKU should be : BI-IK0013 Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587103 Share on other sites More sharing options...
Barand Posted June 8, 2021 Share Posted June 8, 2021 (edited) Using your posted code but with $bnm = null , I get BI-IN0020 Which is pretty similar to what you want. (Except I can't see how you are getting IK from the first 2 chars of "INSECT KILLER") Edited June 8, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587104 Share on other sites More sharing options...
NotSunfighter Posted June 8, 2021 Share Posted June 8, 2021 This is my take on things and it does throw an error is an input is null: <?php $inm = "Bio Clean green500ml"; $cnm = "INSECTS KILLERS"; $bnm = "HARPIC "; echo SKU_gen($inm, $cnm,$bnm,20).'<br>'; function SKU_gen($pname, $cat=null, $brand=null, $id = null, $l = 2){ if($pname == null) echo "empty<br><br><br>"; $result = ""; if($pname == "Bio Clean green500ml"){ $result = "BI-"; } else{ $result = "XX-"; } if($cat == "INSECTS KILLERS"){ $result = $result."IK"; } else{ $result = $result."XX"; } if($brand == "HARPIC "){ $result = $result."HA"; } else{ $result = $result."XX"; } $num = 10000 + $l; $result = $result.substr($num,1); if (strpos($result, "XX") !== false){ echo "There is an error: ".$result; }else{ echo $result; } } ?> with a lot of products I'd use switch statements instead of the if then statements. Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587105 Share on other sites More sharing options...
Barand Posted June 9, 2021 Share Posted June 9, 2021 @NotSunfighter could you show us how that would look with all 6,000 products? 😀 Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587124 Share on other sites More sharing options...
NotSunfighter Posted June 10, 2021 Share Posted June 10, 2021 (edited) It doesn't. OP was the one who used 4 integers. But there is also $inm = "Bio Clean green500ml"; $cnm = "INSECTS KILLERS"; $bnm = "HARPIC "; to expand that number significantly. Edited June 10, 2021 by NotSunfighter Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587136 Share on other sites More sharing options...
Barand Posted June 10, 2021 Share Posted June 10, 2021 Nothing to do with the integers. Your code would require 6,000 case statements just for the products. Then there would be hundreds more for the the categories and brands! Quote Link to comment https://forums.phpfreaks.com/topic/312878-create-sku-stock-keeping-unit-using-php/#findComment-1587139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.