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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.