![]() |
Why crash my code? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Why crash my code? (/showthread.php?tid=75649) Pages:
1
2
|
Why crash my code? - omid_student - 03-01-2020 Hi My codes is very simple But when i run it,cpu usage get over 96% PHP Code: function carts() { RE: Why crash my code? - jreklund - 03-01-2020 It's not simple, you are making X times pid query when you should be making 1. If you have 1000 products, you will be making 1000 sub queries to determine the price. Make an where_in with all your product ids. RE: Why crash my code? - omid_student - 03-01-2020 (03-01-2020, 03:00 AM)jreklund Wrote: It's not simple, you are making X times pid query when you should be making 1. Thank you but i understood problem is other things RE: Why crash my code? - zahhar - 03-02-2020 Hi, You code is a bit suboptimal. I would check a couple of things: 1) Do you have indexes on cart.user_id field, on product.pid and on user.pid fields? 2) How much records do you expect $carts[] array has? Probably, instead of N separate queries woul be beter to implode() your $cart['prodict_id'] nd get them in one shot using SQL IN() clause? 3) ensure $this->get_category() works quick enough and there is nothing slow inside it? 4) don't use @-signs to supress any messages, as they might be vital for your exercies. remove @s and make sure your code works well without them. 5) side note: preg_match is overkill to check for $user_id. You can easilly go with isset() and (int)$user_id>0 for clarity. RE: Why crash my code? - omid_student - 03-02-2020 (03-02-2020, 01:42 AM)zahhar Wrote: Hi, Thank you i try to trace it and let to know you RE: Why crash my code? - tweenietomatoes - 03-02-2020 I didn't understand why you're using SQL query under for loop. Try to solve this problem in SQL query instead of PHP. for ($i = 0 ; $i < count($carts) ; $i++) { ... } RE: Why crash my code? - InsiteFX - 03-02-2020 Why are you using the @ error specifier in a for loop? Also you should get the cart count into a variable then use the variable in the for loop. RE: Why crash my code? - omid_student - 03-02-2020 (03-02-2020, 02:02 PM)InsiteFX Wrote: Why are you using the @ error specifier in a for loop? I guest may be product dont exist in my database What is different between use count in loop and without use in loop? RE: Why crash my code? - nonebeliever - 03-03-2020 Just a guess. You are calling get category without an argument which results in an endless loop. RE: Why crash my code? - InsiteFX - 03-03-2020 Using count in the loop it is called each iteration it loops through the code. Example from php.net: PHP Code: <?php The above code can be slow, because the array size is fetched on every iteration. Since the size never changes, the loop be easily optimized by using an intermediate variable to store the size instead of repeatedly calling count(): This is the correct way of doing it. PHP Code: <?php |