在while循环中检查相同的行,并将它们放在单独的表中 [英] Check for same rows in a while loop and put them in a separate table

查看:102
本文介绍了在while循环中检查相同的行,并将它们放在单独的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先检查所有相等的行,然后将它们放入单独的表中.

I would want to first check for all equal rows and then put them into a separate table.

这是我到目前为止所做的:

This is what I have done so far:

table1
    |   id  |   name    |
    |   1   |   JUS     |
    |   1   |   NUM     |
    |   2   |   SET     |


/**
 * this is the the query for retrieving the data
 * from table1
 */
$query="SELECT 
            id,
            name
        FROM 
            table1
        order by 
            id";

$results=$db->query($query);

$previous='';
while($row=mysqli_fetch_assoc($results)){
    $id=$row['id'];
    $name=$row['name'];

    if($id==$previous){

        /**
         * This is where i am stucked up
         */
        $current='';

    }
    $previous=$id;
}

我想将ID为1的值获取到一个html表中,如下所示

I want to get the id with 1 as the value into one html table, like below

    first html table
    ID      |   1   |   1   |
    Name    |   JUS |   NUM |

,还将2作为ID的值获取到另一个html表中.所以总而言之,我们将获得单独的表格 如果id不相同:

and also get the id with 2 as the value into another html table. So in all we will get separate tables if the id are not the same:

  second html table
    ID      | 2     |
    Name    | SET   |

任何关于如何实现它的想法都值得赞赏.

Any idea as to how to go about it is appreciated.

推荐答案

您可以先将所有内容收集在一个容器中,并使用id作为键,以便将它们组合在一起.之后,只需相应地打印它们即可:

You could just gather all them first in a container, using ids as your keys so that they'll be grouped together. After that, just print them accordingly:

$data = array();
while($row = $results->fetch_assoc()){
    $id = $row['id'];
    $name = $row['name'];
    $data[$id][] = $name; // group them
}

foreach($data as $id => $values) {
    // each grouped id will be printed in each table
    echo '<table>';
    // header
    echo '<tr>';
        echo '<td>ID</td>' . str_repeat("<td>$id</td>", count($values));
    echo '</tr>';

    echo '<tr>';
    echo '<td>Name</td>';
    foreach($values as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';

    echo '</table><br/>';
}

如果这些字段就是这样,这将起作用,如果您需要更具动态性的东西,则需要另一个维度,而不仅仅是压入name,您将需要压入整行:

This will work if those fields are just like that, if you need something more dynamic, you need another dimension, and instead of just pushing name, you'll need the push the entire row:

$results = $db->query('SELECT id, name, age FROM table1');

$data = array();
while($row = $results->fetch_assoc()){
    $id = $row['id']; unset($row['id']);
    $data[$id][] = $row; // group them
}

$fields = array('name', 'age');

foreach($data as $id => $values) {
    // each grouped id will be printed in each table
    echo '<table>';
    // header
    echo '<tr>';
        echo '<td>ID</td>' . str_repeat("<td>$id</td>", count($values));
    echo '</tr>';

    foreach($fields as $field) {
        // construct td
        $temp = '';
        echo "<tr><td>$field</td>";
        for($i = 0; $i < count($values); $i++) {
            $temp .= '<td>' . $values[$i][$field] . '</td>';
        }
        echo $temp; // constructed td
        echo '</tr>';

    }

    echo '</table><br/>';
}

这篇关于在while循环中检查相同的行,并将它们放在单独的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