使用PHP和MySQL在自定义div标签内显示搜索结果

时间:2016-08-16 04:59:15

标签: php html mysql css

我正在搜索页面,我从用户那里获得输入,然后使用PHP,我连接到数据库,执行查询并尝试在页面上显示结果。我需要在div标签中显示结果,我需要显示各种搜索结果。

<?php
ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test1"; // Database name 
$tbl_name="postjob"; // Table name

// Create connection
$conn = new mysqli($host, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

$dbconn = mysqli_select_db($conn, $db_name) or die(mysql_error());
echo $dbconn;
echo "Connected to Database<br />";

$output = '';
$keywords=$_POST['keywords']; 
$location=$_POST['location'];

$keywords = mysqli_real_escape_string($conn, $keywords);
$location = mysqli_real_escape_string($conn, $location);

$sql="SELECT * FROM $tbl_name WHERE job_title LIKE '%$keywords%' OR job_type     
LIKE '%$keywords%' OR job_category LIKE '%$keywords%' OR job_tags LIKE  
'%$keywords%' OR description LIKE '%$keywords%' OR company_name LIKE 
'%$keywords%' OR location LIKE'%$location%'";
$result=mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Sign Up</title>
 </head>
<body>
  <section class="content_area">
    <div class="banner">
      <div class ="result">
        <table>
          <tr>
             <?php 
             while($jobsearch=mysqli_fetch_assoc($result)) {
             echo "<tr>";
             echo "<td>"."<h3>".$jobsearch['job_title']."</h3>"." 
             <br>".$jobsearch['company_name']."<br>".$jobsearch['description']."</td>";
             echo "</tr>";
            }

             ?>
        </tr>
      </table>
     </div>
   </div>
  </section>
</body>
</html>

我使用HTML表单页面中的帖子操作调用上面的PHP文件,其中包含关键字和位置文本框。我从数据库中获取job_title,company_name和description并显示它。我在这里用桌子来展示。但我想使用div我需要做一些造型。我尝试使用下面的代码替换表与div,但它没有显示任何结果。

<?php 
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div>"."<h3>".$jobsearch['job_title']."</h3>"." 
<br>".$jobsearch['company_name']."<br>".$jobsearch['description'].      
</div>";
}
?>

我期待div中的结果如下所示

job_title
company_name
description
<button>Apply</button>

我尝试了下面的代码,但它没有显示任何结果。

<?php
  while($jobsearch=mysqli_fetch_assoc($result)) {
  echo "<div><h3>".$jobsearch['job_title']."</h3></div>
   <div>".$jobsearch['company_name']."</div>
   <div>".$jobsearch['description']. "</div>";
  }
 ?>

我需要自定义这个div来做一些样式。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个。

<div class ="result">
        <table>
             <?php 
             while($jobsearch=mysqli_fetch_assoc($result)) {
             echo "<tr>";
             echo "<td>"."<h3>".$jobsearch['job_title']."</h3></td>"; 
             echo "</tr>";
             echo "<tr>";
             echo "<td>"."<h3>".$jobsearch['company_name']."</h3></td>";
             echo "</tr>";             
             echo "<tr>";
             echo "<td>"."<h3>".$jobsearch['description']."</h3></td>";
             echo "</tr>";                 
            echo "<br />";
            }
             ?>
      </table>
</div>

答案 1 :(得分:0)

希望它能奏效。

<ul>
 <?php
  while($jobsearch=mysqli_fetch_assoc($result)) {
   echo "<li>"."<h3>".$jobsearch['job_title']."</h3>"."</li>" 
   echo "<li>"."<h3>".$jobsearch['company_name']."</h3>"."</li>" 
   echo "<li>"."<h3>".$jobsearch['description']."</h3>"."</li>"
   echo "<button>Apply</button>";
  }
 ?>
</ul>