Why crash my code? |
Hi
My codes is very simple But when i run it,cpu usage get over 96% PHP Code: function carts() {
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.
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.
(03-02-2020, 01:42 AM)zahhar Wrote: Hi, Thank you i try to trace it and let to know you
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++) { ... }
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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?
Just a guess. You are calling get category without an argument which results in an endless loop.
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 What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |