billerda Posted September 18, 2007 Share Posted September 18, 2007 Hi All, Not sure if strpos() is the way to go with this or not. Here's the scenario. I have a field in my MySql database called "Items" containing a value like "001, 002, 003, 004, 005". So when I pull the database field like $row["Items"] and put it into a string (strItems) I want to then scan the value and see if it contains "003" and if it does do something different in my code the the other numbers. In ASP I would do this something like <% if InStr(strSubCategory, "003") <> 0 then %> How do I pull it off in PHP? Thanks in advance! Bill Quote Link to comment https://forums.phpfreaks.com/topic/69765-newb-needs-help-parsing-database-field/ Share on other sites More sharing options...
freakstyle Posted September 18, 2007 Share Posted September 18, 2007 hey there, i would suggest taking that string and converting it to an array, since thats what it looks like anyhow. ex: <?php $strItems = '001, 002, 003, 004, 005'; // your string $SearchFor = '003' ; // what you are searching for $arrayItems = explode(',', $strItems ); // new array created by splitting the data between the comma // now you check if that value is found in the array if ( in_array($SearchFor, $arrayItems ) ) : echo 'i found ' . $SearchFor. ' in the array '; else : echo 'i did not find the string in the array, why?' ; endif ; good luck. Quote Link to comment https://forums.phpfreaks.com/topic/69765-newb-needs-help-parsing-database-field/#findComment-350511 Share on other sites More sharing options...
billerda Posted September 18, 2007 Author Share Posted September 18, 2007 Hey freakstyle, Thanks for the suggestion, looks like a step in the right direction. Turns out I'm going to have to use a Case statement instead of an If statement. Something like Case "001" was found in string do this Case "002" was found in string do that Case "003" was found in string do something else etc What would you recommend in this instance for determining what numbers are found in the string? Thanks again Bill Quote Link to comment https://forums.phpfreaks.com/topic/69765-newb-needs-help-parsing-database-field/#findComment-350623 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.