Model

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

So each product has different options but all products options are prefixed by "X_" or "O_". I need to get the DISTINCT values of each COLUMN of "X_" and "O_" and in any VIEW I need to build a form with these values. Here is a look at the array:

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) 

My current MODEL is only returning X_QTY and O_RC to my view.

What am I doing incorrectly?

有帮助吗?

解决方案

You are only returning the last result object of each; ie, you are setting

$qO_Array = $qO->result_object();

to a variable, and it should be:

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

to get all of them

BTW - do you have a specific reason for calling result_object() instead of the usual result() or result_array()? It's not necessarily wrong, but I wonder if you are doing it intentionally?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top