将搜索结果拆分为不同的表MySql php

时间:2013-12-06 18:49:23

标签: php html mysql

我有一个问题:

SELECT * FROM table WHERE column like '%value%' ORDER BY name

和结果表:

 echo "<table class='table'>";

    if ($num==0) echo "<tr><td>Sorry, no items found.</td></tr>"; 

    else{   
       echo "<tr> <th>Nr.</th> <th>Name</th>";
       echo "<th>Description</th> <th>Image</th>";     

    $lf = 1;
    while ($dsatz = mysql_fetch_assoc($res))
        {
           echo "<tr>";
           echo "<td>$lf</td>";
           echo "<td>" . $dsatz["name"] . "</td>";
           echo "<td>" . $dsatz["description"] . "</td>";
           echo "<td>" . $dsatz["image"] . "</td>";
           echo "</tr>";
        $lf = $lf + 1;
        } 
        }
 echo "</table>";

现在我想做的是根据列“type”中的值将结果拆分为两个结果表。我们的想法是在不同的标题下对结果进行排序。这里的逻辑是:

If value from type == '1' use Table1

If value from type == '2' use Table2

我对如何做到这一点有任何想法?

提前thx! SEB

1 个答案:

答案 0 :(得分:0)

1。逻辑方式是两个查询

SELECT * FROM table WHERE column like '%value%' and type = 1 ORDER BY name

SELECT * FROM table WHERE column like '%value%' and type = 2 ORDER BY name

然后单独填写每个表格。

-

2。您的第二个选择是在每个表格中的“while”部分内使用条件不推荐):< / p>

首先,复制结果:

$res2 = $res;

然后:

echo "<table class='table'>";

if ($num==0) echo "<tr><td>Sorry, no items found.</td></tr>"; 

else
{   
    echo "<tr> <th>Nr.</th> <th>Name</th>";
    echo "<th>Description</th> <th>Image</th>";     

    $lf = 1;
    while ($dsatz = mysql_fetch_assoc($res))
    {
        if($dsatz['type'] == 1)
        {
            echo "<tr>";
            echo "<td>$lf</td>";
            echo "<td>" . $dsatz["name"] . "</td>";
            echo "<td>" . $dsatz["description"] . "</td>";
            echo "<td>" . $dsatz["image"] . "</td>";
            echo "</tr>";

            $lf = $lf + 1;
        }
    } 
}
echo "</table>"; 

echo "<table class='table'>";

if ($num==0) echo "<tr><td>Sorry, no items found.</td></tr>"; 

else
{   
    echo "<tr> <th>Nr.</th> <th>Name</th>";
    echo "<th>Description</th> <th>Image</th>";     

    $lf = 1;
    while ($dsatz = mysql_fetch_assoc($res2))
    {
        if($dsatz['type'] == 2)
        {
            echo "<tr>";
            echo "<td>$lf</td>";
            echo "<td>" . $dsatz["name"] . "</td>";
            echo "<td>" . $dsatz["description"] . "</td>";
            echo "<td>" . $dsatz["image"] . "</td>";
            echo "</tr>";

            $lf = $lf + 1;
        }
    } 
}
echo "</table>";