Jump to content

strpos() problem with numbers


StefanRSA

Recommended Posts

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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?

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.