I can't decide on w/c technique to go w/... although I am tempted to go the Array way as it is much more straight forward and easier/cleaner to code...
lemme explain in a simple way..
For my messaging app, I have 2 tables
- Contacts - contains name, and phone number (currently 5,000+ records)
- Messages - messages and the phone number, but not the name of the member
I need to list 1000 Messages, w/ the corresponding phone number and name if possible...
eg.
- "Hi dude" - from 565-1111 (John)
- "Bring Cheese" - from 565-2222 (Bieber)
- "Hehehe " - from 565-2332
- "iron man is cool!" - from 565-7748 (Arnie)
etc...
I would normally join contacts to messages via the phone number... pretty standard stuff... (but in reality my queries are a bit more complex than a simple left join)
Now, i've been playing around w/ the idea of fetching all contacts and putting it into an array where my app/system could use it in a much easier , more straight forward and cleaner code... where:
- query
SELECT * contacts
and store them into an array:
contacts['555-7748'] = "John";
contacts['555-1111'] = "Joe";
contacts['555-2233'] = "Borat";
contacts['555-4234'] = "Arnie";
etc
this way i can easily lookup the NAME of the Contact via accessing the array, many times in my page/script, just by simply doing:
$name = contacts[$phoneNumber];
I have actually implemented this and it's really cool .. no more join headaches and re-querying contacts table etc..
now the question is.. is this OK? Practical? or could i be hitting some memory cap or something here?
it's basically running JOIN query on EACH message my script outputs or store ALL THE CONTACTS/RECORDS into array (memory) and refer to that instead, making queries are more simple.. my only worry is, querying thousands of records into an array, may be a bit too much? or is this really OK/practical? specially since there are times when my script needs to fetch specific contacts more than once...
To add, I was also thinking of storing that array into a Session variable! lolz.. so my scripts can simply share this data.. lol
looking forward to some good inputs Thanks guys.
---- UPDATE as of 03-31-2013 (Sunday) ----
Ok... I'm thinking about a totally different approach now..
1) SINCE all this is really me needing to show the "CONTACT NAME" for each message for neat/presentation purposes (better to see the name of the contact on a message rather than phone number yeh?) ....
2) and since each message will need to show 2 contact names (receiver and sender) based on their phone numbers on the MESSAGES table.....
3) I now am thinking of ADDING 2 fields into the MESSAGES table instead... "sender_name" and "receiver_name" .. I have seen Bulletin board / Forum software use this technique a lot ..
4) when system receives a message , it checks for existing Contacts entry of the numbers associated w/ the message...and if it exists, get the respective names and include it into storage/insert
5) ..problem is if during the message was received, contact info isnt available, so no NAMES are saved.. and later CONTACT info is then created... the OLD messages wont show the contact names as intended...
6) 3 way solution... 1st, evertime a new contact is added/changed my php will make updates to all messages w/ the matching numbers... 2nd, from time to time perhaps once / month, run a "synchronize routine" to make sure things are in sync.. or, when a user Actually OPENS a message, mandatory routine will run to check if contact name of that particular message is still in sync w/ the contacts table information... and make updates as necessary..
hmmm... better approach you thing? this way, i just need to issue straight forward SELECT queries , simple, fast.. but am able to display the names..
then again, i still kinda find that QUICK array reference thing so cool though.. lolz
- indecisive