Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error in strpos for form

    I'm coding the locations for groups, and the user can search based on the location to find the nearest group to them. The fields are: country, state, city, neighborhood. Let's say there are ten groups in the USA -- I don't want it to list the option USA ten times. I added in a strpos so that it will only list them once, but I'm getting an error.

    Here's the php code:
    Code:
    <?php
    $myQuery = "select country, state, city, neighborhood from groups WHERE group_status = 'open to new members'";
    $rs = mysql_query($myQuery);
    $country_options = $state_options = $city_options = $neighborhood_options = '';
    while($get_row = mysql_fetch_assoc($rs)){
    $pos_country = strpos($get_row['country'], $country_options);
    if($pos_country === false) {
    echo $country_options .= "<option value='" . $get_row['country'] . "'>" . $get_row['country'] . "</option>";}
    		
    $pos_state = strpos($get_row['state'], $state_options);
    if($pos_state === false) {
    echo $state_options .= "<option value='" . $get_row['state'] . "'>" . $get_row['state'] . "</option>";}
    			
    $pos_city = strpos($get_row['city'], $city_options);
    if($pos_city === false) {
    echo $city_options .= "<option value='" . $get_row['city'] . "'>" . $get_row['city'] . "</option>";}
    			
    $pos_neighborhood = strpos($get_row['neighborhood'], $neighborhood_options);
    if($pos_neighborhood === false) {
    echo $neighborhood_options .= "<option value='" . $get_row['neighborhood'] . "'>" . $get_row['neighborhood'] . "</option>";}
     
    }
    ?>
    It outputs the following errors:
    Warning: strpos(): Empty delimiter in sidebar.php on line 66

    Warning: strpos(): Empty delimiter in sidebar.php on line 70

    Warning: strpos(): Empty delimiter in sidebar.php on line 73

    Warning: strpos(): Empty delimiter in sidebar.php on line 76

    Underneath the error it has a nice form with the correct fields: country, state, city, neighborhood. It's just listing the same countries multiple times.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Has nothing to do with MySQL. Try posting in the PHP forum.


    However...

    I don't use PHP, but I can read documentation.
    http://www.php.net/manual/en/function.strpos.php

    Looks to me like you have the arguments to strpos( ) *BACKWARDS*.

    The thing to search *IN* is supposed to be first.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    OH! THIS IS FUNNY!

    LOOK at that code:
    Code:
    echo $country_options .= "<option value='" . $get_row['country'] . "'>" . $get_row['country'] . "</option>";}
    Each time you add a new option to the $xxx_options, you echo it.

    But what you are echoing is the FULL SET OF OPTIONS in $xxx_options!

    Because the .= operator returns the CONCATENATED result, not just the part *AFTER* the operator.

    You need to write something like this, instead:
    Code:
    $val = $get_row["country"]; // not needed, but enhances performance
    if ( strpos($country_options, $val) === false )
    {
        $opt = '<option value='"' . $val . '">' . $val . "</option\n";
        $country_options .= $opt;
        echo $opt;
    }
    HTML tags *should* use "..." for properties, not '...', which is why I changed your quoting. And the generated HTML will be easier to read and debug if you put line breaks in, as shown.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Again, I don't use PHP, but I think you could write that a bit more compactly thus:
    Code:
    strpos($country_options, ( $val = $get_row["contry"] ) ) === false ) 
    { 
        echo ( $opt = '<option value='"' . $val . '">' . $val . "</option\n" ); 
        $country_options .= $opt; 
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    PHP does treat " and ' differently as well - if you wrap the text in " then it gets parsed for all sorts of escape characters and also for variable references whereas text wrapped in ' only gets parsed for \' escapes. So that's another reason to switch the quotes around.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •