0

我正在尝试为我的网页创建一个搜索功能,但我在格式化结果时遇到了困难,以便它以表格格式出现,这是我的代码:

<?php
    $query = $_GET['query'];      
    $min_length = 10;

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 

        $query = mysql_real_escape_string($query);

        $raw_results = mysql_query("SELECT * FROM aggrement
            WHERE (`description` LIKE '%".$query."%') OR (`start_date` LIKE '%".$query."%') OR (`end_date` LIKE '%".$query."%') OR (`balance` LIKE '%".$query."%') OR (`tax_value` LIKE '%".$query."%') OR (`amount_paid` LIKE '%".$query."%') OR (`hosting_name` LIKE '%".$query."%') OR (`domain_name` LIKE '%".$query."%') OR (`first_name` LIKE '%".$query."%') OR (`last_name` LIKE '%".$query."%')") or die(mysql_error());

        if(mysql_num_rows($raw_results) > 0){

            while($results = mysql_fetch_array($raw_results)){

                echo "<p><h3>".$results['description']."</h3>".$results['start_date']."</p>";
                         }    
        }
        else{
            echo "No results";
        }
    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
?>

我正在使用 echo 输出结果 echo "<p><h3>".$results['description']."</h3>".$results['start_date']."</p>";

但我想使用这种语法:

echo "<table>";
echo "<tr>
<th>Id</th>
<th><a href='viewaggrement.php?sort=description'>Description</th>
<th><a href='viewaggrement.php?sort=start_date'>Start Date</th>
<th><a href='viewaggrement.php?sort=end_date'>End Date</th>
<th><a href='viewaggrement.php?sort=balance'>Balance</th>
<th><a href='viewaggrement.php?sort=tax_value'>Tax </th>
<th><a href='viewaggrement.php?sort=amount_paid'>Amount Paid</th>
<th><a href='viewaggrement.php?sort=domain_name'>Domain Name</th>
<th><a href='viewaggrement.php?sort=hosting_name'>Hosting Name</th>
<th><a href='viewaggrement.php?sort=first_name'>First Name</th>
<th><a href='viewaggrement.php?sort=last_name'>Last Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
echo "<table>";
4

2 回答 2

1

这样的事情呢?

在开始循环遍历结果之前打开 table 标记并为每个结果创建一行。然后,当它完成对结果的循环时,关闭 table 标记。

<?php
    $query = $_GET['query'];      
    $min_length = 10;

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 

        $query = mysql_real_escape_string($query);

        $raw_results = mysql_query("SELECT * FROM aggrement
            WHERE (`description` LIKE '%".$query."%') OR (`start_date` LIKE '%".$query."%') OR (`end_date` LIKE '%".$query."%') OR (`balance` LIKE '%".$query."%') OR (`tax_value` LIKE '%".$query."%') OR (`amount_paid` LIKE '%".$query."%') OR (`hosting_name` LIKE '%".$query."%') OR (`domain_name` LIKE '%".$query."%') OR (`first_name` LIKE '%".$query."%') OR (`last_name` LIKE '%".$query."%')") or die(mysql_error());

        if(mysql_num_rows($raw_results) > 0){

        echo "<table>";
            while($results = mysql_fetch_array($raw_results)){

                echo "<tr>";
                    echo "<td>".$results['description']."</td><td>".$results['start_date']."</td>";
                echo "</tr>";
                         }    

        echo "</table>";
        }
        else{
            echo "No results";
        }
    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
?>
于 2013-04-16T18:17:09.973 回答
1

以下应该得到您需要的结果:

if(mysql_num_rows($raw_results) > 0){
    echo "<table>";
    // echo your header row here
    while($results = mysql_fetch_array($raw_results)){
        echo "<tr>";
        echo "<td>".$results['description']."</td>";
        echo "<td>".$results['start_date']."</td>";
        //And so on for each row you require
        echo "</tr>";
    }
    echo "</table>";
}
于 2013-04-16T18:20:06.740 回答