DanielTheRocketMan
DanielTheRocketMan

Reputation: 3249

MySQL search and PHP results

I am new in PHP and I am trying to make a basic search engine. 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. Each item summary should also present a link to a specific HTML page with details about this item (a page for each item). This is very similar for instance to stackoverflow or google or any other search engine.

Is this possible to make in 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):

<?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:

$theUser=mysql_fetch_assoc($theUserSearch); $theFirstName=$theUser['firstname']; $theLastName=$theUser['lastname']; $theUserName=$theUser['username']; $theSummary=$theUser['summary'];

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

$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.

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). 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?

Upvotes: 0

Views: 146

Answers (1)

mogosselin
mogosselin

Reputation: 343

I think what you should do is try to learn a small php framework like Codeigniter or Laravel. Basically, what it allows you to do "out of the box" is to map URLs to specific code.

For example, you could manage this with only 2 different part of code. The first part could display the search form. The second part could display the result. No need to generate any 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

note that the "*" is a wildcard and will always call the file "search_result.php" if the url starts with /search-result/...

After that, in the file "search_result.php", you can access the value of this wild card.


Questing for you : Why do you need to generate the HTML files, what's the need behind that? 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)


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.

Second option, if you want to do it more quickly, learn a little bit about rewriting URL with Apache. For example, check out : http://httpd.apache.org/docs/2.0/misc/rewriteguide.html (or search for Apache rewrite tutorial on Google)

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

<?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. 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?

Upvotes: 1

Related Questions