如果一行不是唯一的-忽略它- php


If a row is not unique - ignore it - php

我正在编写以下代码:

function form_Subcat_Picker() {
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }       
    $catPicker = "SELECT Subcatid, Subcatname, Parentid
            FROM ProductSubCats
            ORDER BY Subcatid";
    if ($Result = $mysqli->query($catPicker)){
        if (!$Result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = $Result->fetch_assoc()) { 
            echo '<div class="parentid'.$row['Parentid'].'">';
            echo '<select name="Subcatid">';
            echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
            echo '</select>';
            echo '</div>';
        }
    }
    $mysqli->close();
}

我要做的,在这行:

while ($row = $Result->fetch_assoc()) { 
            echo '<div class="parentid'.$row['Parentid'].'">';

如果$row['Parentid']部分与前一次迭代相同,我想忽略特定的行(添加div类)

因此,如果例如在第一次运行$row['Parentid']是1,在下一个循环中它再次为1,我不想创建一个新的div,只是回呼其他所有内容,从而保持它在同一个div。

这可能吗?或者,我怎么能使多个子类id和名称出现在一个div,如果他们共享一个共同的父id(有多个父id)对于行:

echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";

也许这行得通:

$last_id = 0;
while ($row = $Result->fetch_assoc()) {
    if ($last_id != $row['Parentid']) {
        echo '<div class="parentid'.$row['Parentid'].'">';
        echo '<select name="Subcatid">';
        echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
        echo '</select>';
        echo '</div>';
        $last_id = $row['Parentid'];
    }
}

然而,我认为最好的解决方案是在SQL语句中过滤它们,也许是GROUP BY子句,但我不是100%确定如何做到这一点:)。

认为,

这是循环的基本功能。让我们看看当前的循环:

 while ($row = $Result->fetch_assoc()) { 
     ...
 }

当您想要在特定条件下跳过时,让我们介绍这种跳过(首先不太关心条件):

 while ($row = $Result->fetch_assoc()) { 
     if ($condition) continue;
     ...
 }

现在我们来表述这个条件。当我们想要查看最后一个$row时,我们需要保留一个副本:

 $last = null;
 while ($row = $Result->fetch_assoc()) { 
     if ($condition) continue;
     ...
     $last = $row;
 }

现在我们已经得到了创建条件所需的数据,$last可以包含最后一行(如果有的话),因此可以进行比较:

 $last = null;
 while ($row = $Result->fetch_assoc()) { 
     $condition = $last && $row['Parentid'] === $last['Parentid'];
     if ($condition) continue;
     ...
     $last = $row;
 }

基本上就是这样。根据逻辑的不同,您可能希望切换到for循环:

 for ($last = null; $row = $Result->fetch_assoc(); $last = $row) { 
     $condition = $last && $row['Parentid'] === $last['Parentid'];
     if ($condition) continue;
     ...         
 }

例如,这确保了对于每次迭代(甚至跳过的迭代),在循环结束时将$last设置为$row

代替continue,您可以自然地做不同的事情,例如不输出<div>或类似的。

我是这么写的

// add a variable to hold the previous value
$previous_parent_id = "";
while ($row = $Result->fetch_assoc()) { 
  // then add an if statement to see if it's the previous statement
  if ($row['parent_id'] != $previous_parent_id){
    echo '<div class="parent_id'.$row['parent_id'].'">';
    $previous_parent_id = $row['parent_id'];
  }
}

在这些记录上进行循环

ID ParentID
1  0
2  0
3  1
4  1
4  2
4  2

的输出将是:

<div class="parent_id0">
<div class="parent_id1">
<div class="parent_id2">