werushka Posted January 21, 2011 Share Posted January 21, 2011 I have a code which I made just up <?php if ($product->title == '01'); print "Earring"; ?> <?php if ($product->title == '02'); print "Necklace"; ?> <?php if ($product->title == '03'); print "Necklace"; ?> <?php if ($product->title == '04'); print "Ring"; ?> I just want to check the product titles first 2 characters and print according to that. Example: If $product->title is "01DSE32" print Earring. This checking must be strgn or something like that which I don't remember. I would appreciate some feedback. Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/ Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 substr. Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162817 Share on other sites More sharing options...
QuickOldCar Posted January 21, 2011 Share Posted January 21, 2011 if (substr($product['title'], 0, 2) == "01"){ $variable = "do something with it"; } well you get the idea, can do what you need by checking the variable $variable_first_two = substr($variable, 0, 2); Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162819 Share on other sites More sharing options...
werushka Posted January 21, 2011 Author Share Posted January 21, 2011 I appreciate your fast reply I have written this but gave me white screen of death, I couldn't understand why <?php if (substr($product['title'], 0, 2) == "01"){ print "Earring";}?> Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162821 Share on other sites More sharing options...
QuickOldCar Posted January 21, 2011 Share Posted January 21, 2011 I just tried this and it worked. <?php $product['title'] = 01; if (substr($product['title'], 0, 2) == "01"){ print "Earring";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162823 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 Isn't your value in $product->title ? Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162825 Share on other sites More sharing options...
werushka Posted January 21, 2011 Author Share Posted January 21, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162827 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 Then that's the variable name you need to put into the code (don't just use code without reading it because it may not have anything to do with what you are doing.) Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162828 Share on other sites More sharing options...
werushka Posted January 21, 2011 Author Share Posted January 21, 2011 I tried this and is working perfect, I really appreciate your help guys <?php if (substr($product->title, 0, 2) == "01"){ echo t('Earring');}?> Quote Link to comment https://forums.phpfreaks.com/topic/225143-check-first-two-characters-of-field/#findComment-1162829 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.