Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Simple database

  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple database

    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.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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

    Code:
    <form method="post" action="search.php">
    <input type="text" name="search_term" />&nbsp;
    <input type="submit" value="submit" />
    </form>
    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.

    search.php:
    PHP Code:
    <?php
      
    if(isset($_POST["search_term"]))
      {
        
    $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($handle1000",")) !== FALSE) {
            if(
    array_key_exists  $data[0]  , $search_data ))
                
    $search_data[$data[0]] .= ", " $data[1];
            else 
                
    $search_data[$data[0]] = $data[1];
        }
        
    fclose($handle);
      }
      
      
    $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
    Last edited by timgolding; 05-11-2009 at 09:21 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Quote Originally Posted by timgolding View Post
    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?
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •