Re: Associative array issues with loading values after initialization
| From: | Thomas Bolioli | Date: | Fri, 19 Sep 2008 17:43:42 +0000 |
| Subject: | Re: Associative array issues with loading values after initialization | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280534@lists.php.net to get a copy of this message | ||
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) {Maybe you should also add what it is that's "not working".$k = key($nex); if (strcmp($selected_index, $k) == 0) { $select = ' SELECTED'; } else { $select = ''; } print("<option value='".$k."'".$select.">".$nex[$k]."</option>");} }Thomas Bolioli wrote: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?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(' ' =>' ');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.while ($row = mysql_fetch_assoc($result)){$country_list[$row['pk_countryID']] = $row['country_name'];} return $country_list; }