Re: array in PHP

From: Piet (p.potjer_at_chello.nl)
Date: 12/03/04


Date: Fri, 03 Dec 2004 14:32:36 GMT

I let it print (see insertion) as a test
"d" <d@example.com> schreef in bericht
news:jD_rd.79663$38.17955@fe2.news.blueyonder.co.uk...
> Cool :) Where do you notice your array is missing variables?
>
> "Piet" <p.potjer@chello.nl> wrote in message
> news:Vz_rd.1736$yn4.972@amsnews03-serv.chello.com...
>> This is the exact code - with comments in Dutch.
>> "d" <d@example.com> schreef in bericht
>> news:_p_rd.79653$38.1588@fe2.news.blueyonder.co.uk...
>>> Is that the exact code you're using? There are quite a few problems
>>> with it, like the missing $ from variables, missing closing brackets,
>>> etc... it most definitely won't run :)
>>
>> Function MaakLijst ($idnr){
>> global $Lijst;
>> $m=1;
>> $Lijst = Leesnamen($idnr, $m);
Print $Lijst[1]["Naam"]; // Result: Pete
>> for ($n=1; $n<=7;$n++){
>> $Vader=$Lijst[$n]["Vader"];
>> $Moeder=$Lijst[$n]["Moeder"];
>> if ($Vader!="1"){
>> $lijst=Leesnamen($Vader, $n*2);
>> $Lijst=Leesnamen($Moeder, ($n*2)+1);
>> }
Print $Lijst[1]["Naam"]; // Result: Empty
Print $Lijst[2]["Naam"]; // Result: Chris

>> }
>> return $Lijst;
>> }
>> Function LeesNamen($idnr, $m) {
>> $x=False; // Variabele laat while lus niet onnodig lang doorlopen
>> //stel de variabelen voor de toegang tot de database in
>> $Host = "localhost";
>> $Gebruiker = "User";
>> $Wachtwoord = "";
>> $DBNaam = "Parenteel";
>> $Tabelnaam="familieleden";
>> $Verbinding=mysql_connect($Host, $Gebruiker, $Wachtwoord);
>> $Opdracht = "Select * from $Tabelnaam";
>> $Resultaat =mysql_db_query ($DBNaam, $Opdracht, $Verbinding);
>> //Haal de resultaten uit de database
>> while (($Rij = mysql_fetch_array ($Resultaat)) && ($x==FALSE)) {
>> if ($Rij[id]==$idnr) {
>> $Lijst[$m]["Naam"]=$Rij[Naam];
>> $Lijst[$m]["Vader"]=$Rij[Vader];
>> $Lijst[$m]["Moeder"]=$Rij[Moeder];
>> $x=True;
>> }
>> }
>> $x=False;
>> mysql_close($Verbinding);
>> }
>> return ($Lijst);
>> }>> "d" <d@example.com> schreef in bericht
>>>> news:5c_rd.69942$F7.2426@fe1.news.blueyonder.co.uk...
>>>>> Can you copy and paste your exact script here? It seems something
>>>>> very subtle is happening :)
>>>>>
>>>>> "Piet" <p.potjer@chello.nl> wrote in message
>>>>> news:c1_rd.56485$lN.37579@amsnews05.chello.com...
>>>>>> You're right: in my message I did not mention the quotes. I did in my
>>>>>> script. Sorry.
>>>>>> So the problem remains: The content of List[1]["Name"} disappears
>>>>>> when I call the function again.
>>>>>>
>>>>>> "d" <d@example.com> schreef in bericht
>>>>>> news:PqZrd.79502$38.8284@fe2.news.blueyonder.co.uk...
>>>>>>> For one thing, you should put your textual keys in quotes, so
>>>>>>> $List[1][Name] becomes $List[1]["Name"]. PHP shouldn't support the
>>>>>>> method you use (as, technically, Name should be a constant in your
>>>>>>> script, not a textual key) - it just does to prevent people's code
>>>>>>> from breaking :)
>>>>>>>
>>>>>>> Try changing that and see if it helps.
>>>>>>>
>>>>>>> If not, put some values in your array then put this command at the
>>>>>>> very end of your script:
>>>>>>>
>>>>>>> var_dump($List);
>>>>>>>
>>>>>>> It will display the contents of the $List variable, letting you see
>>>>>>> if things are as they should be.
>>>>>>>
>>>>>>> d
>>>>>>>
>>>>>>> "Piet" <p.potjer@chello.nl> wrote in message
>>>>>>> news:jjZrd.56081$lN.17932@amsnews05.chello.com...
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> I try to use a two-dimension array in a function.
>>>>>>>> It is declared as global (global $List).
>>>>>>>> When it is filled the first time everything seems to be okay:
>>>>>>>> $List[1][Name]="Pete";
>>>>>>>> When I call the function again I fill it like
>>>>>>>> $List[2][Name]="Chris";
>>>>>>>> The result seems good, but the foolish effect is that
>>>>>>>> $List[1][Name] is empty then.
>>>>>>>> What is going wrong? Can anybody help me?
>>>>>>>> (I use PHP 4.3.8.)
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Relevant Pages