使用Codeigniter和Active Record查询动态构建表单

时间:2012-11-02 20:25:34

标签: php codeigniter activerecord form-generator

模型

    function get_prices() 
    {
        $table_by_product = 'printer_businesscards'; //replace with URI Segment

        //Get all the columns in the table set by the page URI of $table_by_product variable 
        $get_all_col_names = $this->db->list_fields($table_by_product);

        //Loop through the column names. All names starting with 'O_' are optional fields for the 
        //current product. Get all Distinct values and create a radio button list in form.
        foreach ($get_all_col_names as $key => $value) {
            //get all o_types for the product by column name
            if ($all_O_types = preg_match('/O_/', $value)) 
                {
                $O_types = array($value);
                foreach ($O_types as $O) {
                        //echo $O;
                        $this->db->select($O);
                        $this->db->distinct();
                        $qO = $this->db->get($table_by_product);
                        $qO_Array = $qO->result_object();
                    }
                } 
            //Get all x_types for the product by column name. All 'X_' types are specific product options.
            //Create a dropdown menu with all DISTINCT product options.
            if ($all_X_types = preg_match('/X_/', $value)) 
                {
                $X_types = array($value);
                    foreach ($X_types as $X) {
                        //echo $X;
                        $this->db->select($X);
                        $this->db->distinct();
                        $qX = $this->db->get($table_by_product);
                        $qX_Array = $qX->result_object();
                    }
                }       
        }
        return array($qX_Array,$qO_Array);
    }
}

因此每个产品都有不同的选项,但所有产品选项都以“X_”或“O_”为前缀。我需要获取“X_”和“O_”的每个COLUMN的DISTINCT值,并且在任何视图中我需要构建具有这些值的表单。这是一个数组:

array (size=3)
  0 => 
    object(stdClass)[19]
      public 'X_SIZE' => string '1.75x3' (length=6)
  1 => 
    object(stdClass)[20]
      public 'X_SIZE' => string '1.75x3.5(slim)' (length=14)
  2 => 
    object(stdClass)[21]
      public 'X_SIZE' => string '2x3' (length=3)
array (size=3)
  0 => 
    object(stdClass)[17]
      public 'X_PAPER' => string '14ptGlossCoatedCoverwithUV(C2S)' (length=31)
  1 => 
    object(stdClass)[18]
      public 'X_PAPER' => string '14ptPremiumUncoatedCover' (length=24)
  2 => 
    object(stdClass)[24]
      public 'X_PAPER' => string '16ptDullCoverwithMatteFinish' (length=28)
array (size=2)
  0 => 
    object(stdClass)[23]
      public 'X_COLOR' => string '1000' (length=4)
  1 => 
    object(stdClass)[22]
      public 'X_COLOR' => string '1002' (length=4)
array (size=4)
  0 => 
    object(stdClass)[20]
      public 'X_QTY' => string '100' (length=3)
  1 => 
    object(stdClass)[21]
      public 'X_QTY' => string '250' (length=3)
  2 => 
    object(stdClass)[17]
      public 'X_QTY' => string '500' (length=3)
  3 => 
    object(stdClass)[19]
      public 'X_QTY' => string '1000' (length=4)
array (size=3)
  0 => 
    object(stdClass)[25]
      public 'O_RC' => string 'YES' (length=3)
  1 => 
    object(stdClass)[26]
      public 'O_RC' => string 'NO' (length=2)
  2 => 
    object(stdClass)[27]
      public 'O_RC' => string 'NA' (length=2) 

我当前的MODEL仅向我的观点返回X_QTYO_RC

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您只返回每个的最后一个结果对象;即,你正在设置

$qO_Array = $qO->result_object();

变量,它应该是:

$qO_Array[] = $qO->result_object();

获取所有这些

BTW - 您是否有一个特定的原因来调用result_object()而不是通常的result()或result_array()?这不一定是错的,但我想知道你是否故意这样做?