Help regarding member country select

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
robeh
Forum Newbie
Posts: 1
Joined: Wed Nov 01, 2006 5:39 pm

Help regarding member country select

Post by robeh »

Hi there,

I'm currently still learning PHP, enjoying every minute of it. However I've come across a stumbling block and abit puzzled as to whats wrong exactly. Would be wonderful if someone can shed some light and let me know whats wrong.

Here the current code, im still testing, so forgive me if some vairable names seems strange or something.

Code: Select all

<? 

$sqlqueryold = mysql_query("SELECT name,prefix FROM countries WHERE prefix='$info[country]'");
$infoOC = mysql_fetch_array($sqlqueryold, MYSQL_ASSOC);
					
print "<OPTION value=\"$infoOC[prefix]\" selected=\"selected\">x: $infoOC[name]</OPTION>";
				
	$sqlquery = "SELECT name,prefix FROM countries ORDER BY name ASC";
	$result = mysql_query($sqlquery);
	$number = mysql_num_rows($result);

	$i = 0;

	if ($number < 1) {
	print "<p class=\"StandardText\">No countries!</p>";
	}
	else {
	while ($number > $i) {
	$co_name = mysql_result($result,$i,"name");
	$co_prefix = mysql_result($result,$i,"prefix");
								
	print "<OPTION value=\"$co_prefix\">$co_name</OPTION> \n";
	$i++;
	}
	}

?>
Now the select works for all intensive purposes. Basically its for a member profile page, where members can update theyre profile, country being one of them.

Its correctly retrieving the list of countries from my SQL DB and displaying them to the user in a select drop down box. The Update function is also working and checking shows the country prefix, for example (Brazil = br) is being entered into theyre SQL field in the users profile which is "country".

The problem being though, it should be displaying the users current country in the select box, pre selected if you would, when they first load up the "edit profile" page.

At the moment, its just displaying the x: prefix, with no country selected like it should.

Would be ever so grateful if someone could point out whats exactly wrong and where, I've been stuck with this for a while now!

Cheers for any help in advance.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

There are all kinds of ways to do it. You can just add a test condition ( $info['country'] == row_prefix ? selected : null ) in your result loop, or you can just use str_replace() after building your options. If your result array is huge, then the str_replace would be faster, because you don't need to run the condition for each result loop. But if it's a small result array then the inside loop condition would be ok!


// loop based condition

Code: Select all

$out = mysql_query ( "SELECT name, prefix FROM countries ORDER BY name ASC" );

if ( mysql_num_rows ( $out ) > 0 )
{
	$data = '';

	while ( $ra = mysql_fetch_array ( $out, MYSQL_ASSOC ) )
	{
		$data .= "<option value='" . $ra['prefix'] . "'" . ( $info['country'] == $ra['prefix'] ? " selected='selected'" : null ) . ">" . $ra['name'] . "</option>";
	}

	echo "<select name='country'>" . $data . "</select>";
}
else
{
	echo "<p class='StandardText'>No countries!</p>";
}
// outside loop condition

Code: Select all

$out = mysql_query ( "SELECT name, prefix FROM countries ORDER BY name ASC" );

if ( mysql_num_rows ( $out ) > 0 )
{
	$data = '';

	while ( $ra = mysql_fetch_array ( $out, MYSQL_ASSOC ) )
	{
		$data .= "<option value='" . $ra['prefix'] . "'>" . $ra['name'] . "</option>";
	}

	if ( ! empty ( $info['country'] ) )
	{
		$data = str_replace ( "'" . $info['country'] . "'", "'" . $info['country'] . "' selected='selected'", $data );
	}

	echo "<select name='country'>" . $data . "</select>";
}
else
{
	echo "<p class='StandardText'>No countries!</p>";
}
::edit::

fixed the semicolon error...


printf
Post Reply