View Full Version : Simple database

05-09-2009, 02:12 PM
Ok, I know zero about databases, so please, any responses with code will be greatly appreciated. (My HTML knowledge isn't even that great, but I'm learning.)

I wish to make a flatfile, indexing webpages with their URLs.
Something like:

webpage1, www.domain.com/webpage1.html;
webpage2, www.domain.com/webpage2.html;
webpage3, www.domain.com/webpage3.html;
webpage4, www.domain.com/webpage4.html;

Then I want a search box on my webpage, which I am guessing would be a 'form' with a "search" button.

I want to search for "webpage3" and have the URL come up.

05-09-2009, 07:55 PM
You can't do that with html/css and would need a server side scripting language.

So what you need to do is find out which if any are installed on your server. and choose one of those. If you find out that php is available and you choose that one then you can go onto the php forum section and I will meet you there.

05-09-2009, 07:56 PM
Actually you might get called up fro cross posting if you re post this on the php forum. So just PM me if you choose php.

05-11-2009, 12:57 AM
Ok just to let everyone know I was PM today and target practice said he does have PHP installed. So here goes:

So the first step is the search box which is a simple html form. Like so

<form method="post" action="search.php">
<input type="text" name="search_term" />&nbsp;
<input type="submit" value="submit" />

Style that how you like. But notice the action attribute is search.php. That is the name of the php file that will do the search. That file will be called when the user hits submit and the forms post data will be sent to that page. Here is what the php file will look like.


$handle = fopen("flatfile.txt", "r"); // This must be replace with the path and file name of your flat file. file returns an array
$search_data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(array_key_exists ( $data[0] , $search_data ))
$search_data[$data[0]] .= ", " . $data[1];
$search_data[$data[0]] = $data[1];

$result = @$search_data[trim($_POST['search_term'])] or $result ="No results";
echo "Results for " .$_POST["search_term"].": ". $result;


This will work i have tested it as long as you call the files the right things will be fine. The flatfile.txt must be CSV. Which it is but doesn't need the ; at the end of the lines. This code works provided all three files are in the same directories if you need to put them in different directories then change the file paths.

Well it does what you want. It will allow as many keywords for each page as you like and you can even have the same keyword for more then one page.

I have to say though this is not the best way to search your site. If it was me I would think of a way of looking inside the actual pages for keywords. Maybe I'd have a database with all the non html (textual) content for each valid url. Then my search would query the database for those words phrases looking at the page content and the URL name to get results. Something like grep. Well its up to you. Have fun

05-11-2009, 04:49 AM
Ok just to let everyone know I was PM today and target practice said he does have PHP installed.

I hadn't realised the OP's name and what you wrote there sounded very funny/scary. It was like, you had to use him for target practice before he told you he had php? :D