Re: Associative array issues with loading values after initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux