CodeIgniter Forums

Full Version: problem with database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Bigil Michael[/eluser]
Quote:hi friends......

I want to know is it possible to limit the number of insertions into the table?????

i want to insert maximum 10 notes into a table
can anyone help me?

thanks in advance.........

El Forum

[eluser]Near[/eluser]
try to put the insert query inside a FOR LOOP

Code:
for($i =0;$i<=10;$i++)
{
//insert query here
}


untested

El Forum

[eluser]pickupman[/eluser]
Are you wanting only ten notes a time can be posted, or are you wanting a rolling maximum of 10 (say there is already 10, you add one more, the oldest is deleted, and the newest is inserted)?

El Forum

[eluser]Bigil Michael[/eluser]
Quote:this is my table structure

id complaint_id message
1 c1001 test1
2 c1001 test2
i want to limit complaint
here complaint_id is 'c1001'
maximum 10 'c1001' are inserted into my table.
sorry for my bad english....

El Forum

[eluser]Near[/eluser]
you can first fetch the id in descending order.
check if the id is lower than 10. if it is. then execute the query if greater than 10 send an error message...

El Forum

[eluser]Bigil Michael[/eluser]
Thank you so much
my problem solved

El Forum

[eluser]boltsabre[/eluser]
This will only work if 'c1001' is the only thing ever inserted into your complaint_id column... if you have other stuff in there (maybe some 'c1002', c'2001', or even a blank/null value, etc), then this method will not work.

If so, then you will want to first do a query of your table and get the count of the number of times 'c1001' already exists. Something like this (depending on how your doing your db stuff, native sql, active records, or whatever it is, you'll have to ammend the below code to suit, but you get the idea!):

Code:
$checkCount = "SELECT count(*) FROM 'theTableName' WHERE complaint_id = 'c1001'";

Then run this thru and if statement:

Code:
if ($checkCount < 11){ //is less than or equal to 10
   //do your stuff here
}else{
   //else there are already 10 'c1001' records, do something else, perhaps output a message, or redirect user
}

Hope that helps

El Forum

[eluser]boltsabre[/eluser]
@ Mbet:

Your first suggestion to run it through a counter and run a insert query is a really bad method (I'm not picking on your or anything, just trying to share some knowledge!!!). If you did this, then you are 'hitting' the DB 10 times for one 'action'. Not soooo bad in this case, but if you had a query that involved a counter of 1000 then this becomes a Major problem (or a counter of 10 like we have here and 10 users every minute doing something that will call this to run, that'd would be 100 hits to the DB per minute instead of 10)

If you really had to use a counter for some DB method, it's much better to use your counter to build a String and then once the counter if finished, pass that string to your DB call (perhaps the string is 10 different Id's seperated by commas, or 10 comments to insert into an autoincrementing table, etc, etc, you get my point).

Happy coding guys!!!