在最后一个记录结果上创建表关闭

时间:2011-08-08 22:25:45

标签: php mysql html-table

我正在为订单购物车创建基本搜索功能。我有一个看起来像这样的循环:

$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'";
$data = mysql_query($dataQuery) or die(mysql_error());

$pageContent .= '
<table border="1">
    <thead>
        <tr>
            <th>Stock Code</th>
            <th>Description</th>
            <th>Packsize</th>
            <th>Price</th>
            <th>In-Stock?</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
    ';
 //And we display the results 
while($result = mysql_fetch_array( $data ))
{
    $prId = $result['id'];
    $prRefCode = $result['refCode'];
    $prDesc = $result['desc'];
    $prPack = $result['pack'];
    $prMeasure = $result['measure'];
    $prQuantity = $result['quantity'];
    $prDeptCode = $result['deptCode'];
    $prTaxable = $result['taxable'];
    $prPrice1 = $result['price1'];
    $prPrice2 = $result['price2'];
    $prCrdCode = $result['crdCode'];
    $prCost1 = $result['cost1'];
    $prCost2 = $result['cost2'];
    $pageContent .= '
        <tr>
            <td>'.$prId.'</td>
            <td>'.$prDesc.'</td>
            <td>'.$prPack.'x'.$prSize.' '.$prMeasure.'</td>
            <td>R'.$prPrice1.'</td>
    ';
    if (empty($prQuantity)) {
        $pageContent .= '
        <td>No</td>
        ';
        } else {
        $pageContent .= '
        <td>Yes</td>
        ';
        }
    $pageContent .= '
            <td>
                <form action="" method="post">
                    <div>
                        <input type="hidden" name="id" value="'.$prId.'" />
                        <input type="submit" name="action" value="Order" />
                    </div>
                </form>
            </td>            
       </tr>
    ';
} 
$pageContent .= '
        </tbody>
    </table>
';
//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{
    $pageContent .= '
    <p>Sorry, but we can not find an entry to match your query</p>
';
} 

//And we remind them what they searched for 
 $pageContent .= '
<p><b>Searched For:</b>  '.$find.'</p>
';
}

$pageContent .= '
<br />
<p>All prices are inclusive of VAT</p>
';

如您所见,这会生成一个表,显示与where子句匹配的表产品的每一行。

我想要做的是将$pageContent的最后一次出现添加到最后一个循环结果中,因此有效地关闭了表。这种情况将是:

$pageContent .= '
    </tbody>
</table>
';

作为补充,我希望在循环之前首次出现$pageContent,因为结果被调用,有效地打开了表格。这种情况将是:

$pageContent .= '
<table border="1">
    <thead>
        <tr>
            <th>Stock Code</th>
            <th>Description</th>
            <th>Packsize</th>
            <th>Price</th>
            <th>In-Stock?</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
    ';

我面临的问题是,如果用户搜索无效字符串,则段落“抱歉,但我们找不到与您的查询匹配的条目”会在表标题下输出,该标题没有分配给它的行。我试图产生一种效果,如果用户输入无效的搜索选项,那么这段将自己显示,没有表头,这应该只适用于有效搜索字符串的出现。

如果有人对此有任何意见,我将非常感激!

2 个答案:

答案 0 :(得分:1)

也许您可以将变量设置为0,然后在循环内部,检查变量是否= = 0,如果是,则输出表格开头。

然后,在循环结束时,还要检查变量是否等于结果集中的行数,如果是,则输出结束标记。 然后,每次迭代递增变量。

类似的东西:

$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'";
$data = mysql_query($dataQuery) or die(mysql_error());

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
    $anymatches = mysql_num_rows($data);
    if ($anymatches == 0) {
        $pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
';
    }

$tempVar = 0;
//And we display the results 
while ($result = mysql_fetch_array($data)) {
    $prId = $result['id'];
    $prRefCode = $result['refCode'];
    $prDesc = $result['desc'];
    $prPack = $result['pack'];
    $prMeasure = $result['measure'];
    $prQuantity = $result['quantity'];
    $prDeptCode = $result['deptCode'];
    $prTaxable = $result['taxable'];
    $prPrice1 = $result['price1'];
    $prPrice2 = $result['price2'];
    $prCrdCode = $result['crdCode'];
    $prCost1 = $result['cost1'];
    $prCost2 = $result['cost2'];


    if ($tempVar == 0) {
        $pageContent .= '
<table border="1">
    <thead>
        <tr>
            <th>Stock Code</th>
            <th>Description</th>
            <th>Packsize</th>
            <th>Price</th>
            <th>In-Stock?</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
    ';
    }
    if ($tempVar == mysql_num_rows($data)) {
        $pageContent .= '
    </tbody>
</table>
';
    }

        $pageContent .= '
    <tr>
        <td>' . $prId . '</td>
        <td>' . $prDesc . '</td>
        <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
        <td>R' . $prPrice1 . '</td>
';
        if (empty($prQuantity)) {
            $pageContent .= '
    <td>No</td>
    ';
        } else {
            $pageContent .= '
    <td>Yes</td>
    ';
        }
        $pageContent .= '
        <td>
            <form action="" method="post">
                <div>
                    <input type="hidden" name="id" value="' . $prId . '" />
                    <input type="submit" name="action" value="Order" />
                </div>
            </form>
        </td>           
   </tr>
';
        $tempVar ++;
    }
    $pageContent .= '
    </tbody>
</table>
';


//And we remind them what they searched for 
    $pageContent .= '
<p><b>Searched For:</b>  ' . $find . '</p>
';
}

$pageContent .= '
<br />
<p>All prices are inclusive of VAT</p>
';

答案 1 :(得分:1)

有几种可能的解决方案。您可以使用不同的var名称来存储表格HTML($pageContent以外的其他内容),如果行数大于零,则将其连接到$pageContent(您已经在底部附近进行了此检查)代码)。另一种可能的方法是在生成表之前将mysql_num_rows调用移动到某处,然后将表相关代码包装在if中。