CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   Error in strpos for form (http://www.codingforums.com/showthread.php?t=281590)

hannahfriedman 11-08-2012 08:58 PM

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.

Old Pedant 11-08-2012 09:37 PM

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.

Old Pedant 11-08-2012 09:49 PM

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.

Old Pedant 11-08-2012 09:51 PM

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


felgall 11-09-2012 01:25 AM

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.


All times are GMT +1. The time now is 10:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.