简体   繁体   English

MySQL搜索和PHP结果

[英]MySQL search and PHP results

I am new in PHP and I am trying to make a basic search engine. 我是PHP的新手,正在尝试制作一个基本的搜索引擎。 After every search, I would like to present a summary of the results in a HTML page with all the items that satisfy the query. 每次搜索之后,我想在HTML页面中显示结果摘要,其中包含所有满足查询条件的项目。 Each item summary should also present a link to a specific HTML page with details about this item (a page for each item). 每个项目摘要还应提供指向特定HTML页面的链接,其中包含有关该项目的详细信息(每个项目的页面)。 This is very similar for instance to stackoverflow or google or any other search engine. 例如,这与stackoverflow或google或任何其他搜索引擎非常相似。

Is this possible to make in PHP? 这可以用PHP制作吗?

The only way that I found to do this is generating automatically the HTML summary code for each search using a code like this (and it works), for the summary page (I have to do something similar to generate the HTML pages): 我发现做到这一点的唯一方法是,使用摘要代码页的这种搜索(并且它可以正常工作)为每次搜索自动生成HTML摘要代码(我必须做类似的事情才能生成HTML页):

<?php
function frontPagePresentation($theFirstName,$theLastName,$theUserName,$theSummary)
{

    $fileName='ShowSearch'.$theUserName.'.html';

    $theHTML="profileO".$theUserName.".html";

    $theLinkText=$theFirstName." ".$theLastName;

    $theFigureName="/NetBeansProjects/NewPhpProject/photos/".$theUserName;

    $thePos=strpos($theSummary, ' ', 200);
    $theSummary=substr($theSummary,0,$thePos); 
    $theSummary=": ".$theSummary;



    //$file = fopen(,"w");

    $str1="<table border=\"0\" width=\"75%\">";

    echo $str1;

if (!($fp = fopen($fileName, 'w'))) {
    return;
}

    $str1="<table border=\"0\" width=\"75%\">";


$str2="<tr>";
$str3="<td width=\"20%\">";
$str4="<img src= $theFigureName  alt=\" \" width= \"100\">"; 
$str5="</td>";
$str6="<td width=\"80%\">";
$str7="<a href=  $theHTML >  $theLinkText  </a>"; 
$str8= $theSummary;
$str9="</td>";
$str10="</tr>";
$str11="</table>";    




$len = fprintf($fp, '%s',$str1."\n");
$len = fprintf($fp, '%s',$str2."\n");
$len = fprintf($fp, '%s',$str3."\n");
$len = fprintf($fp, '%s',$str4."\n");
$len = fprintf($fp, '%s',$str5."\n");
$len = fprintf($fp, '%s',$str6."\n");
$len = fprintf($fp, '%s',$str7."\n");
$len = fprintf($fp, '%s',$str8."\n");
$len = fprintf($fp, '%s',$str9."\n");
$len = fprintf($fp, '%s',$str10."\n");
$len = fprintf($fp, '%s',$str11."\n");






}
?>

After that I load this page in a HTML page (the HTML page that will show the summaries) using something like this: 之后,我使用以下方法将此页面加载到HTML页面(将显示摘要的HTML页面)中:

$theUser=mysql_fetch_assoc($theUserSearch); $ theUser = mysql_fetch_assoc($ theUserSearch); $theFirstName=$theUser['firstname']; $ theFirstName = $ theUser [ '姓名']; $theLastName=$theUser['lastname']; $ theLastName = $ theUser [ '姓氏']; $theUserName=$theUser['username']; $ theUserName = $ theUser [ '用户名']; $theSummary=$theUser['summary']; $ theSummary = $ theUser [ '摘要'];

frontPagePresentation($theFirstName,$theLastName,$theUserName,$theSummary); frontPagePresentation($ theFirstName,$ theLastName,$ theUserName,$ theSummary);

$theHTML="ShowSearch".$theUserName.".html"; $ theHTML = “ShowSearch” $ theUserName。 “HTML”;

require($DOCUMENT_ROOT . $theHTML);

As I told, I am new in PHP, but this does not seem a very clever way to do that. 就像我说的那样,我是PHP的新手,但是这似乎并不是一个很聪明的方法。

