![]() |
problem with database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: problem with database (/showthread.php?tid=44287) |
problem with database - El Forum - 08-10-2011 [eluser]Bigil Michael[/eluser] Quote:hi friends...... problem with database - El Forum - 08-10-2011 [eluser]Near[/eluser] try to put the insert query inside a FOR LOOP Code: for($i =0;$i<=10;$i++) untested problem with database - El Forum - 08-10-2011 [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)? problem with database - El Forum - 08-11-2011 [eluser]Bigil Michael[/eluser] Quote:this is my table structure problem with database - El Forum - 08-11-2011 [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... problem with database - El Forum - 08-11-2011 [eluser]Bigil Michael[/eluser] Thank you so much my problem solved problem with database - El Forum - 08-11-2011 [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 problem with database - El Forum - 08-11-2011 [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!!! |