StefanRSA Posted October 7, 2010 Share Posted October 7, 2010 If $allcat=10 and $cat_id in my While loop is 1,2,4,5,6,7,8,10,12 Why does $pos = strpos($allcat,$cat_id); select 1 and 10? My script: $ssql="SELECT * FROM adcat WHERE disabled='0' ORDER BY name ASC"; $sresult=mysql_query($ssql); while($srows = mysql_fetch_array($sresult)){ $cat_id = $srows['id']; $cat_name = $srows['name']; $link=$srows['clinkname']; $tot_ads = $srows['ads']; $cat_desc = $srows['description']; $rrows=$srows['div']; $catcol1=$srows['catcol1']; $catcol2=$srows['catcol2']; $catcol3=$srows['catcol3']; $catcol3a=$srows['catcol3a']; $divsub=$srows['divsub']; ///////////////// $pos = strpos($allcat,$cat_id); if($pos === false) { $selected[$cat_id]=''; }else { $selected[$cat_id]='selected="selected"'; } //////////////// echo '<option '.$selected[$cat_id].'>'.$cat_name.'</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/ Share on other sites More sharing options...
Psycho Posted October 7, 2010 Share Posted October 7, 2010 Your answer is in the manual. Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used. In other words, if you are using a PHP version prior to v5 then it is only using the first character. Looking at your logic though, are you really wanting to use strpos()? Or are you wanting to see if the $cat_id value is equal to $allcat? Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119937 Share on other sites More sharing options...
StefanRSA Posted October 7, 2010 Author Share Posted October 7, 2010 Thanks mjdamato. I am using php5.3.2 To be honest... I am not sure if strpos() is the correct function to use here. I am looking at the DB to see what fields were selected in a select box and am now trying to re-populate the select box with the values in the DB as selected on edit. Any suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119942 Share on other sites More sharing options...
StefanRSA Posted October 7, 2010 Author Share Posted October 7, 2010 In short... I am looking for "10" in a while loop that contain "1,2,4,5,7,8,10,12" Why does it find "1" and "10" Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119962 Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2010 Share Posted October 7, 2010 if $allcat = "10", 1 and 10 both occur once. Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119968 Share on other sites More sharing options...
StefanRSA Posted October 7, 2010 Author Share Posted October 7, 2010 Yes... But I am not looking for 1... I am looking for 10... $cat_id=10 $allcat="1,2,3,5,7,8,10,12"; So, as per my script... When it finds 1 or 2 or 3 or 5 or 7 or 8 or 12 $selected[$cat_id]=''; and only 10 Should be $selected[$cat_id]='selected="selected"'; ---------------- But for some unknown reason 1 and 10 gets the value of $selected[$cat_id]='selected="selected"' instead of only 1 ?????? while($srows = mysql_fetch_array($sresult)){ $cat_id = $srows['id']; ///////////////// $pos = strpos($allcat,$cat_id); if($pos === false) { $selected[$cat_id]=''; }else { $selected[$cat_id]='selected="selected"'; } //////////////// echo '<option '.$selected[$cat_id].'>'.$cat_name.'</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119978 Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2010 Share Posted October 7, 2010 if $allcat="1,2,3,5,7,8,10,12", neither of these $pos === false: $pos = strpos($allcat, "1"); // returns 0, for position 0 $pos = strpos($allcat, "10"); // returns 12, for position 12. maybe I'm still misunderstanding you. Why not just store the values of $allcat in an array and us in_array() instead of strpos? Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119980 Share on other sites More sharing options...
StefanRSA Posted October 7, 2010 Author Share Posted October 7, 2010 Thumbs Up BlueSkyIS! Thanks! It solved my problem! Quote Link to comment https://forums.phpfreaks.com/topic/215364-strpos-problem-with-numbers/#findComment-1119983 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.