これまでのところ、ユーザーがデータベースの特定の列を検索してすべての結果を表示できるようにする次の php があります。
<?php
require("header.php");
if(isset($_REQUEST['searching'])){ //check if form has been submitted
echo"<h2>Results</H2><p>";
connect('final');//connect to DB
//set the values from search form
$field = $_POST['field'];
$query = $_POST['query'];
$query = htmlspecialchars($query); // stop HTML characters
$query = mysql_real_escape_string($query); //stop SQL injection
$data = mysql_query("SELECT *
FROM customer
INNER JOIN address ON customer.ID = address.customer_ID
LEFT OUTER JOIN sites ON address.ID = sites.address_ID
WHERE customer.ID IN (SELECT customer.ID
FROM customer
INNER JOIN address ON customer.ID = address.customer_ID
LEFT OUTER JOIN sites ON address.ID = sites.address_ID
WHERE upper(customer.$field) LIKE'%$query%')") ;//query the DB with search field in colleumn selected//
//$data = mysql_query("SELECT * FROM customer INNER JOIN address ON customer.ID = address.Customer_ID LEFT OUTER JOIN sites ON address.ID = sites.address_ID WHERE upper(customer.$field) LIKE'%$query%'") ;
if($data === FALSE) {
$error = 'Query error:'.mysql_error();
echo $error;
}
else
{
while($results = mysql_fetch_array($data)){// puts data from database into array, loops until no more
echo "<br>";
echo $results['First_Name'];
echo " ";
echo $results['Surname'];
echo " ";
echo $results['Company_Name'];
echo " ";
echo $results['Telephone'];
echo " ";
echo $results['Alt_Telephone'];
echo " ";
echo $results['line_1'];
echo " ";
echo $results['line2'];
echo " ";
echo $results['town'];
echo " ";
echo $results['postcode'];
echo " ";
echo $results['site_name'];
//posts results from db query
}
}
$anymatches=mysql_num_rows($data); //checks if the querys returned any results
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
ただし、出力に echo を使用しているため、すべての結果がページの一番上に表示され、すべての html コンテンツがその下にプッシュされます。
HTMLコンテンツに表示されるようにフォーマットするにはどうすればよいですか? たとえば、検索ボタンの下。