建立有效的mysql ajax搜索

时间:2010-03-01 14:06:59

标签: php ajax

我目前正在研究一个小型数据库的搜索引擎,但感觉好像我的构建它的方法到目前为止有点膨胀。有没有人有任何关于如何简化ajax代码的建议?这是我的ajax.php文件....

<p><a href="#clients" class="form">Clients</a> &nbsp;
<a href="#files" class="form">Files</a> &nbsp;
<a href="#vehicles" class="form">Vehicles</a> &nbsp;
<a href="#comments" class="form">Comments</a> &nbsp;
<a href="#solicitors" class="form">Solicitors</a> &nbsp;</p>

<hr width='75%' size='1' color='#F5F5F5' align='left'/>






<!--- CLIENTS ---->

<p style='font-size: 16px;'><b><a name="clients">Clients</a></b></p>

<?php require_once('Connections/speedycms.php');
if(isset($_GET['search_word']))
{
$search_word=$_GET['search_word'];
$sql=mysql_query("SELECT * FROM tbl_accident WHERE clientName LIKE '%$search_word%' ORDER BY clientName ASC");
$count=mysql_num_rows($sql);
if($count > 0)
{
while($row=mysql_fetch_array($sql))
{
$msg=$row['clientName'];
$bold_word='<b>'.$search_word.'</b>';
$final_msg = str_ireplace($search_word, $bold_word, $msg);
?>
<p><?php echo "
<a class='box' href='viewclient.php?id=".$row['id']."'><span style='font-size: 12px;'>".$final_msg."</span><BR>
<span style='font-size: 10px;'>".$row['address']."</span></a><BR>
<a href='manageclient.php?id=".$row['id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='viewclient.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='editclient.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='printclient.php?id=".$row['id']."'><img src='resources/print.png' border='0'></a>&nbsp;
<a href='deleteclient.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?></p>
<?php
}
}
else
{
echo "<p>No results (road traffic accident)<BR>
</p>";
}
}
?>

<?php require_once('Connections/speedycms.php');
if(isset($_GET['search_word']))
{
$search_word6=$_GET['search_word'];
$sql6=mysql_query("SELECT * FROM tbl_personalinjury WHERE clientName LIKE '%$search_word%' ORDER BY clientName ASC");
$count6=mysql_num_rows($sql6);
if($count6 > 0)
{
while($row=mysql_fetch_array($sql6))
{
$msg6=$row['clientName'];
$bold_word6='<b>'.$search_word6.'</b>';
$final_msg6 = str_ireplace($search_word6, $bold_word6, $msg6);
?>
<p><?php echo "
<a class='box' href='viewclient2.php?id=".$row['id']."'><span style='font-size: 12px;'>".$final_msg6."</span><BR>
<span style='font-size: 10px;'>".$row['address']."</span></a><BR>
<a href='manageclient2.php?id=".$row['id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='viewclient2.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='editclient2.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='printclient2.php?id=".$row['id']."'><img src='resources/print.png' border='0'></a>&nbsp;
<a href='deleteclient2.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?></p>
<?php
}
}
else
{
echo "<p>No results (personal injury)<BR>
</p>";
}
}
?>

<hr width='75%' size='1' color='#F5F5F5' align='left'/>






<!--- FILES ---->

<p style='font-size: 16px;'><b><a name="files">Files</a></b></p>

