Jump to content

"strpos" - use only once on a variable?


jay.barnes

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.