CodeIgniter Forums

Full Version: Inserting data for each user
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hey guys, i have a quick question.

I am writing an application, and part of it requires me to be able to send mass pm to each user.
The way pm system works is by splitting a message into 2 parts, part A consisting of pm id, message id, which user it is for and whether its read or not. Part B actually consists of the message, subject time etc. (Part A and B are stored in separate tables)

Now i am doing mass pm system that admins can use, so what ive got so far is that when a mass pm is sent it creates 1 B messages and what i want to do is send each user an A message. I know i can do a foreach loop and then insert data for each user but i am wondering if there is a quicker and more efficient way to insert the date for each user.

El Forum

look at $this->db->insert_batch(); (

You see it can insert from an array.

so, Table A has:
- pm id (I assume pk, autoincrement - you don't need to generate)
- message id - your pk from table B?
- user id
- status - unread for everybody


Insert to Table B first and get your insert_id().

Then query user table with:

"SELECT ".insert_id. ", user_id, 'unread' FROM users ";

This will return an array that you should be able to just insert_batch() straight into your Table A


I should have added that you are going to need to look at how many users you have, how big the array will be and how long the batch insert sql strings is, etc. and modify for your needs. Don't just blindly use this method.

El Forum

thanks mate, was exactly what i was after.