Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2012, 08:58 PM   PM User | #1
hannahfriedman
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
hannahfriedman is an unknown quantity at this point
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.
hannahfriedman is offline   Reply With Quote
Old 11-08-2012, 09:37 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 11-08-2012, 09:49 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 11-08-2012, 09:51 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 11-09-2012, 01:25 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:39 PM.


Advertisement
Log in to turn off these ads.