Hey guys, I've searched php.net but I'm having problems figure out what command to use. I want php to take a field from a database and look for a character, the + (plus) sign specifically. If its there, I want to do something using an if statement, but I think I can take it from there. Is there a function that will let me do that, maybe one of the string commands? So to summarize I need to figure out If (plus sign exists in this database field) Show Image 1 else Show Image 2 Thanks for a lead in the right direction, I can usually find the answer but I'm not using the right search terms.
// assuming mysql connection is up $res = mysql_query("SELECT field FROM table WHERE id=123"); if (!$res) die("error in mysql query: " . mysql_error() ); $field = ""; $row = mysql_fetch_row($res); if ($row) { $field = $row[0]; } mysql_free_result($res); if ( substr($field, "+") ) { echo "AAA"; }else{ echo "BBB"; } PHP: Also check php.net for indexof() function, might be faster
The task you describe could probably done in more than one way, and would involve more than one function. Once PHP has extracted the field's value, ereg() might do the trick, something like: if (ereg("+",$value)) { do_something(); } The ereg can be more exact than this, it depends on what exactly you want (eg is the + always at the beginning or end of a line, etc).
You can probably do it straight from the database with LIKE SELECT * FROM yourtable WHERE field LIKE '%+%';
Thanks for your help!! I wasn't quite smart enough to get any of your suggestions to work...however in searching I found a workable solution. I realized that the field I was looking at had the + sign, but another field (used for mod-rewrite URLs), had the word "plus" in it, so I used preg_match. php.net tells me it's slow, but this works for now! if (preg_match("/plus/i", "$item")) { echo "Got Milk?"; } else { echo "No Milk!"; } PHP: The point of this all was to show sample product images. Some models have a + at the end, designating a heavier construction. So on normal model pages, I show a regular size sample and on heavy model pages I can show a heavy size sample. In the future I could add another field in the database to say heavy or regular, but it's not that big of a deal, I think this will work. Thanks again!
Roze What you were looking for is the strpos() function. It is designed to do what you want. if (strpos($item, 'plus') !== FALSE) echo 'Got Milk?'; else echo 'No Milk?';
Ah ha! Thank you draculus, this seems a bit more elegant than my solution. I can usually find what I need on php.net, but somtimes I just cant figure out what to search for!