Re: how to extract fields from associative array into different variables

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

 



Yuri Yarlei wrote:
> You can try use the foreach function.
>
> ex:
> if ($res = mysql_query ($sql, $db)) {
>  /* I personally prefer "res" for "resource", not "results" */
>  while ($r = mysql_fetch_assoc ($res)) {
>  $rows[] = $r;
> }
> }
>
>
> foreach ($rows as $row){
>   echo $row['first_name'];
>   echo "<br>";
> }
>
> the foreach function transform the array $rows[] (the fisrt level of associative array) in a variable $row.
>
>   
Thanks Yuri, I needed this explanation. Looks like it might be a simpler
alternative to my while... I was thinking about it, but didn't quite
grasp it... ashita wa "shining brain" ;-)
> Yuri Yarlei.
> Programmer PHP, CSS, Java, PostregreSQL;
> Today PHP, tomorrow Java, after the world.
> Kyou wa PHP, ashita wa Java, sono ato sekai desu.
>
>
>
>   
>> To: php-general@xxxxxxxxxxxxx
>> From: news.NOSPAM.0ixbtqKe@xxxxxxxx
>> Date: Thu, 18 Jun 2009 14:17:59 +0200
>> Subject: Re:  how to extract fields from associative array into	different variables
>>
>> On Tue, 16 Jun 2009 08:50:45 -0400, PJ wrote:
>>
>>     
>>> Ashley Sheridan wrote:
>>>       
>>>> On Mon, 2009-06-15 at 17:38 -0400, PJ wrote:
>>>>   
>>>>         
>>>>> Then I have to add some loopy thing to assign the values to the
>>>>> $variables... a real pita since my variable do not lend themselves too
>>>>> well to linear alterations ($varIn, $var2IN, $var3IN... etc... so $i=0
>>>>> and $i++ arren't going to be too cooperative here... I guess I'd have to
>>>>> change that to $var1IN... and then figure out how to do $var.$i.IN ... I
>>>>> already tried, but don't seem to have it right
>>>>>     
>>>>>           
>>>> Why do you need them in separate variables? Can't you use them using
>>>> array syntax elsewhere in your code?
>>>>         
>>> Basically, I don't know how else to handle inserting and updating random
>>> ...
>>>       
>> What he's talking about is doing this:
>>
>>   if ($res = mysql_query ($sql, $db)) {
>>     /* I personally prefer "res" for "resource", not "results" */
>>     while ($r = mysql_fetch_assoc ($res)) {
>>       $rows[] = $r;
>>     }
>>   }
>>
>> And then you access the individual rows/columns as
>>   $rows[0]['first_name']
>>   $rows[0]['last_name']
>>   $rows[1]['first_name']
>>   $rows[1]['last_name']
>> and so on, in some kind of loop probably.
>>
>> (I thought this had been spelled out already, but I
>>  was unable to find it so perhaps not.)
>>
>>
>> /Nisse
>>
>> -- 
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>     
>
> _________________________________________________________________
> Deixe suas conversas mais divertidas. Baixe agora mesmo novos emoticons. � gr�tis!
> http://specials.br.msn.com/ilovemessenger/pacotes.aspx
>   


-- 
Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
-------------------------------------------------------------
Phil Jourdan --- pj@xxxxxxxxxxxxx
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
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