2017-04-26 26 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>