如何在php搜索结果后显示新页面上的每条记录

时间:2017-01-23 17:55:53

标签: php mysql function search

我创建了一个简单的搜索功能,根据用户输入的内容查看几个字段。然后它将结果作为超链接返回,只显示学校的标题。我试图让用户在单独的页面上查看其余信息。我已经为每条记录提供了一个唯一的ID,但我很难找到如何在所需的页面上调用它。这是我用于搜索结果页面search.php的代码

<?php
   require_once 'connection.inc.php';

   if(isset($_GET['keywords'])){
   $keywords = $db->escape_string($_GET['keywords']);
   $query = $db->query("
    SELECT school, address_town
    FROM School
    WHERE school LIKE '%{$keywords}%'
    OR address_town LIKE '%{$keywords}%'
     ");
   ?>
     <div class ="result-count">
      Found <?php echo $query->num_rows; ?> results
      <br>
      <?php 
        if ($query->num_rows == 0)
        echo "Sory there were no results matching your search" ;{
        }
      ?>
    </div>
<?php

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>

    <div class ="result">
        <a href='school.php?id=".$row['id']."'><?php echo $r-> school; ?></a>
    </div>
    <?php
    }
}

以下是我希望显示该特定记录中所有信息的页面代码。这个页面是school.php

<?php
 include ('connection.inc.php');
 connect();

 $sql = "SELECT * FROM School WHERE id = ".$_GET['id'];
 $result = mysql_query($sql);
 ?>

 <!doctype html>
  <html>
    <head>
        <meta charset="utf-8">
            <title>School Details</title>  
    </head>
    <body>
        <?php
            while ($record = mysql_fetch_object($result))
         {
        ?>
        <div class="post">
          <p class="school"><? echo $record->school; ?></p>
          <p class="address_1"><? echo $record->address_1; ?></p>
          <p class="address_town"><? echo $record->address_town; ?></p>
          <p class="postcode"><? echo $record->postcode; ?></p>
          <p class="website"><? echo $record->website; ?></p>
          <p class="phone"><? echo $record->phone; ?></p>
          <p class="council"><? echo $record->council; ?></p>
          <p class="pupils"><? echo $record->pupils; ?></p>
          <p class="age_range"><? echo $record->age_range; ?></p>
          <p class="gender"><? echo $record->gender; ?></p>
          <p class="school_type"><? echo $record->school_type; ?></p>
          <p class="ofsted_rating"><? echo $record->ofsted_rating; ?></p>
          <p class="ofsted_report"><? echo $record->ofsted_report; ?></p>
          <p class="gcse_percentage"><? echo $record->gcse_percentage; ?></p>
          <p class="points_score"><? echo $record->points_score; ?></p>
          <p class="pupil_teacher_ratio"><? echo $record->pupil_teacher_ratio; ?></p>
          <p class="absence_percentage"><? echo $record->absence_percentage; ?></p>
          <p class="language_not_english"><? echo $record->language_not_english; ?></p>
          <p class="free_school_meals"><? echo $record->free_school_meals; ?></p>
          <p class="happiness"><? echo $record->happiness; ?></p>
          <p class="ofsted_parent_review"><? echo $record->ofsted_parent_review; ?></p>
          <p class="events"><? echo $record->events; ?></p>
          <p class="school_description"><? echo $record->school_description; ?></p>
        <?php
        }
         mysql_free_result($result);
         mysql_close();
        ?>  
    </body>
</html>

如果有人可以提供帮助,那将是非常好的,我对此非常陌生并在我去的时候接受它。

由于

2 个答案:

答案 0 :(得分:0)

您应该在锚标记中添加值为_blank的目标属性。

例如:

<a target="_blank" href="http://your_url_here.html">Link</a>

在你的情况下:

<a target="_blank" href='add_your_base_url_here/school.php?id=".$row['id']."'><?php echo $r-> school; ?></a>

我希望有所帮助,欢呼!

答案 1 :(得分:0)

感谢您的帮助,我在一个名为search.php的页面上使用了以下内容后,设法得到了我的帮助

<?php

require 'connection.inc.php';

if(isset($_GET['keywords'])){

$keywords = $db->escape_string($_GET['keywords']);

$query = $db->query("
    SELECT school, address_town, id
    FROM School
    WHERE school LIKE '%{$keywords}%'
    OR address_town LIKE '%{$keywords}%'
    OR council LIKE '%{$keywords}%'
    OR postcode LIKE '%{$keywords}%'") or die($db->error);?>

 <div class ="result-count">
    Found <?php echo $query->num_rows; ?> results
    <br>
    <?php 
        if ($query->num_rows == 0)
        echo "Sorry there were no results matching your search" ;{
        }
    ?></div>
<?php

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>
    <div class ="result">
        <a href="school.php?id=<?php echo $r->id;?>"><?php echo $r->school; ?></a>
    </div>
    <?php
    }
}

}

并且在我需要填充用户选择的特定记录的信息的页面上,我在名为school.php的页面上使用了以下代码

<?php
require 'connection.inc.php';
$id = $_GET['id'];

?>

<!DOCTYPE html>
<html>
   <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><</title>
       <link rel="stylesheet" type="text/css" href="style.css" />
   </head>
   <body>
      <?php

if($row = $db->query("SELECT * FROM School WHERE id = $id")) {
if($count = $row->num_rows) {
    echo '<p>', '</p>';

    while ($row = $row->fetch_object()) {?>

        <div class="all_details">
          <div class="school"><?php echo $row->school;?></div>
          <div class="address_1"><? echo $row->address_1; ?></div>
          <div class="address_town"><?php echo $row->address_town;?></div>
          <div class="postcode"><? echo $row->postcode; ?></div>
          <div class="website"><? echo $row->website; ?></div>
          <div class="phone"><? echo $row->phone; ?></div>
          <div class="council"><? echo $row->council; ?></div>
          <div class="pupils"><? echo $row->pupils; ?></div>
          <div class="age_range"><? echo $row->age_range; ?></div>
          <div class="gender"><? echo $row->gender; ?></div>
          <div class="school_type"><? echo $row->school_type; ?></div>
          <div class="ofsted_rating"><? echo $row->ofsted_rating; ?></div>
          <div class="ofsted_report"><? echo $row->ofsted_report; ?></div>
          <div class="gcse_percentage"><? echo $row->gcse_percentage; ?></div>
          <div class="points_score"><? echo $row->points_score; ?></div>
          <div class="pupil_teacher_ratio"><? echo $row->pupil_teacher_ratio; ?></div>
          <div class="absence_percentage"><? echo $row->absence_percentage; ?></div>
          <div class="language_not_english"><? echo $row->language_not_english; ?></div>
          <div class="free_school_meals"><? echo $row->free_school_meals; ?></div>
          <div class="happiness"><? echo $row->happiness; ?></div>
          <div class="ofsted_parent_review"><? echo $row->ofsted_parent_review; ?></div>
          <div class="events"><? echo $row->events; ?></div>
          <div class="school_description"><? echo $row->school_description; ?></div>
    <?php
    }
}
}

?>        
</body>