<?php require_once('Connections/speedycms.php');        
if(isset($_GET['search_word']))
{
$search_word2=$_GET['search_word'];
$sql2=mysql_query("SELECT * FROM tbl_accidentfiles WHERE name LIKE '%$search_word%' ORDER BY name ASC");
$count2=mysql_num_rows($sql2);
if($count2 > 0)
{
while($row=mysql_fetch_array($sql2))
{
$msg2=$row['name'];
$bold_word2='<b>'.$search_word2.'</b>';
$final_msg2 = str_ireplace($search_word2, $bold_word2, $msg2);
?>
<p><?php echo "
<a class='box' href='viewfile.php?id=".$row['id']."'><span style='font-size: 12px;'>".$final_msg2."</span><BR>
<span style='font-size: 10px;'>".$row['size']."K&nbsp;".$row['type']."</span></a><BR>
<a href='downloadfile.php?id=".$row['id']."'><img src='resources/download.png' border='0'></a>&nbsp;
<a href='viewfile.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='deletefile.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (road traffic accident)<BR></p>";
}
}
?>
<?php require_once('Connections/speedycms.php');        
if(isset($_GET['search_word']))
{
$search_word7=$_GET['search_word'];
$sql7=mysql_query("SELECT * FROM tbl_personalinjuryfiles WHERE name LIKE '%$search_word%' ORDER BY name ASC");
$count7=mysql_num_rows($sql7);
if($count7 > 0)
{
while($row=mysql_fetch_array($sql7))
{
$msg7=$row['name'];
$bold_word7='<b>'.$search_word7.'</b>';
$final_msg7 = str_ireplace($search_word7, $bold_word7, $msg7);
?>
<p><?php echo "
<a class='box' href='viewfile3.php?id=".$row['id']."'><span style='font-size: 12px;'>".$final_msg7."</span><BR>
<span style='font-size: 10px;'>".$row['size']."K&nbsp;".$row['type']."</span></a><BR>
<a href='downloadfile3.php?id=".$row['id']."'><img src='resources/download.png' border='0'></a>&nbsp;
<a href='viewfile3.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='deletefile3.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (personal injury)<BR></p>";
}
}
?>
<?php require_once('Connections/speedycms.php'); 
if(isset($_GET['search_word']))
{
$search_word3=$_GET['search_word'];
$sql3=mysql_query("SELECT * FROM tbl_vehiclefiles WHERE name LIKE '%$search_word%' ORDER BY name ASC");
$count3=mysql_num_rows($sql3);
if($count3 > 0)
{
while($row=mysql_fetch_array($sql3))
{
$msg3=$row['name'];
$bold_word3 ='<b>'.$search_word3.'</b>';
$final_msg3 = str_ireplace($search_word3, $bold_word3, $msg3);
?>
<p><?php echo "
<a class='box' href='viewfile2.php?id=".$row['id']."'><span style='font-size: 12px;'>".$final_msg2."</span><BR>
<span style='font-size: 10px;'>".$row['size']."K&nbsp;".$row['type']."</span></a><BR>
<a href='downloadfile2.php?id=".$row['id']."'><img src='resources/download.png' border='0'></a>&nbsp;
<a href='viewfile2.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='deletefile2.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (vehicle)<BR></p>";
}
}
?>

<hr width='75%' size='1' color='#F5F5F5' align='left'/>

<p style='font-size: 16px;'><b><a name="vehicles">Vehicles</a></b></p>








<!--- VEHICLES ---->

<?php require_once('Connections/speedycms.php'); 

if(isset($_GET['search_word']))
{
$search_word6=$_GET['search_word'];
$sql6=mysql_query("SELECT * FROM tbl_vehicles WHERE vehicleMake LIKE '%$search_word%' ORDER BY vehicleMake DESC");
$count6=mysql_num_rows($sql6);
if($count6 > 0)
{
while($row=mysql_fetch_array($sql6))
{
$msg6=$row['vehicleMake']."&nbsp;".$row['vehicleModel'];
$bold_word6='<b>'.$search_word6.'</b>';
$final_msg6 = str_ireplace($search_word6, $bold_word6, $msg6);
?>
<p><?php echo "
<span style='font-size: 12px;'><a class='box' href='viewvehicle.php?id=".$row['id']."'>".$final_msg6."</a></span><BR>
<a href='managevehicle.php?id=".$row['id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='viewvehicle.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='editvehicle.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='deletevehicle.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results<BR></p>";
}
}
?>

<hr width='75%' size='1' color='#F5F5F5' align='left'/>

<p style='font-size: 16px;'><b><a name="comments">Comments</a></b></p>








<!--- COMMENTS ---->

<?php require_once('Connections/speedycms.php');

function do_TrimString($string, $MaxSTRLEN = null) {
   if ($MaxSTRLEN == null) { global $MaxSTRLEN; }
   if (strlen($string) > $MaxSTRLEN) {
      $string = substr($string, 0, $MaxSTRLEN);
      $end = strrpos($string, ' ');
      if ($end === false) {
         $end = $MaxSTRLEN;
         }
      $string = substr($string, 0, $end) . '...';
      }
   return $string;
   }

if(isset($_GET['search_word']))
{
$search_word4=$_GET['search_word'];
$sql4=mysql_query("SELECT * FROM tbl_comment WHERE comment LIKE '%$search_word%' ORDER BY comment ASC");
$count4=mysql_num_rows($sql4);
if($count4 > 0)
{
while($row=mysql_fetch_array($sql4))
{
$msg4=$row['comment'];
$bold_word4='<b>'.$search_word4.'</b>';
$final_msg4 = str_ireplace($search_word4, $bold_word4, $msg4);
?>
<p><?php echo "
<a href='viewcomment.php?id=".$row['id']."' class='box'><span style='font-size: 12px;'>".do_TrimString($final_msg4, 40)."</span><BR>
<span style='font-size: 10px;'>".$row['date']."</span></a><BR>
<a href='viewcomment.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='manageclient.php?id=".$row['client_id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='editcomment.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='deletecomment.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;
<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (road traffic accident)<BR></p>";
}
}
?>
<?php
if(isset($_GET['search_word']))
{
$search_word4=$_GET['search_word'];
$sql4=mysql_query("SELECT * FROM tbl_comment3 WHERE comment LIKE '%$search_word%' ORDER BY comment ASC");
$count4=mysql_num_rows($sql4);
if($count4 > 0)
{
while($row=mysql_fetch_array($sql4))
{
$msg4=$row['comment'];
$bold_word4='<b>'.$search_word4.'</b>';
$final_msg4 = str_ireplace($search_word4, $bold_word4, $msg4);
?>
<p><?php echo "
<a href='editcomment3.php?id=".$row['id']."' class='box'><span style='font-size: 12px;'>".do_TrimString($final_msg4, 40)."</span><BR>
<span style='font-size: 10px;'>".$row['date']."</span></a><BR>
<a href='manageclient2.php?id=".$row['client_id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='editcomment3.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='deletecomment3.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;
<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (personal injury)<BR></p>";
}
}
?>
<?php
if(isset($_GET['search_word']))
{
$search_word4=$_GET['search_word'];
$sql4=mysql_query("SELECT * FROM tbl_comment2 WHERE comment LIKE '%$search_word%' ORDER BY comment ASC");
$count4=mysql_num_rows($sql4);
if($count4 > 0)
{
while($row=mysql_fetch_array($sql4))
{
$msg4=$row['comment'];
$bold_word4='<b>'.$search_word4.'</b>';
$final_msg4 = str_ireplace($search_word4, $bold_word4, $msg4);
?>
<p><?php echo "
<a href='editcomment2.php?id=".$row['id']."' class='box'><span style='font-size: 12px;'>".do_TrimString($final_msg4, 40)."</span><BR>
<span style='font-size: 10px;'>".$row['date']."</span></a><BR>
<a href='managevehicle.php?id=".$row['veh_id']."'><img src='resources/manage.png' border='0'></a>&nbsp;
<a href='editcomment2.php?id=".$row['id']."'><img src='resources/edit.png' border='0'></a>&nbsp;
<a href='deletecomment2.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;
<BR>"; ?>
</p>
<?php
}
}
else
{
echo "<p>No results (vehicle)<BR></p>";
}
}
?>
<hr width='75%' size='1' color='#F5F5F5' align='left'/>










<p style='font-size: 16px;'><b><a name="solicitors">Solicitors</a></b></p>

<!--- 5) tbl_solicitors search ---->

<?php require_once('Connections/speedycms.php'); 

if(isset($_GET['search_word']))
{
$search_word5=$_GET['search_word'];
$sql5=mysql_query("SELECT * FROM tbl_solicitors WHERE solicitorName LIKE '%$search_word%' ORDER BY solicitorName ASC");
$count5=mysql_num_rows($sql5);
if($count5 > 0)
{
while($row=mysql_fetch_array($sql5))
{
$msg5=$row['solicitorName'];
$bold_word5='<b>'.$search_word5.'</b>';
$final_msg5 = str_ireplace($search_word5, $bold_word5, $msg5);
?>
<p><?php 
if($row['solicitorName']=='None')
{
echo "<p>No results<BR></p>";   
}
else
echo "
<a class='box' href='viewsolicitor.php?id=".$row['solicitorName']."'><span style='font-size: 12px;'>".$final_msg5."</span><BR>
<span style='font-size: 10px;'>".$row['solicitorDetail']."</span></a><BR>
<a href='viewsolicitor.php?id=".$row['id']."'><img src='resources/view.png' border='0'></a>&nbsp;
<a href='deletesolicitor.php?id=".$row['id']."'><img src='resources/no.png' border='0'></a>&nbsp;<BR>
"; 
?>
</p>
<?php
}
}
else
{
echo "<p>No results<BR></p>";
}
}
?>

<hr width='75%' size='1' color='#F5F5F5' align='left'/>

提前感谢你

1 个答案:

答案 0 :(得分:1)

除了确保您的代码正确缩进之外;)我还会考虑更好地分离您的演示文稿(HTML)和业务逻辑。

我不是在谈论完整的模板系统或类似的东西,但如果你至少可以在文件顶部收集处理代码(查询等)并将所有演示代码移到底部,我想你会更好地掌握你到目前为止的实际目标,以及你可以做些什么来改进它。