Jump to content

strpos quesiton


Go to solution Solved by CroNiX,

Recommended Posts

This will be an easy one for you guys...

 

One of the fields in a MySQL database is a varchar() that was populated using a checkbox method and can contain more than one response. in this case i have blue, yellow, green and want to check if the field contains blue before echoing the statement to the screen. How would i use strpos in this case? ive tried a few thigns and get syntax error. The below of course works if the field ONLY contains blue.

 if (($row['colorsyoulike']) =='blue') {
   echo "<tr>";
   echo "<td>Blue is an available Color!</td>";
 
}

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/293813-strpos-quesiton/
Share on other sites

  • Solution
if (strpos($row['colorsyoulike'], 'blue') !== FALSE)
{
  //$row['colorsyoulike'] contained 'blue' somewhere in the string
}

strpos() will return boolean FALSE if the string isn't contained in the search string.

 

However, it would probably be better to use stripos() which is case-insensitive, so it will match blue, BLUE, Blue, BlUe etc.

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/293813-strpos-quesiton/#findComment-1502426
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.