php第二个foreach没有写出值

时间:2013-04-22 06:07:29

标签: php

我在显示数据时遇到问题。我有以下数据

filter_name        filter_value
Hard Drize Size    16GB
Hard Drize Size    32GB
Screen Size        7''
Screen Size        8''

我想这样呈现。

Hard Drize Size
16GB
32GB

Screen Size
7''
8''

我在php代码中想要的是检查filter_name是否在$ temp数组中,如果不是,则将其添加到$ temp数组中,这样就不会导致重复的条目。我现在遇到的问题是当我使用相同的数据在循环中做第二个foreach时我得不到数据并且我的sql是正确的。所以我不知道如何把价值观。使用第二个foreach,它只打印出第一个$ filter_name [“filter_name”]和所有。

PHP:

if($stmt->rowCount() > 0) 
{
    $filters = "<div class=\"filters\">         
    <div class=\"apply-filters\">Filters</div>";
    $temp = array();        
    $stmt->fetch(PDO::FETCH_ASSOC);

    foreach($stmt as $filter_name)
    {
        if(!in_array($filter_name["filter_name"], $temp)) 
        {
            $temp[] = $filter_name["filter_name"];
            $filters .= "<div class=\"filter-header\">".$filter_name["filter_name"]."</div>";
            //second loop here doesn't work with the same data.
            // test if filter_name["filter_name"] == second loop second_loop["filter_name"]
            // ten write out second_loop["filter_value"] e.g.
            foreach($stmt as $filter_value)
            {
                if($filter_name["filter_name"] == $filter_value["filter_name"])
                {
                    $filters .= $filter_value["filter_value"] ."<br />";
                }                  
            }

        }               
    }
    $filters .= "</div>";   
}

5 个答案:

答案 0 :(得分:2)

首先,在第一个和第二个 foreach 之前执行$stmt的var_dump来查看它的表单。也许它没有像你想象的那样格式化?

为什么不循环使用$ stmt一次并在该循环中打印$stmt['filter_name']$stmt['filter_value']?现在,您为第一个$stmt的每次迭代循环foreach一个“额外”时间。相反,只需要foreach($stmt as $filter)$filter作为关联数组,每个条目都应包含filter_namefilter_value

然后你可以将实际打印移到foreach之外,并且只在循环中构造你的数组,看起来应该像

array("Hard Drive Size" => array("16GB", "32GB"), "Screen Size" => array("7''", "8''"));

然后您可以使用

遍历该数据结构
foreach($myArray as $filter_name => $filter_values)
{
  // Print filter name header
  foreach($filter_values as $filter_value)
  {
    // print each value of the specific filter
  }
}

要构建呈现的结构,您可以执行类似

的操作
...

    $filters = array();

    foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $filter)
    {
      $filters[$filter['filter_name']][] = $filter['filter_value'];     
    }

...

然后迭代 $ filters 结构并打印出来。

更新

$stmt的var_dump中可以清楚地看出$stmt不是您的结果集。您应该将$stmt->fetch的结果分配给某事物。与$result = $stmt->fetch(PDO::FETCH_ASSOC)一样,然后迭代$result

检查

http://php.net/manual/en/pdostatement.fetch.php

http://php.net/manual/en/pdostatement.fetchall.php

答案 1 :(得分:1)

我稍微修改了您的代码以满足您的需求:

$temp = array();
foreach($stmt as $filter_name)
{
    if(!in_array($filter_name["filter_name"], $temp)) 
    {
        echo '<b>' . $filter_name['filter_name'] . '</b><br>';
        $temp[] = $filter_name["filter_name"];

        foreach($stmt as $filter_value)
        {
            if($filter_name["filter_name"] == $filter_value["filter_name"])
            {
                echo $filter_value['filter_value'] . '<br>';
            }
        }
    }
}

你可以在这里检查一个工作样本 - &gt; http://codepad.viper-7.com/aFD079

答案 2 :(得分:0)

$之前您遗失filter_value

更改

foreach($stmt as filter_value)

foreach($stmt as $filter_value)

答案 3 :(得分:0)

 $stmt->fetch(PDO::FETCH_ASSOC);

foreach($stmt as $filter_name)

可能(我不确定)

 $aaa= $stmt->fetch(PDO::FETCH_ASSOC);

foreach($aaa as $filter_name)

并尝试

$rows = $stmt->fetch(PDO::FETCH_ASSOC);
$tmp_name='';
$html='';
foreach($rows as $row)
{
    if($row['filter_name'] !=  $tmp_name){
     $html .=$row['filter_name'];
    }
    $html .=$row['filter_value'];

    $tmp_name=$row['filter_name'];
}

echo $ html;

答案 4 :(得分:0)

if($stmt->rowCount() > 0) 
{
    $filters = "<div class=\"filters\">         
    <div class=\"apply-filters\">Filters</div>";
    $temp = array();        
    $stmt->fetch(PDO::FETCH_ASSOC);

    foreach($stmt as $filter)
        $temp[$filter['filter_name']][] = $filter['filter_value']; 

    foreach($temp as $filter_name => $filter_values)
    {
        $filters .= "<div class=\"filter-header\">".$filter_name."</div>";
        foreach ($filter_values as $value)
            $filters .= $filter_value."<br />";
    }

    $filters .= "</div>";   
}