php-general Digest 1 Mar 2011 16:03:20 -0000 Issue 7205
php-general Digest 1 Mar 2011 16:03:20 - Issue 7205 Topics (messages 311597 through 311607): Sorting an array 311597 by: Ron Piggott 311599 by: Simon J Welsh 311605 by: David Robley 311606 by: FeIn 311607 by: Jim Lucas Implementing strict types 311598 by: Paul McGarry 311604 by: Richard Quadling Re: very quick Regex query 311600 by: David Hutto 311601 by: David Hutto 311602 by: Ashley Sheridan 311603 by: Richard Quadling Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #’s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron The Verse of the Day “Encouragement from God’s Word” http://www.TheVerseOfTheDay.info ---End Message--- ---BeginMessage--- On 1/03/2011, at 4:52 PM, Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #’s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron The Verse of the Day “Encouragement from God’s Word” http://www.TheVerseOfTheDay.info Have a look at asort() (http://php.net/asort), which is used to sort arrays but maintain the key association. The default search option should work fine for you, if not try one of the other SORT_* flags described on the sort() manual page. --- Simon Welsh Admin of http://simon.geek.nz/ Who said Microsoft never created a bug-free program? The blue screen never, ever crashes! http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e ---End Message--- ---BeginMessage--- Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #?s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron Like the man said - asort. May I recommend you to http://php.net where you will find the answer to most of your queries, simply by looking under a generic area, such as array (http://php.net/array) for this particular problem. Surely you have been around here long enough to be able to find things in the documentation, or at least try there first, by now? Cheers -- David Robley Do fish get thirsty? Today is Setting Orange, the 60th day of Chaos in the YOLD 3177. ---End Message--- ---BeginMessage--- Also check http://www.php.net/manual/en/function.natsort.php On Tue, Mar 1, 2011 at 1:39 PM, David Robley robl...@aapt.net.au wrote: Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #?s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron Like the man said - asort. May I recommend you to http://php.net where you will find the answer to most of your queries, simply by looking under a generic area, such as array (http://php.net/array) for this particular problem. Surely you have been around here long enough to be able to find things in the documentation, or at least try there first, by now? Cheers
Re: [PHP] Implementing strict types
On 1 March 2011 04:40, Paul McGarry p...@paulmcgarry.com wrote: I am looking at implementing something roughly equivalent to strict typing for a part of my code. Has anybody else had a go at such a thing? I have some data types which I'm implementing as classes (base types like int but also more specific types like country) and I'd like to be able to use them as normal variables (as far as assigning and retrieving it's value goes) with 'magic' happening inside the class for validation and normalisation (eg accepting any ISO style country code on assignment but standardising on alpha 2 on output). I am aware of the capability to override the assignment operator for a property of an object, ie use the __set() magic method so that $object-var='hello'; is equivalent to: $object-__set('var','hello'); allowing the __set() method to do some funky stuff. Is there a similar capability to override the assignment operator on the object itself, ie have: $object='hello'; handled by a method on the $object? Similarly the __toString() magic method would cover part of the getter overriding, but it seems to be a special case override that only works within specific string functions rather than a generic getter override. Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php There is __invoke, but that is for ... $instance() The problem is (hopefully) explained in ... $instance = 'something that needs to be passed to instance and not assigned to instance'; How do you differentiate between the two assignments (one to the variable and one to be processed by the instance) ? Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] very quick Regex query
On 1 March 2011 03:07, Alexis phplis...@antonakis.co.uk wrote: Hi, I know not strictly a PHP question, but was just wondering if someone can tell me the regex format for something please. I need to replace .. with That is six periods replaced by eight hyphens. Many thanks Alexis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php $text = preg_replace('/\.{6}/', '', $text) but $text = str_replace('..', '', $text); is going to quicker. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Sorting an array
Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #?s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron Like the man said - asort. May I recommend you to http://php.net where you will find the answer to most of your queries, simply by looking under a generic area, such as array (http://php.net/array) for this particular problem. Surely you have been around here long enough to be able to find things in the documentation, or at least try there first, by now? Cheers -- David Robley Do fish get thirsty? Today is Setting Orange, the 60th day of Chaos in the YOLD 3177. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Sorting an array
Also check http://www.php.net/manual/en/function.natsort.php On Tue, Mar 1, 2011 at 1:39 PM, David Robley robl...@aapt.net.au wrote: Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #?s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron Like the man said - asort. May I recommend you to http://php.net where you will find the answer to most of your queries, simply by looking under a generic area, such as array (http://php.net/array) for this particular problem. Surely you have been around here long enough to be able to find things in the documentation, or at least try there first, by now? Cheers -- David Robley Do fish get thirsty? Today is Setting Orange, the 60th day of Chaos in the YOLD 3177. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array
On 2/28/2011 7:52 PM, Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #’s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron The Verse of the Day “Encouragement from God’s Word” http://www.TheVerseOfTheDay.info Besides the answer others have pointed you to, I would recommend doing it in mysql? SELECT * FROM table ORDER BY your_column; Check out the syntax for it here. http://dev.mysql.com/doc/refman/5.0/en/select.html and http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help! Made a boo-boo encrypting credit cards
I just wanted to ping this, as it's becoming a serious problem. I hope someone can help. On Feb 11, 2011, at 2:42 PM, Brian Dunning wrote: Hey all - I'm using mcrypt to store credit cards into MySQL. About 90% of them decrypt fine, but about 10% decrypt as nonsense (b1�\�JEÚU�A��� is a good example). Maybe there is a character that appears in about 10% of my encryptions that's not being encoded properly??? // Encryption is set up at the top of the script: $crypto = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypto), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($crypto); $key = substr(md5('my_funky_term'), 0, $ks); // When the card number is collected by the form, it's encrypted: $cc_number = addslashes($_POST['cc_number']); mcrypt_generic_init($crypto, $key, $iv); $cc_encrypt = mcrypt_generic($crypto, $cc_number); mcrypt_generic_deinit($crypto); // This is written to the database: $query = update accounts set cc_encrypt='$cc_encrypt', encrypt_iv='$iv', other_fields='$other_stuff' where id='$account_id' limit 1; $result = mysql_query($query) or die(mysql_error()); Both the cc_encrypt and encrypt_iv fields are tinytext, latin1_swedish_ci, MyISAM, MySQL 5.0.91 In another script, when I retrieve, I first set it up at the top of the script exactly like step #1 above, then retrieve it like this: mcrypt_generic_init($crypto, $key, $row['encrypt_iv']); $cc_number = trim(mdecrypt_generic($crypto, $row['cc_encrypt'])); mcrypt_generic_deinit($crypto); Most of them are good, a few of them are bad. Can anyone see anything I'm doing wrong or a case I'm not covering? Thanks much. -- 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] Help! Made a boo-boo encrypting credit cards
On Tue, Mar 1, 2011 at 12:34 PM, Brian Dunning br...@briandunning.com wrote: I just wanted to ping this, as it's becoming a serious problem. I hope someone can help. On Feb 11, 2011, at 2:42 PM, Brian Dunning wrote: Hey all - I'm using mcrypt to store credit cards into MySQL. About 90% of them decrypt fine, but about 10% decrypt as nonsense (b1�\�JEÚU�A��� is a good example). Maybe there is a character that appears in about 10% of my encryptions that's not being encoded properly??? // Encryption is set up at the top of the script: $crypto = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypto), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($crypto); $key = substr(md5('my_funky_term'), 0, $ks); // When the card number is collected by the form, it's encrypted: $cc_number = addslashes($_POST['cc_number']); mcrypt_generic_init($crypto, $key, $iv); $cc_encrypt = mcrypt_generic($crypto, $cc_number); mcrypt_generic_deinit($crypto); // This is written to the database: $query = update accounts set cc_encrypt='$cc_encrypt', encrypt_iv='$iv', other_fields='$other_stuff' where id='$account_id' limit 1; $result = mysql_query($query) or die(mysql_error()); Both the cc_encrypt and encrypt_iv fields are tinytext, latin1_swedish_ci, MyISAM, MySQL 5.0.91 In another script, when I retrieve, I first set it up at the top of the script exactly like step #1 above, then retrieve it like this: mcrypt_generic_init($crypto, $key, $row['encrypt_iv']); $cc_number = trim(mdecrypt_generic($crypto, $row['cc_encrypt'])); mcrypt_generic_deinit($crypto); Most of them are good, a few of them are bad. Can anyone see anything I'm doing wrong or a case I'm not covering? Thanks much. -- 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 Could it be that the addslashes is creating a \0 (null) value? That might screw up the decryption routine. -- Bastien Cat, the other other white meat -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help! Made a boo-boo encrypting credit cards
On Fri, Feb 11, 2011 at 4:42 PM, Brian Dunning br...@briandunning.com wrote: Hey all - I'm using mcrypt to store credit cards into MySQL. About 90% of them decrypt fine, but about 10% decrypt as nonsense (b1�\�JEÚU�A��� is a good example). Maybe there is a character that appears in about 10% of my encryptions that's not being encoded properly??? // Encryption is set up at the top of the script: $crypto = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypto), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($crypto); $key = substr(md5('my_funky_term'), 0, $ks); // When the card number is collected by the form, it's encrypted: $cc_number = addslashes($_POST['cc_number']); mcrypt_generic_init($crypto, $key, $iv); $cc_encrypt = mcrypt_generic($crypto, $cc_number); mcrypt_generic_deinit($crypto); // This is written to the database: $query = update accounts set cc_encrypt='$cc_encrypt', encrypt_iv='$iv', other_fields='$other_stuff' where id='$account_id' limit 1; $result = mysql_query($query) or die(mysql_error()); Both the cc_encrypt and encrypt_iv fields are tinytext, latin1_swedish_ci, MyISAM, MySQL 5.0.91 In another script, when I retrieve, I first set it up at the top of the script exactly like step #1 above, then retrieve it like this: mcrypt_generic_init($crypto, $key, $row['encrypt_iv']); $cc_number = trim(mdecrypt_generic($crypto, $row['cc_encrypt'])); mcrypt_generic_deinit($crypto); Most of them are good, a few of them are bad. Can anyone see anything I'm doing wrong or a case I'm not covering? Thanks much. Just a WAG, but when I first was working with mcrypt, it would append spaces to the encrypted value. I would have to TRIM() everything for processing or decryption. BTW, we also elected *not* to store card numbers, only the last 4 digits. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Sorting an array
That or do it in mysql before you get the data back, its also pretty good at sorting, you know ;) -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. FeIn aci...@gmail.com wrote: Also check http://www.php.net/manual/en/function.natsort.php On Tue, Mar 1, 2011 at 1:39 PM, David Robley robl...@aapt.net.au wrote: Ron Piggott wrote: I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = Punished [98] = Sanctuary [187] = Sin [596] = Sing ) The #?s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron Like the man said - asort. May I recommend you to http://php.net where you will find the answer to most of your queries, simply by looking under a generic area, such as array (http://php.net/array) for this particular problem. Surely you have been around here long enough to be able to find things in the documentation, or at least try there first, by now? Cheers -- David Robley Do fish get thirsty? Today is Setting Orange, the 60th day of Chaos in the YOLD 3177.-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Help needed with mysql import
Dear all, I am trying to make a website with php and I found the following code in a book and I am trying to import it. The following are the beginning of the file i am trying to import with the command mysql -u root -pmypassword certainty dump I get the following error : ERROR 1067 (42000) at line 9: Invalid default value for 'id' but when I see line 9 i see the value '0' for id which seems ok to me, I also tried removing the quotes but same error. Can someone guide me ? Thank you, Ashim # MySQL dump 7.1 # # Host: [host deleted] Database: certainty # # Server version 3.22.32 # # Table structure for table 'high_scores' # CREATE TABLE high_scores ( id int(11) DEFAULT '0' NOT NULL auto_increment, name varchar(30), answer_count int(11), credit double(16,4), PRIMARY KEY (id) );
Re: [PHP] Help needed with mysql import
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 03/02/2011 07:56 AM, Ashim Kapoor wrote: Dear all, I am trying to make a website with php and I found the following code in a book and I am trying to import it. The following are the beginning of the file i am trying to import with the command mysql -u root -pmypassword certainty dump I get the following error : ERROR 1067 (42000) at line 9: Invalid default value for 'id' but when I see line 9 i see the value '0' for id which seems ok to me, I also tried removing the quotes but same error. Can someone guide me ? Thank you, Ashim # MySQL dump 7.1 # # Host: [host deleted] Database: certainty # # Server version 3.22.32 # # Table structure for table 'high_scores' # CREATE TABLE high_scores ( id int(11) DEFAULT '0' NOT NULL auto_increment, name varchar(30), answer_count int(11), credit double(16,4), PRIMARY KEY (id) ); It's not really a PHP question. But here goes. Your first field id is an auto_increment field this means the counter goes up by every insert. Normally this will start at 1 not 0. So either change the 0 to a higher number or remove the auto_increment part before you import change the 0 after and alter the table to put back auto_increment -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAEBAgAGBQJNbfOvAAoJEMffsHAOnubXC7UP/1k5qA4TDxDnUKrYZLV/rl9s bLRPTQ21riFMIzt0ne14No4+MTwjNtfVAdSnjpCWEZP+Y2MEixaiz8gIcOt9GIOD f9QPJZFEIcVADs3lqeS88eqdgRBNiYy3x2PHyslR3jtuaeFrRvxOLBTgBISq6Ih4 Dd5nRCbo6WObQ5e26HhbDeMJDAnOw4iQMjpoxc6UD9syxkJrORYw6XFvEmJA/QNF RDTNIO7P62ROamGor8urmPdfIemFLyqjD5YAQ64O6aWVHp0ehjO4l1xPWCeI84sV 2g8C3yqi06UjYOE8NHrf64VYcQtvLFkJbzGT6mmPwEP0gBdqX6o2YDwnudv7+APN F5zoVBv/7wygFaP+P0zgJ+EWVML35VfJFuq5VCH3CUk1hROS4X/JtsNXdVkAbaFA BpEhQ4jN0x/34HrI1cWjEUwaUuU6m9XoMIuO+1tQRLFatEW9I5z1c3hrJsPUNImX qSxEGLAZyA7tex++4YFn8DZXWz4mdllI7yejRe0nl1vl4Nn1+t2se/vF0TfZAGdB HgDeUWTdY/N2KeT4z9gPjGEDlRp8Wqo13Sv1yVhzWDdAJQdWaH8+Kk0GCI0jBrgT Pthmjr0e4bKCW19SJtL7/mTRU12qX/kbjMG5JqIh1ixn72qgqcvkTjgvEeQ1Y0DM xBBFUUedwoKevRJI05/2 =mdri -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php