If anyone could provide me with the proper code that would be great. What I am trying to do is add an <hr />
after the information that is being echoed ONLY if more than one result is being pulled from my database. Here is the code if anyone can help me. Thanks.
<html>
<script>
function goBack()
{
window.history.back()
}
</script>
<body>
<div style="width: 875px; margin-left: 30px; margin-right: auto;"><img src="searchresults.png" alt="" title="Search Results" alt="" /></p>
<?php
$term = $_POST['term'];
$sql = mysql_query("SELECT * FROM store_location where store_name like '%$term%' or address like '%$term%' or city like '%$term%' or state like '%$term%' or zip like '%$term%' or phone like '%$term%' or fax like '%$term%' or email like '%$term%' or url like '%$term%' ");
if( mysql_num_rows($sql) == 0) echo "<p>No TeachPro Store(s) in your area.</p>";
while ($row = mysql_fetch_array($sql)){
echo 'Store Name: '.$row['store_name'];
echo '<br/> Address: '.$row['address'];
echo '<br/> City: '.$row['city'];
echo '<br/> State: '.$row['state'];
echo '<br/> Zip: '.$row['zip'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Fax: '.$row['fax'];
echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
echo '<br/><br/>';
}
?>
</div>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
Just wrap you while
loop in an else
case and output the <hr>
in there. You already have the appropriate logic to output a <p>
if no rows are found, and you can extend it.
if( mysql_num_rows($sql) == 0) {
echo "<p>No TeachPro Store(s) in your area.</p>";
}
// Instead of relying on an empty fetch to output nothing, put it in an else {}
else {
while ($row = mysql_fetch_array($sql)){
echo 'Store Name: '.$row['store_name'];
echo '<br/> Address: '.$row['address'];
echo '<br/> City: '.$row['city'];
echo '<br/> State: '.$row['state'];
echo '<br/> Zip: '.$row['zip'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Fax: '.$row['fax'];
echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
echo '<br/><br/>';
}
// And your <hr /> and whatever else you need...
echo "<hr />";
}
Just a side note about HTML output - be sure to wrap these values in htmlspecialchars()
for proper escaping as HTML, to avoid problems if they contain HTML special characters like < > &
(and possibly to protect against XSS if this was user-input!)
// Ex:
echo 'Store Name: '.htmlspecialchars($row['store_name']);
And even more pressing is to sanitize your query inputs against SQL injection with mysql_real_escape_string()
.
// At a minimum:
$term = mysql_real_escape_string($_POST['term']);
In the long run, consider switching to an API which supports prepared statements, like MySQLi or PDO.