从数据库搜索结果调用下一个/上一个查询

时间:2016-11-09 21:36:55

标签: javascript php html mysql

我有一个数据库,其中包含与其他表格链接的客户信息。

我试图创建一个带搜索选项的网页来搜索数据库 我的搜索功能正常运行。

我希望能够让输出一次只显示一个结果。 我也做到了。

我希望能够使用“下一个”和“上一个”按钮在该查询的不同结果之间循环。
根据我的理解(有点新的编码),搜索完成后,所有数据都被放入一个数组中。此阵列已分配ID 所以搜索"史密斯"可能有10个结果。阵列中的结果为0-9 我的逻辑是让初始搜索显示结果为0然后当按下Next时函数将RecNum增加1然后重新显示。代码设置为由RecNum搜索,因此Next将显示结果2(数组中的第1行)。

让我的工作就是问题所在。我甚至不确定如何接近这一目的。 (我是否提到我是编码的新手。)

现在我正在使用Javascript确保按下按钮上的添加和减少功能正常。我失去了如何将新的RecNum转换为PHP $ RecNum并重新显示数据的方法。
任何有关代码的帮助将不胜感激。我并不喜欢使用JavaScript。我愿意使用_Sessions或AJAX。什么都行得很好。

这是我现在的代码。 搜索工作和新搜索按钮工作,但是现在所有Prev和Next按钮都会增加/减少RecNum的数量并将其输出到页面底部。



<?php
require 'Connect.php';
$output = '';
$RecNum = 0;
//$Prev = $RecNum - 1;
//$Next = $RecNum + 1;
//$_Post['Prev'] = $Prev;
//$_Post['Next'] = $Next;


//collect
if(isset($_POST['search'])) {
  $searchq = $_POST['search'];
  // Joins and tables with realationships and allows for searching of the searchable areas
  
  $sql="SELECT aps.*, customers.*, equipment.*, routableip.*
    FROM customers  
	LEFT JOIN aps ON customers.AP_ID = aps.AP_ID
    LEFT JOIN equipment ON equipment.Cust_ID = customers.Cust_ID
    LEFT JOIN routableip ON routableip.Cust_ID = customers.Cust_ID
    WHERE (equipment.ESN LIKE '%$searchq%') 
    OR (equipment.WMAC LIKE '%$searchq%') 
	OR (equipment.UnitIP LIKE '%$searchq%') 
    OR (equipment.Cust_ID LIKE '%$searchq%') 
	OR (equipment.AP_ID LIKE '%$searchq%')
	OR (equipment.SN LIKE '%$searchq%')
	OR (customers.Account LIKE '%$searchq%')
	OR (customers.AlternatePhone LIKE '%$searchq%')	
	OR (customers.Phone LIKE '%$searchq%')
	OR (Routableip.Routable_IP LIKE '%$searchq%')
	OR (customers.Address LIKE '%$searchq%')
    OR (customers.LastName LIKE '%$searchq%')";

  
  $query= mysqli_query($conn, $sql);
  
  $count = mysqli_num_rows($query);
  if($count == 0){
    $output = 'There was no search results!';
  
  
  
  mysqli_free_result($query);
  }
  else{      
    //while($row = mysqli_fetch_array($query)) {
		mysqli_data_seek($query,$RecNum);
		$row = mysqli_fetch_row($query);
		
		$Account = $row[11];
		$LastName = $row[3];
		$FirstName = $row[4];
		$Address = $row[5];
		$City = $row[6];
		$State = $row[7];
		$Zip = $row[8];
		$Phone = $row[9];
		$AlternatePhone = $row[10];

		$UnitIP = $row[22];
		$BridgeMode = $row[23];
		$RIP = $row[27];
		$AP = $row[1];
		
		
		$NewDate = $row[21];
		$SimpleModel = $row[18];
		$Manufacturer = $row[19];
		$Frequency = $row[20];
		$SN = $row[16];
		$ESN = $row[15];
		$WMAC = $row[14];
			
		
	
      $output .= '<div>
				<br> <h2>Customer Information</h2>
				<br> Account Number: '.$Account.'
				<br> Name: '.$LastName.', '.$FirstName.'
				<br> Address: '.$Address.'
				<br> City: '.$City.' State: '.$State.' Zip: '.$Zip.'
				<br> Phone #: '.$Phone.'
				
				<br> <h2>Connection Information</h2>
				<br> Unit IP: '.$UnitIP.'
				<br> Bridge Mode: '.$BridgeMode.'
				<br> Routable IP: '.$RIP.'
				<br> AP: '.$AP.'
				
				<br> <h2>Unit Information</h2>
				<br> New Date: '.$NewDate.' Model: '.$SimpleModel.'
				<br> Manufacturer: '.$Manufacturer.' Frequency: '.$Frequency.'
				<br> SN: '.$SN.'
				<br> ESN: '.$ESN.'
				<br> WMAC: '.$WMAC.'
				</div>';
		
	
  
 }
			mysqli_free_result($query);
 }
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content=text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>Search</title>
<script type="text/javascript">
var RecNum = 0;
function add(){
     RecNum++;
     document.getElementById('display').innerHTML = RecNum;
}
function sub(){
	RecNum--;
	document.getElementById('display').innerHTML = RecNum;
}
</script>
</head>

<body>
<form action="" method="post">
	<input type="text" name="search" placeholder="Search for Information" autocomplete="off"/>
	<input type="submit" value="Search" />
	
	
</form>

<?php print("$output");?>

	<button class="button Prev1" onclick="sub()" style="vertical-align:middle"><span>Prev</span></button>
	<a href="" class="button button3" style="vertical-align:middle"><span>New Search</span></a></button>
	<button class="button Next2" onclick="add()" style="vertical-align:middle"><span>Next</span></button>
	
<br>
	
<div id="display"><script type="text/javascript">document.write(RecNum);</script></div>

	
PHP:<?php print($RecNum);?>


</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案