CodeIgniter Forums
Why crash my code? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Why crash my code? (/thread-75649.html)

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() {
    
$user_id $this->_check_login();

    
$this->db->select('*');
    
$carts $this->db->where('user_id',$user_id)->get('cart')->result_array();

    for (
$i $i count($carts) ; $i++) {
    
$pro_info   =   @$this->db->select('price,title')->where('pid',$carts['product_id'])->get('product')->row();
    
$carts[$i]['price'] =   $pro_info->price;
    
$carts[$i]['title'] =   $pro_info->title;
    }

    
$header['category'] =   $this->get_category();
    
$data['cat_id']     =   '';

    
$title  =   'carts';

    
$data['list'] = $carts;

    if (
$user_id != '') {
    
$info  = @$this->db->where('pid',$user_id)->get('user')->row();
    
$data['post']           =   $info->post_price;
    }

    
$data['title']          =   $title;
    
$data['tax']            =   0;

    
$this->load->view('site/master/header',$header);
    
$this->load->view('site/carts',$data);
    
$this->load->view('site/master/footer');

}

   function 
_check_login() {

        
$user_id    =   $this->session->userdata('user_id');

        if (!
preg_match('/^\d+$/',$user_id)) {
            
$this->session->set_flashdata('message','login to app<br>'.'<a class="btn btn-success" href="login">login</a>');
            
redirect('https://beautyfly.com/');
        }

        return 
$user_id;

    } 



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.
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.

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,

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.

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?

Also you should get the cart count into a variable then use the variable in the 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
/*
 * This is an array with some data we want to modify
 * when running through the for loop.
 */
$people = array(
    array('name' => 'Kalle''salt' => 856412),
    array('name' => 'Pierre''salt' => 215863)
);

for(
$i 0$i count($people); ++$i) {
    $people[$i]['salt'] = mt_rand(000000999999);


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
/*
 * This is an array with some data we want to modify
 * when running through the for loop.
 */
$people = array(
    array('name' => 'Kalle''salt' => 856412),
    array('name' => 'Pierre''salt' => 215863)
);

$count count($people);

for(
$i 0$i $count; ++$i) {
    $people[$i]['salt'] = mt_rand(000000999999);