Re: [PHP] Associative array issues with loading values after initialization
funny how that perl code looks so much like php ... or is it the other way around. Thomas Bolioli schreef: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } okay, so all the while, next, key stuff is gonna make your eyes bleed. we have foreach() ... it is your friend: function dropBox($items, $selected, $name, $output = false) { if (!is_array($items)) return; $opts = array(); foreach($items as $k = $v) { $k = htmlentities($k, ENT_QUOTES); // htmlentities should really $v = htmlentities($v, ENT_QUOTES); // be getting a charset are 4rd arg $s = $k == $selected ? ' selected=selected' : ''; $opts[] = 'option value='.$k.''.$s.''.$v.'/option'; } $html = 'select name='.$name.''.join('', $opts).'/select'; if ($output) echo $html; else return $html; } Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } I know how to write this in perl but for some reason, when I write it in PHP it doesn't work. In perl it would be (roughly): function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); my %country_list; while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return \%country_list; } What am I doing wrong here? you learnt perl first :-D Thanks in advance, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
On Fri, 2008-09-19 at 13:43 -0400, Thomas Bolioli wrote: I hav ebeen able to track down that this is the part not working. It throws a parse error: PHP Parse error: syntax error, unexpected T_VARIABLE on the line where the foreach loop starts. function dropbox_from_list($list, $selected_index){ foreach ($list as $k = $v) { if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print option value='$k'$select$v/option; } } b wrote: Thomas Bolioli wrote: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { I'd use foreach() here and avoid next(). At least, reset the array first. And maybe pass the array by reference: function dropbox_from_list($list, $selected_index) { foreach($list as $k = $v) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Maybe you should also add what it is that's not working. Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); Are you starting with an empty key value so that you'll have an empty option in your select list? Why not just print an empty one? while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } Start with the obvious: what does $country_list contain when it's returned? Again, some details about what you're getting would go a long way toward getting some advice. You should think about changing that SELECTED line to $select = ' selected=selected'; as the previous way is not recommended for modern code. Ash www.ashleysheridan.co.uk
[PHP] Associative array issues with loading values after initialization
The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } I know how to write this in perl but for some reason, when I write it in PHP it doesn't work. In perl it would be (roughly): function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); my %country_list; while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return \%country_list; } What am I doing wrong here? Thanks in advance, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } I know how to write this in perl but for some reason, when I write it in PHP it doesn't work. In perl it would be (roughly): function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); my %country_list; while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return \%country_list; } What am I doing wrong here? Thanks in advance, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
This came straight out of the docs and it doesn't even work. It throws PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING on the line 'one' = 1, What is wrong with how I am trying to do this loop?? Thanks, Tom $a = array( 'one' = 1, 'two' = 2, 'three' = 3, 'seventeen' = 17 ); foreach ($a as $k = $v) { echo \$a[$k] = $v.\n; } Thomas Bolioli wrote: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } I know how to write this in perl but for some reason, when I write it in PHP it doesn't work. In perl it would be (roughly): function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); my %country_list; while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return \%country_list; } What am I doing wrong here? Thanks in advance, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
Thomas Bolioli wrote: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { I'd use foreach() here and avoid next(). At least, reset the array first. And maybe pass the array by reference: function dropbox_from_list($list, $selected_index) { foreach($list as $k = $v) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Maybe you should also add what it is that's not working. Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); Are you starting with an empty key value so that you'll have an empty option in your select list? Why not just print an empty one? while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } Start with the obvious: what does $country_list contain when it's returned? Again, some details about what you're getting would go a long way toward getting some advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
I hav ebeen able to track down that this is the part not working. It throws a parse error: PHP Parse error: syntax error, unexpected T_VARIABLE on the line where the foreach loop starts. function dropbox_from_list($list, $selected_index){ foreach ($list as $k = $v) { if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print option value='$k'$select$v/option; } } b wrote: Thomas Bolioli wrote: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { I'd use foreach() here and avoid next(). At least, reset the array first. And maybe pass the array by reference: function dropbox_from_list($list, $selected_index) { foreach($list as $k = $v) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Maybe you should also add what it is that's not working. Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); Are you starting with an empty key value so that you'll have an empty option in your select list? Why not just print an empty one? while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } Start with the obvious: what does $country_list contain when it's returned? Again, some details about what you're getting would go a long way toward getting some advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Associative array issues with loading values after initialization
I found the issue. The whitespace in between $list as $k = $V was all not truly whitespace. Gotta love BBEdit... Thomas Bolioli wrote: I hav ebeen able to track down that this is the part not working. It throws a parse error: PHP Parse error: syntax error, unexpected T_VARIABLE on the line where the foreach loop starts. function dropbox_from_list($list, $selected_index){ foreach ($list as $k = $v) { if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print option value='$k'$select$v/option; } } b wrote: Thomas Bolioli wrote: I should add, it is not working with this funciton, which could be the source of the issue. function dropbox_from_list($list, $selected_index){ while ($nex = next($list)) { I'd use foreach() here and avoid next(). At least, reset the array first. And maybe pass the array by reference: function dropbox_from_list($list, $selected_index) { foreach($list as $k = $v) { $k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print(option value='.$k.'.$select..$nex[$k]./option); } } Maybe you should also add what it is that's not working. Thomas Bolioli wrote: The below function is not working. function crm_get_country_list(){ global $dbh; $result = mysql_query(SELECT * FROM countries ORDER BY pk_country_id ASC, $dbh) or die(mysql_error()); $country_list = array(' ' =' '); Are you starting with an empty key value so that you'll have an empty option in your select list? Why not just print an empty one? while ($row = mysql_fetch_assoc($result)){ $country_list[$row['pk_countryID']] = $row['country_name']; } return $country_list; } Start with the obvious: what does $country_list contain when it's returned? Again, some details about what you're getting would go a long way toward getting some advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php