Farhan
Farhan

Reputation: 96

One of my class methods returns incorect values

I'm writing a plugin for wordpress and I have a class file like below:

class pgboxdata{
    protected $boxesdata;

    public function pgboxdata(){
        if(get_option('fixed_boxes_data')){
            $this->boxesdata = maybe_unserialize(get_option('fixed_boxes_data'));
        }
        else {
            $this->addbox('PG Fixed Box','500','300','pg-btn-tl','#fff','everywhere');
        }

    }

    public function addbox($title,$width,$height,$place,$backcolor,$page){
            $data = $this->boxesdata;
            $uniqueid;
            $uniqueid = $this->generate_id(); 
            $data []= array(
                        'theid' => $uniqueid,
                        'title' => $title,
                        'width' => $width,
                        'height' => $height,
                        'backcolor' => $backcolor,  
                        'btnpos' => $place,
                        'page' => $page
                        );
            $this->updateit($data);
    }

    function generate_id(){
        if(is_array($this->boxesdata)){
            $count = count($this->boxesdata);
            if($count > 0){
                $releasedid = "fixedboxarea".$count+1;
                return $releasedid;
            }
            else {
                $releasedid = "fixedboxarea".$count;
                return $releasedid;
            }
        }
        else
            return 0;
    }




    function updateit($data){
        if(is_array($data)){
            $this->boxesdata = $data;
            update_option('fixed_boxes_data' , maybe_serialize($data));
            return true;
        }
    }



}

in my plugin I create new object of this class and I call addbox like below:

$datasender = new pgboxdata();              
$datasender->addbox($_POST['box_name'],$_POST['box_width'],$_POST['box_height'],$_POST['btnplace'],$_POST['bgcolor'],$_POST['selectedplace']);

but the problem is that first time method generate_id returns correct value (returns : fixedboxarea0) but from second time it always returns 1!
Can anyone tell me why this happens? the reason of using function generate_id is that I want a unique id for every box with that specified prefix, but even I don't know that method is correct way for doing this or not.

Upvotes: 0

Views: 31

Answers (1)

lsouza
lsouza

Reputation: 2488

That's because you're incrementing the $count var along with a string concatenation. The correct expression would be this:

$releasedid = "fixedboxarea" . ($count + 1);

Upvotes: 1

Related Questions