2017-04-26 48 views
1

我正在試圖製作一個簡單的搜索引擎,並且結果以現在的形式顯示得很好。我唯一的問題是我希望它更有形象。在簡單的搜索引擎內創建表格+結果的最大限制

這是代碼,因爲它代表(我也有另一種.PHP那裏得到的searchval,searchfunction和jQuery)

<?php 
mysql_connect ("localhost","root","xxxxxxx") or die ("Connectionissues"); 
mysql_select_db ("xxxxxxxx") or die("Can't find database"); 
$output = ''; 

if(isset($_POST['searchVal'])) { 
$searchq = $_POST['searchVal']; 
$searchq = preg_replace ("#^0-9a-z#^1"," ",$searchq); 

$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE 
'%$searchq%'") or die("Search incomplete!"); 
$count = mysql_num_rows ($query); 

if($count == 0){ 
    $output = 'Order have never been made before'; 



}else{ 

    while($row = mysql_fetch_array($query)) { 

     $idproduct = $row['idProduct']; 
     $idorder = $row['idOrder']; 
     $title = $row['title']; 
     $qty = $row['qty']; 


     $output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.' 
</div>'; 

     } 




    if($_POST['searchVal'] == NULL) { 
      $output = ""; 
     } 

    } 

} 

echo ($output); 
?> 

要限制SearchResult所我試過在之前進行的if語句聲明如下:

if($count = <100){ 
    $output = 'Too many results!'; 

而對於我已經嘗試了各種方法的表,我總是最終使簡單的HTML表,但我不能得到的搜索結果到四列內張貼。

任何關心幫助初學者?

提前,感謝您的閱讀和任何可能的解決方案!

+0

你需要什麼? –

+1

在SQL語句的末尾添加限制子句。即。 '''LIMIT 20'''。 –

+0

@AhmedGinani我需要一個代碼來說明如何格式化表格以及在哪裏放置它,以便它將搜索結果發佈到具有四個柱的表格中。 (idProduct,idOrder,標題和數量) – easyquestions

回答

1

月1日,你應該真的考慮使用PPS : Prepared Parameterized Statements之前次數變量,把條件循環。這將有助於Preventing SQL injection

如果您要限制和訂購,請使用查詢,因此,MySQL : LIMIT Query Optimization很有用。

對於你的要求,使用這樣的:

<?php 

-> SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' ORDER BY title ASC, quantity DESC LIMIT 20 
// THIS IS NOT SAFE as you trust user data !!! 
// then you have 20 results already ordered 
// here, I used title alphabetical order + the most avalaible quantity, but you adapt it... 

if ($result_of_num_rows > 0) { /* we have results */ 
echo"<table>"; // only raw, use a nicely formatted html output :) 

while($row = mysql_fetch_array($query)) { 

    $idproduct = $row['idProduct']; 
    $idorder = $row['idOrder']; 
    $title = $row['title']; 
    $qty = $row['qty']; 

echo"<tr> 
     <td> $idorder </td> 
     <td> $idproduct </td> 
     <td> $title </td> 
     <td> $qty </td> 
</tr>"; 
} 
echo"</table>"; 
} 
else { echo"nothing yet !"; } 
?> 

更好的將被利用的「新」標準choosing an API,你必須給出一個例子(我希望)會幫助你選擇新路徑:)

<?php 

error_reporting(E_ALL); ini_set('display_errors', 1); /* PHP will help us */ 

/* connexion to db */ 
$mysqli = mysqli_connect("$host", "$user", "$pwd", "$db"); 

if (mysqli_connect_errno()) { echo "Error connecting to DB : " . mysqli_connect_error($mysqli); } 

$param = "%{$_POST['searchq']}%"; 

$query = " SELECT idProduct, idOrder, title, qty FROM ds_OrderItem WHERE idProduct LIKE ? ORDER BY title ASC, quantity DESC LIMIT 20 "; 

$stmt = $mysqli->prepare($query); /* prepare query */ 

$stmt->bind_param("s", $param); /* bind param wil sanitize */ 

print_r($stmt->error_list); /* check for error -> can be removed later */ 
print_r($stmt->get_warnings()); /* check for error -> can be removed later */ 
print_r($stmt->error); /* check for error -> can be removed later */ 

$results = $stmt->execute(); /* execute query */ 
$stmt->bind_result($idProduct, $idOrder, $title, $qty); /* bounded results */ 
$stmt->store_result(); 

if ($stmt->num_rows > 0) { /* we have results */ 

echo"<table>"; // start table 

while($stmt->fetch()){ /* loop through results */ 

echo"<tr> 
    <td> $idOrder </td> 
    <td> $idProduct </td> 
    <td> $title </td> 
    <td> $qty </td> 
    </tr>"; 

    } 

echo"</table>"; 

} 
else 
{ echo"[ no data ]"; } 
?> 
+0

@easyquestions:thx對於UV :)只是FMI你實際使用哪種方式?第一(你的,修改)還是第二(使用PPS)? – OldPadawan

0

你可能的解決方案是在下面,你可以關閉,而所有的

<table> 
    <?php while($row = mysql_fetch_array($query)) { 
     $idproduct = $row['idProduct']; 
     $idorder = $row['idOrder']; 
     $title = $row['title']; 
     $qty = $row['qty']; 
    ?> 
    <tr> 
      <td><?php echo $idproduct; ?></td> 
      <td><?php echo $idorder; ?></td> 
      <td><?php echo $title; ?></td> 
      <td><?php echo $qty; ?></td> 
    </tr> 
    <?php } ?> 
    </table>