Welcome Guest, Not a member yet? Register   Sign In
Why crash my code?
#1

(This post was last modified: 03-01-2020, 01:18 AM by omid_student.)

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;

    } 
Reply
#2

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.
Reply
#3

(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
Reply
#4

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.
Reply
#5

(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
Reply
#6

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++) {
...
}
Reply
#7

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 )
Reply
#8

(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?
Reply
#9

Just a guess. You are calling get category without an argument which results in an endless loop.
Reply
#10

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);

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB