I am creating a basic search function for an order cart. I have a loop that looks like this :
$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>
';
As you can see, this produces a table that displays each row of the table products that matches the where clause.
What I would like to do is have the last occurance of $pageContent
added to th开发者_如何学编程e very last loop result, so effectively it closes the table. This occurance would be:
$pageContent .= '
</tbody>
</table>
';
As an addition, I would like to have the first occurance of $pageContent
added directely before the loop as the results are called, effectively opening the table. This occurance would be:
$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>
';
The problem that I am facing is that if a user searches an invalid string, the paragraph "Sorry, but we can not find an entry to match your query" gets output under the table header, which has no rows assigned to it. I am trying to produce an effect where if the user enters an invalid search option, then this paragraph will be displayed by itself, without table header, which should only apply to the occurance of a valid search string.
If anyone has some input on this, I would really appreciate it!
Perhaps you can set a variable to 0, then inside the loop, check if the variable == 0, if so, then output your table opening.
Then, at the end of the loop, also check if the variable is equal to the number of rows in the resultset, and if so, output your closing tags. Then, increment the variable every iteration.
something like this:
$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>
';
There are several possible solutions. You could use a different var name for storing the table HTML (something other than $pageContent
), then concatenate it to $pageContent
if the number of rows is greater than zero (you already have this check near the bottom of the code). Another possible approach is to move the mysql_num_rows
call to somewhere before generating the table, then wrapping the table-related code inside an if
.
精彩评论