Re: [PHP] Newbie: Array of objects iteration
- Original Message From: MEM tal...@gmail.com To: Tommy Pham tommy...@yahoo.com; php-general@lists.php.net Sent: Sat, October 10, 2009 2:49:23 AM Subject: RE: [PHP] Newbie: Array of objects iteration MEM, http://www.php.net/language.oop5.reflection Regards, Tommy And brand new world opens in from of my eyes... O.O. I will search more info on this on the net... just for the records, as properties names is concern, I couldn't find any better I suppose: http://pt.php.net/manual/en/reflectionproperty.getname.php I'm just wondering, we call it like this? $property = new ReflectionProperty($myproperty); $proptertyName = $property-getName(); Can we called statically like this? $propertyName = ReflectionProperty::getName($myproperty); - this would be nice. :) The documentation is a little bit lacking, and I'm a little bit newbie, is this correct? Note: The array_keys as a possibility to retrieve ALL keys from a given array. It would be nice to retrieve ALL properties names at once as well... :) Regards, Márcio -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Márcio, Sorry, I don't remember since it's been over 5 years I used reflection. Reflection has many uses, 1 of it is that it allow you reverse engineer any PHP app ;) Regards, Tommy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Newbie: Array of objects iteration
MEM, http://www.php.net/language.oop5.reflection Regards, Tommy And brand new world opens in from of my eyes... O.O. I will search more info on this on the net... just for the records, as properties names is concern, I couldn't find any better I suppose: http://pt.php.net/manual/en/reflectionproperty.getname.php I'm just wondering, we call it like this? $property = new ReflectionProperty($myproperty); $proptertyName = $property-getName(); Can we called statically like this? $propertyName = ReflectionProperty::getName($myproperty); - this would be nice. :) The documentation is a little bit lacking, and I'm a little bit newbie, is this correct? Note: The array_keys as a possibility to retrieve ALL keys from a given array. It would be nice to retrieve ALL properties names at once as well... :) Regards, Márcio -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie: Array of objects iteration
2009/10/10 MEM tal...@gmail.com: MEM, http://www.php.net/language.oop5.reflection Regards, Tommy And brand new world opens in from of my eyes... O.O. I will search more info on this on the net... just for the records, as properties names is concern, I couldn't find any better I suppose: http://pt.php.net/manual/en/reflectionproperty.getname.php I'm just wondering, we call it like this? $property = new ReflectionProperty($myproperty); $proptertyName = $property-getName(); Can we called statically like this? $propertyName = ReflectionProperty::getName($myproperty); - this would be nice. :) The documentation is a little bit lacking, and I'm a little bit newbie, is this correct? Note: The array_keys as a possibility to retrieve ALL keys from a given array. It would be nice to retrieve ALL properties names at once as well... :) Regards, Márcio Hi Márcio, You may also find the following useful: http://php.net/get_class_vars http://php.net/get_object_vars http://php.net/reflectionclass.getproperties Regards, Torben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie: Array of objects iteration
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. Good luck ;-) De: MEM tal...@gmail.com Para: php-general@lists.php.net Enviado: jue,8 octubre, 2009 22:59 Asunto: [PHP] Newbie: Array of objects iteration Hello all, I'm grabbing all records from a table using: $records = $stmt-fetchAll(PDO::FETCH_OBJ); return $records; In order to display the values we can do: foreach ($records as $record) { echo $record-id; echo $record-name; } However, I'd like to grab, also, the *column names*. I've tried: foreach ($records as $column=$value) { echo $column is $value\n; } But I get: Catchable fatal error: Object of class stdClass could not be converted to string Can I have your help on how can I properly get the column values? Regards, Márcio -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] 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
RE: [PHP] Newbie: Array of objects iteration
@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:les...@lsces.co.uk] Sent: sexta-feira, 9 de Outubro de 2009 07:23 To: php-general@lists.php.net Subject: Re: [PHP] 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
Re: [PHP] Newbie: Array of objects iteration
De: MEM tal...@gmail.com Para: Lester Caine les...@lsces.co.uk; php-general@lists.php.net Enviado: vie,9 octubre, 2009 12:40 Asunto: RE: [PHP] Newbie: Array of objects iteration @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 - Correct about my example, although I'm not sure if you get each record as an array or as an object. Anyway, you can iterate both. And if you want to get the column names, I suppose you could use array_keys() on the first record if you receive an array, or maybe get_object_vars() if it's an objecs (note that with this you get an array with keys and values). I've found PDOStatement-getColumnMeta(). There's a big fat warning saying this is experimental, but you could take a look in case you need more data on the columns. Hope it helps.
RE: [PHP] Newbie: Array of objects iteration
Correct about my example, although I'm not sure if you get each record as an array or as an object. Anyway, you can iterate both. And if you want to get the column names, I suppose you could use array_keys() on the first record if you receive an array, or maybe get_object_vars() if it's an objecs (note that with this you get an array with keys and values). I've found PDOStatement-getColumnMeta(). There's a big fat warning saying this is experimental, but you could take a look in case you need more data on the columns. Hope it helps. Right now, I have something like this: foreach ($objRecord as $record) { //we will have a new line. $xls_file .=\n; foreach ($record as $column=$value) { $xls_file .= $value.\t; } } The only thing I need now, is to put on top, the column names, and I have myself a xls file. YUpii!! I just need the column names... I will take a look on those functions you have mentioned. Thanks a lot, :) Márcio -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Newbie: Array of objects iteration
Right now, I have something like this: foreach ($objRecord as $record) { //we will have a new line. $xls_file .=\n; foreach ($record as $column=$value) { $xls_file .= $value.\t; } } The only thing I need now, is to put on top, the column names, and I have myself a xls file. YUpii!! I just need the column names... I will take a look on those functions you have mentioned. Thanks a lot, :) Márcio Update: PDO::FETCH_OBJ Returns an anonymous object with property names that correspond to the column names returned in your result set. I will give a shot on array_keys however, is there any method to access the PROPERTIES NAMES of a given object? Regards, M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Newbie: Array of objects iteration
Dear all, it's done. Can I call your help for the remain issues please, I'm sure they are easy to explain, and at some extend, common: http://pastebin.com/m691d3e66 Instead of saving to the database, if I do a print_r or a var_dump, I get the charsets quite ok. However, they do not appear properly in the xls file. I've tried to use utf8_encode but I was not sure where to apply it, and I end up with the same strange chars on the .xls file. If I write a meta tag containing charset information on the script that generates the file, I get headers already send message. I've tried to add Content-type: application/x-msdownload; *charset=UTF-8;* but no success either... Would a proper use of utf8_encode solve this? If so, where? Thanks in advance, M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Newbie: Array of objects iteration
Done. print utf8_decode($xls_file); Regards, Márcio -Original Message- From: MEM [mailto:tal...@gmail.com] Sent: sexta-feira, 9 de Outubro de 2009 18:19 To: 'MEM'; 'Fernando Castillo Aparicio'; 'Lester Caine'; 'php- gene...@lists.php.net' Subject: RE: [PHP] Newbie: Array of objects iteration Dear all, it's done. Can I call your help for the remain issues please, I'm sure they are easy to explain, and at some extend, common: http://pastebin.com/m691d3e66 Instead of saving to the database, if I do a print_r or a var_dump, I get the charsets quite ok. However, they do not appear properly in the xls file. I've tried to use utf8_encode but I was not sure where to apply it, and I end up with the same strange chars on the .xls file. If I write a meta tag containing charset information on the script that generates the file, I get headers already send message. I've tried to add Content-type: application/x-msdownload; *charset=UTF- 8;* but no success either... Would a proper use of utf8_encode solve this? If so, where? Thanks in advance, M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie: Array of objects iteration
- Original Message From: MEM tal...@gmail.com To: MEM tal...@gmail.com; Fernando Castillo Aparicio f_c_a_1...@yahoo.es; Lester Caine les...@lsces.co.uk; php-general@lists.php.net Sent: Fri, October 9, 2009 6:22:05 AM Subject: RE: [PHP] Newbie: Array of objects iteration Right now, I have something like this: foreach ($objRecord as $record) { //we will have a new line. $xls_file .=\n; foreach ($record as $column=$value) { $xls_file .= $value.\t; } } The only thing I need now, is to put on top, the column names, and I have myself a xls file. YUpii!! I just need the column names... I will take a look on those functions you have mentioned. Thanks a lot, :) Márcio Update: PDO::FETCH_OBJ Returns an anonymous object with property names that correspond to the column names returned in your result set. I will give a shot on array_keys however, is there any method to access the PROPERTIES NAMES of a given object? MEM, http://www.php.net/language.oop5.reflection Regards, Tommy Regards, M. -- 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