1

这是从我的模型返回的 var_dump 或数组:

array (size=5)
  0 => 
    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)
  1 => 
    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)
  2 => 
    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)
  3 => 
    array (size=4)
      0 => 
        object(stdClass)[26]
          public 'X_QTY' => string '100' (length=3)
      1 => 
        object(stdClass)[25]
          public 'X_QTY' => string '250' (length=3)
      2 => 
        object(stdClass)[29]
          public 'X_QTY' => string '500' (length=3)
      3 => 
        object(stdClass)[30]
          public 'X_QTY' => string '1000' (length=4)
  4 => 
    array (size=3)
      0 => 
        object(stdClass)[28]
          public 'O_RC' => string 'YES' (length=3)
      1 => 
        object(stdClass)[27]
          public 'O_RC' => string 'NO' (length=2)
      2 => 
        object(stdClass)[33]
          public 'O_RC' => string 'NA' (length=2)

我的控制器正在发送到我的视图,在我看来,上面的转储是:var_dump($printerOptions)

好的,所以我需要为每个数组和数组的单选按钮创建一个下拉菜单O_RC

我正在使用 codeigniter 并form_dropdown('',$val);在 foreach 循环内部使用会导致数组中每个键和元素的多个下拉列表。

foreach ($printerOptions as $Options) 
{
//var_dump($Options);
    foreach ($Options as $Specs) 
    {
        echo $Specs->X_SIZE;
        echo $Specs->X_PAPER;
        echo $Specs->X_COLOR;
        echo $Specs->X_QTY;
        echo $Specs->O_RC;
    }
}

此 ^ 代码将回显相应数组的预期值,但由于某种原因,输出出现 LOOPING 错误:

Undefined property: stdClass::$X_PAPER
Undefined property: stdClass::$X_COLOR
Undefined property: stdClass::$X_QTY
Undefined property: stdClass::$O_RC

查看我的数组如何分别为每个数组创建一个下拉菜单?

谢谢您的帮助。

模型 /// 更新 ///

class M_Pricing extends CI_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.
        $resultArray = array();
        
        foreach ($get_all_col_names as $key => $value) {
            //get all o_types for the product by column name
            if (preg_match('/O_/', $value)) 
                {
                $v = (array('Specs' => $value));
                foreach ($v as $vals) {
                    $this->db->select($vals);
                    $this->db->distinct();
                    $qX = $this->db->get($table_by_product);
                    $resultArray[] = $qX->result();
                    }
                } 
            
            //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 (preg_match('/X_/', $value)) 
                {
                $v = (array('Type' => $value));
                foreach ($v as $vals) {
                    $this->db->select($vals);
                    $this->db->distinct();
                    $qX = $this->db->get($table_by_product);
                    $resultArray[] = $qX->result();
                    }
                }       
        }
        //return $resultArray
        //var_dump($resultArray); die;
        return $resultArray;
    }
4

1 回答 1

0

据我了解,您的结果数组应该是对象或哈希,如下所示:

array('Type'=>array(
   'X_SIZE'=>array('1.75x3','1.75x3.5(slim)','2x3'),
   'X_PAPER'=>array('14ptGlossCoatedCoverwithUV(C2S)','14ptPremiumUncoatedCover'),
///...),
'Specs'=>array(

//...) );

循环通过这样的结构:

//Types
echo 'Types';
foreach ($Options['Types'] as $type=>$values) {
echo $type;
echo "<select>";
foreach ($values as $value) {
echo "<option value="$value">$value</option>";
}
echo "</select>";
}

//Specs
echo 'Specs';
foreach ($Options['Specs'] as $type=>$values) {
 echo $type;
 echo "<select>";
 foreach ($values as $value) {
   echo "<option value="$value">$value</option>";
 }
 echo "</select>";
}

模型:

<?php
class M_Pricing extends CI_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.

        $resultArray=array();
        $resultArray['Options']=array();
        $resultArray['Specs']=array();

        foreach ($get_all_col_names as $key => $value) {
            //get all o_types for the product by column name
            $opt_type="";
            if (preg_match('/O_/', $value)) {$opt_type="Options";} elseif
               (preg_match('/X_/', $value)) {$opt_type="Specs";} else {continue;}
               if (!isset($resultArray[$opt_type][$value])) {$resultArray[$opt_type][$value]=array();}
                    $this->db->select($value);
                    $this->db->distinct();
                    $qX = $this->db->get($table_by_product);
                    foreach ($qX->result() as $v) {
                     $resultArray[$opt_type][$value][]=$v->$value;
                    }
           }      

        //return $resultArray
        //var_dump($resultArray); die;
        return $resultArray;
    }
}
于 2012-11-03T01:22:17.953 回答