du94414 2014-07-25 21:29
浏览 25
已采纳

我的一个类方法返回不正确的值

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.

  • 写回答

1条回答 默认 最新

  • duai5344 2014-07-25 21:43
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