Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a URL

Status
Not open for further replies.

GavW

Programmer
Jan 13, 2006
58
GB
Hey all!

Was wondering if anybody could help me with this problem. I am attempting to search a URL for specific criteria and display the results of the search on the same page.

I have two files which I am working with: Index.php and WebSearch.inc.php class.

Index.php consists of the following form and results area:

<?php
define ("AUTHOR", "Me");
require_once("WebSearch.inc.php");

$search = new WebSearch ();
?>

<form method="GET" action="<?=$_SERVER['PHP_SELF']?>">
<div id="searchBox">
<table id="searchDetails">
<tr><td>Search Term</td><td><input class="text" name="term" type="text" value="some criteria" /></td></tr>
<tr><td>Website</td><td><input class="text" name="url" type="text" value=" /></td></tr>
<tr><td>Search Depth</td><td><select name="depth">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td></tr>
</table>
<input type="submit" value="start search" />
</div>
</form>

<?php $results = $search->return_result(); ?>
<h2>Showing <?=count($results)?> results</h2>
<div id="resultsBox">
<?php foreach ($results as $result) { ?>
<div id="result">
<h3 class="title"><?=$result['title']?></h3>
<em class="date"><?=$result['date']?></em>
<p class="desc"><?=$result['content']?></p>
<span class="url"><?=$result['url']?></span>
</div>
<?php } ?>

The WebSearch.inc.php class I have managed to get this far:

<?php

class WebSearch
{

public function set_url ($url)
{

}

public function set_term ($search)
{

}

public function return_result ()
{
$result = array();

/* Fake Results */
for ($i=0; $i<10; $i++)
$result[$i] = array (
"title" => ($i+1).": Page Title",
"date" => "Date Found: 15.08.06 00:00:00",
"content" => "Content. More content. Some more content.",
"url" => " );
return $result;
}
}

?>

Would somebody be able to advise me about which direction that I need to take?

Thanks
 
First, your script needs to behave differently when there is no data submitted to it (when the script is first run) and when there is (when the values from the form are submitted back).

Typically, such a script will look something like:

Code:
<?php
if (isset($_POST['some field from form']))
{
  //process submitted data
}
else
{
  //show form
}
?>

As for reading the web page and parsing its details [that is what you intend for this script to do, right?], you will need to read the contents of the page. See the code examples on the PHP online web pages for fread(), fsockopen(), or curl_exec() for three ways to do this.

Beyond that, it's difficult to advise you without more details. What, for example, is the value in the form field "depth" for?



Want the best answers? Ask the best questions! TANSTAAFL!
 
hmmmm which part of my code needs to go into that statement? Would that method mean echoing the form out?

forget the depth field actually I was just trying something.
 
Yes, if you want the form to only appear when you have no parameters.

If you want the form to always appear, leave out the else line and curly brackets and just put the form in normal html.
 
Different parts of your code will go in different places.

The part of your code that outputs your "blank" form will go in the else-block. The part of your code that processes the input from that form will go in the if-block.



Want the best answers? Ask the best questions! TANSTAAFL!
 
One advantage of putting your form in the else block is that you can repopulate it with your parameters. Many times, people will want to repeat a search with slightly different input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top