jay.barnes Posted September 13, 2010 Share Posted September 13, 2010 Hi all, I've gotten some great help from people here in the past, and just return with one question: I've got a field in a DB that contains a string of hyphen-separated values: "Accordion-Bass(Upright)-Bass-Bassoon" I pull that string through a MySQL query, which ends up in the array key "$currentuser['Skills']" Based on that string, I want to check a series of check-boxes to see whether a particular string is present in the array key, and, if so, check the corresponding box: <input name="M-Accordian" type="checkbox" value="Accordion-" <?PHP if (strpos($currentuser['Skills'],"Accordion-") == "true") echo "checked=\"checked\"";?>/>Accordion <br /> <input name="M-UpBass" type="checkbox" value="Bass (Upright)-" <?PHP if (strpos($currentuser['Skills'],"Bass (Upright)-") == "true") echo "checked=\"checked\"";?> />Bass (upright) <br /> <input name="M-Bass" type="checkbox" value="Bass-" <?PHP if (strpos($currentuser['Skills'],"Bass") == "true") echo "checked=\"checked\"";?> />Bass <br /> <input name="M-Bassoon" type="checkbox" value="Bassoon-" <?PHP if (strpos($currentuser['Skills'],"Bassoon-") == "true") echo "checked=\"checked\"";?> />Bassoon <br /> However, whenever I load the form, despite the presence of the strings, only the first checkbox will ever correctly load checked. Can "strpos" only be invoked on a variable once, after which it can no longer be used? Thanks, and please let me know if you need more info to work from! Howver Link to comment https://forums.phpfreaks.com/topic/213263-strpos-use-only-once-on-a-variable/ Share on other sites More sharing options...
sasa Posted September 13, 2010 Share Posted September 13, 2010 try to explode $currentuser['Skills'] with '-' and then use in_array() function Link to comment https://forums.phpfreaks.com/topic/213263-strpos-use-only-once-on-a-variable/#findComment-1110534 Share on other sites More sharing options...
jay.barnes Posted September 13, 2010 Author Share Posted September 13, 2010 Worked great - thanks! So, am I correct about the function only being able to be used on a variable once? Link to comment https://forums.phpfreaks.com/topic/213263-strpos-use-only-once-on-a-variable/#findComment-1110633 Share on other sites More sharing options...
sasa Posted September 13, 2010 Share Posted September 13, 2010 no let see 1st condition strpos($currentuser['Skills'],"Accordion-") == "true" strpos return number 0 (start position of substring) when php compare number and string it convert string to number and get 0 == 0 => true (btw. "true" is not same as true) 1st condition can be strpos($currentuser['Skills'].'-',"Accordion-") !== false 2nd must be strpos($currentuser['Skills'].'-',"Bass (Upright)-") !== false and so on Link to comment https://forums.phpfreaks.com/topic/213263-strpos-use-only-once-on-a-variable/#findComment-1110650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.