RE: Newbie: Array of objects iteration

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

 



@LinuxManMikeC and all @All
Thanks. I was more or less aware of that possibility, however, please let me
share the big picture will you guys: 

The main point is to access the properties name as well as values, of that
object, and put them on a xls file.
Instead of using mysql_num_rows, and mysql_num_files, and split and use a
bunch of loops.... I thought: 
Maybe fetching as an object may help me doing this far better. 
However, I'm having a hard time figuring out how to make the switch. 


To be honest, I don't really know if iterate is the "buzz word" here, I
would like to understand a little bit more the following:

We have in our hands an Object returned by a Fetch_All w/ fetch_obj option
applied to it, that object, when I do var_dump, reveals himself as in
object, containing an array and, each key of that array corresponds the
column names of our table or tables we have previously fetched. 
However, since we are working with an object, should we think differently
than if we were working with an array? If so, it is, in the fact, iteration,
the best option we have to put all properties and values into a xls file? Or
there are far better options for that?



@Lester
Yes, actually, I was having only one record on the database, but I believe
that is far to less. Since I was not been able to see if I was getting one
object, or several objects. That make me think of that. Thanks :) And
because of that, I was able (I hope) to properly understand Fernando
example:



@Fernando
foreach ( $records as $record ) {            
    foreach( $record as $column=>$value ) {
        echo "$column is $value\n";
    }
}
So the first foreach will iterate over each object of stdClass that
corresponds to each record of mysql data fetched, then, for each of them,
take the associative array key and the corresponding value...
Correct? :DDD




However, is this the right track to archive the goal stated above, or, they
are far better ways for doing so?



Regards,
Márcio



> -----Original Message-----
> From: Lester Caine [mailto:lester@xxxxxxxxxxx]
> Sent: sexta-feira, 9 de Outubro de 2009 07:23
> To: php-general@xxxxxxxxxxxxx
> Subject: Re:  Newbie: Array of objects iteration
> 
> Fernando Castillo Aparicio wrote:
> > I think you are just looking for the key in the wrong place. Try:
> >
> > foreach ( $records as $record ) {
> >     foreach( $record as $column=>$value ) {
> >         echo "$column is $value\n";
> >     }
> > }
> >
> > You've got the columns names in each record, not in the global
> recorset.
> 
> print_r($record); often helps - you can see what is actually returned
> and check that you have names rather than numbers for the fields ;)
> 
> print_r($records); becomes a little large if you have a lot of results
> so use with care :)
> 
> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk//
> Firebird - http://www.firebirdsql.org/index.php
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.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