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
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Search function using static pages

    I need to search a bunch of files (.php files) for whatever a user searches for. I can do this easily enough from a database but i cant find a way to do it on a file.

    Can anyone help me figure out what i need to do?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    What are you searching for?
    and can you show us one of the pages with an example of what you might search?

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    If you look at this page as example
    http://221test.co.uk/AWRX/index.php

    I need it so if someone searches for e.g. 'Zero Tolerance Approach' It would show a link to that page.

    So far ive got this which seems to work but theres probably a better-easier way to do it

    PHP Code:
    <?php

    $Files 
    = array('index.php' => 'index.php'
                   
    'cpcs.php'  => 'cpcs.php',
                   
    'risk.php'  => 'riskassessments.php',
                   
    'env.php'   => 'enviromentalpolicy.php',
                   
    'hns.php'   => 'healthandsafety.php');
                  
    $Links = array('index.php' => 'Homepage'
                   
    'cpcs.php'  => 'CPCS',
                   
    'risk.php'  => 'Risk Assessments',
                   
    'env.php'   => 'Enviromental Policy',
                   
    'hns.php'   => 'Health And Safety');

    foreach (
    $Files as $f => $l)
    {
        
    $lines implode(''file('view/'.$f));

        
    //foreach ($lines as $line_num => $line)
        
               
    if(strstr(strtolower($lines),strtolower($_GET['q']))) 
            {
                echo 
    "<a href='$l?hl=".$_GET['q']."'>".$Links[$f]."</a><br />";
            }
            else
            {
                
    //echo "not found.<br />";
            
    }
        
    }

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Without having your content in a database (like MySQL), I think you're on
    the right track with what you did. A lot of looping, but that's the way it will be.
    That's why MySQL is so nice.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    I've never done this before, but here's my stab at it. I didn't really understand why you had two arrays, and which ones were the actual files - so I've used the values in the files array as keys, and the title names as their values so that I can loop like shown - makes more sense to me.
    PHP Code:
    $searchString $_GET['q'];
    $files = array('index.php' => 'Homepage'
                   
    'cpcs.php'  => 'CPCS',
                   
    'riskassessments.php'  => 'Risk Assessments',
                   
    'enviromentalpolicy.php'   => 'Enviromental Policy',
                   
    'healthandsafety.php'   => 'Health And Safety');

    foreach (
    $files as $file => $title){
        
    // Get contents of the file, and put in a string
        
    $content file_get_contents($file);
        
    // Compare case-insensitively
        
    $found stristr($content,$searchString);
        if(
    $found !== FALSE){
            
    // String was found
            
    echo '<a href="'.$file.'">'.$title.'</a>';
        }

    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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