使用php从foreach复制行

时间:2015-12-11 07:19:02

标签: php

我有来自查询的输出

object(stdClass)#14 (6) {
  ["aid"]=>
  string(2) "11"
  ["bid"]=>
  string(2) "34"
  ["colorname"]=>
  string(6) "Silver"
  ["colorgroupname"]=>
  string(15) "LIGHT AQUA OPAL"
  ["colorcode"]=>
  string(3) "6M8"
  ["brand"]=>
  string(6) "Car"
}

但是在foreach循环之后

complite代码我使用的和我使用var_dump显示输出我得到一行但在循环后我得到6个重复的行。

function ViewDBGridColorfinder($header,$DBG_control,$parameters)
    {
        $rows = $this->select_join_by(index_join_ColorFinder,TBL_COLOR,tbl_join_ColorFinder,where_by,order_by_dbgrid,$parameters);

        $table = new Generator_Table($header);
        //var_dump($rows);

        if (is_array($rows) || is_object($rows))
        {
            foreach($rows as $val)
            {
                $DetailColor = '<a href="#PopColor" onclick="showdetail('.$rows->bid.')" data-toggle="modal">'.$rows->colorname.'</a>';
                $table->addCell($DetailColor);
                $table->addCell($rows->colorcode);
                $table->addCell($rows->brand);
                $table->addCell($rows->colorgroupname);
                $table->addCell(str_replace('onclick=""',"onclick=DelRow(".$rows->aid.")",$DBG_control));
            }
        }
        $DBGrid_data = $table->generate();

        return $DBGrid_data;
    }

结果要重复。

错误在哪里?

4 个答案:

答案 0 :(得分:0)

将所有Sub Validate13() Dim mymsg As String Dim celltext As String Dim celltext1 As String celltext = ActiveSheet.Range("C4").Text celltext1 = ActiveSheet.Range("C3").Text celltext2 = ActiveSheet.Range("E4").Text If InStr(1, celltext1, "mz_013_reg-life_") = 1 Then Range("$B$26").Value = "The Header is OK." Range("$A$26").Interior.Color = vbGreen Else Range("$B$26").Value = "The header is not OK. Please check." Range("$A$26").Interior.Color = vbYellow End If If InStr(1, celltext, "44") = 1 Then Range("$B$27").Value = "The subscriber number starts with 44 and is OK." Range("$A$27").Interior.Color = vbGreen Else Range("$B$27").Value = "The subscriber number is not OK. Please check." Range("$A$27").Interior.Color = vbYellow End If If Len(celltext) > 10 And Len(celltext) < 13 Then Range("$B$28").Value = "The subscriber number length is " & Len(celltext) & " and is OK ." Range("$A$28").Interior.Color = vbGreen Else Range("$B$28").Value = "The subscriber number Length not OK. Please check." Range("$A$28").Interior.Color = vbYellow End If If Len(celltext2) > 13 And Len(celltext2) < 15 Then Range("$B$29").Value = "The timestamp length is " & Len(celltext2) & " and is OK ." Range("$A$29").Interior.Color = vbGreen Else Range("$B$29").Value = "The timestanmp not OK. Please check." Range("$A$29").Interior.Color = vbYellow End If End Sub 替换为foreach循环中的$rows

答案 1 :(得分:0)

您使用变量$ rows本身而不是$ val。像这样更新代码:

for

答案 2 :(得分:0)

我认为这只会对你有所帮助

$DetailColor = '<a href="#PopColor" onclick="showdetail('.$rows->bid.')" data-toggle="modal">'.$rows->colorname.'</a>';
 $table->addCell($DetailColor);
 $table->addCell($rows->colorcode);
 $table->addCell($rows->brand);
 $table->addCell($rows->colorgroupname);

 $table->addCell(str_replace('onclick=""',"onclick=DelRow(".$rows->aid.")",$DBG_control));

删除foreach为。

答案 3 :(得分:0)

您的object(stdClass)#14 (6) { ["aid"]=> string(2) "11" ["bid"]=> string(2) "34" ["colorname"]=> string(6) "Silver" ["colorgroupname"]=> string(15) "LIGHT AQUA OPAL" ["colorcode"]=> string(3) "6M8" ["brand"]=> string(6) "Car" } 是一个对象。因此,您并不需要在对象内 forloop 。获得6条记录的原因是因为对象中有六个元素。

算一下,看看,有6个元素(援助,出价,颜色名,颜色组名,颜色代码,品牌):

->

因此,对于每个元素,它将添加单元格。在PHP中,您可以使用 if (is_array($rows) || is_object($rows)) { $DetailColor = '<a href="#PopColor" onclick="showdetail('.$rows->bid.')" data-toggle="modal">'.$rows->colorname.'</a>'; $table->addCell($DetailColor); $table->addCell($rows->colorcode); $table->addCell($rows->brand); $table->addCell($rows->colorgroupname); $table->addCell(str_replace('onclick=""',"onclick=DelRow(".$rows->aid.")",$DBG_control)); } 来访问对象内的元素。以这种方式修改代码:

<provider
  android:name=".data.providers.CRMContentProvider"
  android:authorities="@string/my_provider_authority"
  android:exported="false"
  android:syncable="true"/>