[PHP] Sorting, sort of
I have a 'name' field in a MySQL table that contains people's first and last names, i.e. John Smith - but some people may choose not to include their last name, so they're just John in the table, or they may choose to include themselves and someone else, but not the last name, John and Jane. I'm thinking the easiest way to sort by last name, which is what I want to do, is to just go into MySQL and make 'first_name' and 'last_name' fields, but if there's a way to do this with PHP that'd be great, so then I wouldn't have to mess with the table and data. Anyhow, I've looked up sort() and asort() in the PHP manual, but I'm not sure how I'd go about this. I assume I'd use some kind of function to read the 'name' field to the first space, take what it finds after the space and put it into an array, then have some if/else statement to deal with the lack of a space or the presence of multiple spaces, then use sort() on that array. Just looking for some guidance, maybe a specific function or bit of code. Anything that'd help. Or if this would be more easily addressed by reconfiguring my MySQL table, just let me know. Thanks, Jason Soza -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting, sort of
On Mon, May 06, 2002 at 11:25:03AM -0800, Jason Soza wrote: I have a 'name' field in a MySQL table that contains people's first and last names, ... snip ... I'm thinking the easiest way to sort by last name, which is what I want to do, is to just go into MySQL and make 'first_name' and 'last_name' fields, but if there's a way to do this with PHP that'd be great No, it wouldn't. You'd be wasting a lot of time and memory. Do the job right. Separate the fields. If you already have a large dataset, you can use PHP to do the initial separation, but it'll be a pain. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting, sort of
You _definetly_ want to do this in your database, not with PHP and arrays. Databases are made to do this kind of sorting for you. I would modify your table first, adding in another column for first_name, then modify your form so that all data going in from now on is correct. The hard part will be getting the data you have now into the correct format. How big of a table are we talking? ---John Holmes... - Original Message - From: Jason Soza [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, May 06, 2002 3:25 PM Subject: [PHP] Sorting, sort of I have a 'name' field in a MySQL table that contains people's first and last names, i.e. John Smith - but some people may choose not to include their last name, so they're just John in the table, or they may choose to include themselves and someone else, but not the last name, John and Jane. I'm thinking the easiest way to sort by last name, which is what I want to do, is to just go into MySQL and make 'first_name' and 'last_name' fields, but if there's a way to do this with PHP that'd be great, so then I wouldn't have to mess with the table and data. Anyhow, I've looked up sort() and asort() in the PHP manual, but I'm not sure how I'd go about this. I assume I'd use some kind of function to read the 'name' field to the first space, take what it finds after the space and put it into an array, then have some if/else statement to deal with the lack of a space or the presence of multiple spaces, then use sort() on that array. Just looking for some guidance, maybe a specific function or bit of code. Anything that'd help. Or if this would be more easily addressed by reconfiguring my MySQL table, just let me know. Thanks, Jason Soza -- 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] Sorting, sort of
Hi Jason, I'd just get it over with and make first_name and last_name fields in your table. It will be so much easier to sort by last name that way. I do it this way everytime. It won't take much time to implement either. Good luck, Tyler Longren Captain Jack Communications [EMAIL PROTECTED] www.captainjack.com - Original Message - From: Jason Soza [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, May 06, 2002 2:25 PM Subject: [PHP] Sorting, sort of I have a 'name' field in a MySQL table that contains people's first and last names, i.e. John Smith - but some people may choose not to include their last name, so they're just John in the table, or they may choose to include themselves and someone else, but not the last name, John and Jane. I'm thinking the easiest way to sort by last name, which is what I want to do, is to just go into MySQL and make 'first_name' and 'last_name' fields, but if there's a way to do this with PHP that'd be great, so then I wouldn't have to mess with the table and data. Anyhow, I've looked up sort() and asort() in the PHP manual, but I'm not sure how I'd go about this. I assume I'd use some kind of function to read the 'name' field to the first space, take what it finds after the space and put it into an array, then have some if/else statement to deal with the lack of a space or the presence of multiple spaces, then use sort() on that array. Just looking for some guidance, maybe a specific function or bit of code. Anything that'd help. Or if this would be more easily addressed by reconfiguring my MySQL table, just let me know. Thanks, Jason Soza -- 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] Sorting, sort of
It's actually not that large of a table, but it'll still be a PITA to get all of the data reinserted. I have about 50 records with the following fields: 'gen', 'year', 'color', 'name', 'email', 'location', 'misc', 'pic1', 'pi c2', 'pic3', 'pic4' So basically, I guess I want to rename 'name' to 'first_name', add another column for 'last name', then update each record to separate the first and last names into their respective columns, eh? I suppose the easiest way is to save/export the table as a tab- delimited txt file, make my first/last name changes that way, change the layout of the table, then update the table using the new txt file? Sorry for the newbie (and MySQL) questions - I'm not a programmer or DB admin by trade, just volunteered to make a website and got caught up with PHP and MySQL. I'm learning, really I am! (And having fun too!) Jason Soza - Original Message - From: 1LT John W. Holmes [EMAIL PROTECTED] Date: Monday, May 6, 2002 11:46 am Subject: Re: [PHP] Sorting, sort of You _definetly_ want to do this in your database, not with PHP and arrays.Databases are made to do this kind of sorting for you. I would modify your table first, adding in another column for first_name,then modify your form so that all data going in from now on is correct. The hard part will be getting the data you have now into the correct format. How big of a table are we talking? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting, sort of
Yeah, just dump it, edit it, and import it back into a table. 50 records shouldn't take you to long. You'll thank yourself later.. :) ---John Holmes... - Original Message - From: Jason Soza [EMAIL PROTECTED] To: 1LT John W. Holmes [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Monday, May 06, 2002 3:56 PM Subject: Re: [PHP] Sorting, sort of It's actually not that large of a table, but it'll still be a PITA to get all of the data reinserted. I have about 50 records with the following fields: 'gen', 'year', 'color', 'name', 'email', 'location', 'misc', 'pic1', 'pi c2', 'pic3', 'pic4' So basically, I guess I want to rename 'name' to 'first_name', add another column for 'last name', then update each record to separate the first and last names into their respective columns, eh? I suppose the easiest way is to save/export the table as a tab- delimited txt file, make my first/last name changes that way, change the layout of the table, then update the table using the new txt file? Sorry for the newbie (and MySQL) questions - I'm not a programmer or DB admin by trade, just volunteered to make a website and got caught up with PHP and MySQL. I'm learning, really I am! (And having fun too!) Jason Soza - Original Message - From: 1LT John W. Holmes [EMAIL PROTECTED] Date: Monday, May 6, 2002 11:46 am Subject: Re: [PHP] Sorting, sort of You _definetly_ want to do this in your database, not with PHP and arrays.Databases are made to do this kind of sorting for you. I would modify your table first, adding in another column for first_name,then modify your form so that all data going in from now on is correct. The hard part will be getting the data you have now into the correct format. How big of a table are we talking? ---John Holmes... -- 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] Sorting, sort of
-Original Message- I have about 50 records with the following fields: 'gen', 'year', 'color', 'name', 'email', 'location', 'misc', 'pic1', 'pi c2', 'pic3', 'pic4' -Original Message- If you're going to be changing your database, you might also consider what to do with 'picX'. As it stands you've got a max of four pics, but what if you later decide on a max of 5 or reducing it to 3? I'd actually separate them out to their own table and just have an ID reference that points them to your user data and the 'picX' data as the second field. The you can use a join to get all the information and you aren't limited by your database structure to any set number of 'pic' records. ...and now that I'm completely off-topic I think I'll finish here. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php