Hallo,
Hoewel ik al een jaar of 10 met PHP bezig ben en vele forums heb doorgesnuffeld om antwoorden te vinden, heb ik nog nooit een forum gekozen om daar zelf vragen te plaatsen. Nu heb ik iets wat ik nergens kan vinden.
Ik ben nu voor het eerst echt bezig met zaken als PDO en arrays. Heb voor die tijd veel programmaregels geschreven waarvan ik nu weet dat het veel efficienter kan.
Ik heb deze functie gemaakt, komtie:
[code]
function getDataFromDB()
{
//In: nothing at the moment, 4 parameters in future
//Out: array
try{
$stmt = $conn->query('SELECT Countrycode, Country_fr FROM Country');
$Country_data = $stmt->fetchAll(PDO::FETCH_ASSOC);}
catch(PDOException $e) { echo "Country not found ";} //. $e->getMessage()
//echo 'Test'.'<pre>' . print_r($Country_data, true) . '</pre>';
$arrlength = count($Country_data);
for($x = 0; $x < $arrlength; $x++) {
$pc= $Country_data[$x]['Countrycode'];
$qc= $Country_data[$x]['Country_fr'];
$ac[]=$pc;
$bc[]=$qc;
}
$countryarray = array_combine($ac, $bc);
//print_r($countryarray);
return $countryarray;
} //end function GetDataFromDB
[code]
het stuk startend met try en eindigend op de returnregel werkt, letterlijk gecodeerd, perfect en wordt verderop in een foreach gebruikt op een invoerpagina.
De eerste print_r levert (klein stukkie):
Array
(
[0] => Array
(
[Countrycode] => A
[Country_fr] => Autriche
)
[1] => Array
(
[Countrycode] => AA
[Country_fr] => AutrrappSousRemarks
)
[2] => Array
(
[Countrycode] => AL
[Country_fr] => Albanie
)
Etc)
De tweede print_r:
Array ( [A] => Autriche [AA] => AutrrappSousRemarks [AL] => Albanie [AND] => Andorre [AO] => Angola [BE] => Belgique etc)
Zet ik het in deze functie (dus 4 regels vooraf en 1 er achteraan, dan krijg ik de array er niet uit, mijn menu blijft zichtbaar en verder is de pagina egaal groen in de gewenste stijl.
Ik probeer de aanroep zo op te pakken, buiten de functie:
$countryarray = getDataFromDB();
Wie kan helpen?
1.131 views