Having trouble with strpos(), how to return true?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
erika
Forum Newbie
Posts: 17
Joined: Sat Oct 25, 2008 5:27 pm

Having trouble with strpos(), how to return true?

Post by erika »

I don't know if I'm going about this the right way. If there is a better way to deal with this, I'd appreciate that information.

I'm retrieving the value of $_POST, which includes a series of elements whose names start with "promo_id_" and are followed by an integer. The integers will always vary. There are also several other elements in $_POST, but right now they're irrelevant to my problem... except that I can't process the whole $_POST array since they're in there and I don't need them for this particular task.

I'd like to place all the elements whose keys start with promo_id_ into a separate array to use later. I noted that php.net indicates that strpos() can return a deceptive value that evaluates to false and that === should be used... but I think I'm doing it wrong.

So I thought I could do it like this (the print statements are part of my attempts to make it work):

Code: Select all

                $new_data = Array();
                foreach ($_POST as $key => $sort_order) {
                    // What is the value in key?
                    $key_value = $key;
                    print "Key value is: " . $key_value . "<br />";

                    // Does it include the text "promo_id_"?
                    $in_key = strpos($searching_for,$key_value);
                    print "in_key value is: " . $in_key . "<br />";

                    // Check if the value is zero                    
                    if ($in_key === "0") {
                        // If we find it, strip it out and save the
                        // remaining number to $promo_id
                        $promo_id = str_replace('promo_id_','',$key_value);
                    }

                    // Add the data to the new array
                    $new_data[$promo_id] = $sort_order
                }
So if $_POST originally included:

Code: Select all

    [promo_id_26] => 1
    [promo_id_16] => 5
    [promo_id_17] => 8
    [promo_id_20] => 1
What I wanted to get out of it in $new_data was:

Code: Select all

    [26] => 1
    [16] => 5
    [17] => 8
    [20] => 1
The number attached to the promo_id is pulled from the primary key in a MySQL table and will always be unique, so that isn't a concern.

But instead I just get nothing--the if statement never returns true because $in_key is always NULL (not zero like I thought it would be), so the commands in it never happen.

I'm sure I'm just missing something really obvious, but I'm at a loss. Any suggestions for how to make my code work or recommendations for a different method that would accomplish what I am trying to do would be greatly appreciated.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Having trouble with strpos(), how to return true?

Post by John Cartwright »

if ($in_key === "0") {

should be

if ($in_key !== false) {

Another thing, you have $searching_for defined and set correctly elsewhere?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Having trouble with strpos(), how to return true?

Post by requinix »

Do you control the HTML for the form? Because there's a really easy way of doing this. Say the form field is a checkbox:

Code: Select all

<input type="checkbox" name="promo_id[26]" value="1" />
$_POST["promo_id"] will then be an array in exactly the format you want.
erika
Forum Newbie
Posts: 17
Joined: Sat Oct 25, 2008 5:27 pm

Re: Having trouble with strpos(), how to return true?

Post by erika »

Tasairis: Thanks, I had NO idea that was possible! I think that may solve my problem for this particular application.

John: Yes, $searching_for was set elsewhere (I've also tried just putting 'promo_id_' directly into the strpos function), I should have included that. The !== false solution didn't help... the string is definitely in there, but it isn't working... could it be related to the underscores? For future reference in other applications I'd really like to get this figured out.
Post Reply