Every time I have to generate a lot summaries and all the homepages associated to these summaries (since after sending the browser to HTML, I do not have access to them anymore - I am in the client side) and after every search I have to delete them (both summaries and HTML pages related to each item). 每次我必须生成大量摘要以及与这些摘要关联的所有主页时(由于将浏览器发送为HTML之后,我将无法再访问它们-我在客户端),并且每次搜索后都必须删除它们(与每个项目相关的摘要和HTML页面)。 Furthermore, It is not clear how to choose the correct time to delete this (how to know if these results are still being used?) 此外,尚不清楚如何选择正确的时间删除它(如何知道这些结果是否仍在使用?)

What is the correct way to do this? 正确的方法是什么?

I think what you should do is try to learn a small php framework like Codeigniter or Laravel. 我认为您应该做的是尝试学习像Codeigniter或Laravel这样的小型php框架。 Basically, what it allows you to do "out of the box" is to map URLs to specific code. 基本上,它允许您“开箱即用”执行的操作是将URL映射到特定代码。

For example, you could manage this with only 2 different part of code. 例如,您可以只用2个不同的代码部分来管理它。 The first part could display the search form. 第一部分可以显示搜索表单。 The second part could display the result. 第二部分可以显示结果。 No need to generate any html. 无需生成任何HTML。 If you need to "cache" your data, you can do it with the framework I listed. 如果您需要“缓存”数据,则可以使用我列出的框架来完成。

So, in summary, what you could do with those framework is something like : - map the url /search to the file search.php - map the urls /search-result/* to the file search_result.php 因此,总而言之,您可以使用这些框架进行以下操作:-将url / search映射到文件search.php-将urls / search-result / *映射到文件search_result.php

note that the "*" is a wildcard and will always call the file "search_result.php" if the url starts with /search-result/... 请注意,“ *”是通配符,如果url以/ search-result /开头,它将始终调用文件“ search_result.php”。

After that, in the file "search_result.php", you can access the value of this wild card. 之后,在文件“ search_result.php”中,您可以访问该通配符的值。


Questing for you : Why do you need to generate the HTML files, what's the need behind that? 向您询问:为什么需要生成HTML文件,其背后的需求是什么? Is it for performance? 是为了表现吗? to share the results with somebody.. ? 与他人分享结果。

(Sorry, I would use the comment feature but my reputation is not at 50 yet) (对不起,我会使用评论功能,但我的声誉还不到50岁)


Edit 2 : What I would suggest you do is either to try to learn Laravel (getting "cooler") or code igniter (lightweigh) if you have some free time. 编辑2:我建议您做的是尝试学习Laravel(获取“冷却器”)或尝试使用代码点火器(轻量化),如果您有空闲时间的话。

Second option, if you want to do it more quickly, learn a little bit about rewriting URL with Apache. 第二种选择,如果您想更快地做,请学习一些有关用Apache重写URL的知识。 For example, check out : http://httpd.apache.org/docs/2.0/misc/rewriteguide.html (or search for Apache rewrite tutorial on Google) 例如,请查看: http : //httpd.apache.org/docs/2.0/misc/rewriteguide.html (或在Google上搜索Apache重写教程)

What you'll want to achieve with that is : - get all URL that beings with /user/ and send it to your search_user.php?stringToSearch=$1 php script - note that $1 will be replaced by whatever follow the "/user/" part of the url - so, if you type /user/bob, then the URL will basically be search_user.php?stringToSearch=bob (but it will be a "silent redirect", so the user will still see /user/bob in the URL. - then, in your search_user.php script, you would do 您要实现的目标是:-使用/ user /获取所有存在的URL,并将其发送到search_user.php?stringToSearch = $ 1 php脚本-注意$ 1将被替换为“ / user / ”部分的网址-因此,如果您输入/ user / bob,则该网址基本上将为search_user.php?stringToSearch = bob(但它将是“无提示重定向”,因此用户仍会看到/ user / bob在网址中-然后,在您的search_user.php脚本中

<?php
$stringToSearch = $_GET['stringToSearch'];
$theUser=mysql_fetch_assoc($stringToSearch); 
...

So that's for your "detail" part of your search engine. 这就是您搜索引擎“详细”部分的内容。

For the "search and display" part, create a new script (like index.php) and do basically what you posted at the beginning. 对于“搜索和显示”部分,创建一个新脚本(如index.php),并基本上执行您在开始时发布的内容。 But instead of this line : 但是代替这一行:

$str7="<a href='$theHTML'>  $theLinkText  </a>"; 

use this 用这个

$str7="<a href='http://www.yoursite.com/user/$theUserName'>  $theLinkText  </a>"; 

Does that make sense? 那有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM