Getting words from search page's url into the title tag

Discussion in 'PHP' started by chops, Apr 23, 2008.

  1. #1
    I have a site which is made from php, based on open Realty.
    I wish to get keywords from the search results page's URL and inject them into the page Title and heading H1.
    eg
    The page is
    mysite.com/index.php?searchtext=sea+view&action=searchresults
    (The URL is rewriteruled to ....com/sea_view.html)

    The Title of the page must become 'sea view'.
    Can anyone help?
     
    chops, Apr 23, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    mysite.com/index.php Source:

    
    <?
    //Other code//
    
    if(!empty($_GET['searchtext'])){
    	$title = trim(htmlspecialchars($_GET['searchtext']));
    }
    
    //Other code//
    ?>
    //---
    <head>
    <title><? echo "Search results for: " . $title; ?></title>
    </head>
    //---
    
    PHP:
    Remember to include doctype and other meta data.
     
    ToddMicheau, Apr 23, 2008 IP
  3. chops

    chops Well-Known Member

    Messages:
    199
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks for that. i'm not much of a programmer. Will this work when the dynamic search result page is produced simply by another site page linking to it? eg with a link from the same site: mysite.com/sea-view.html which is then changed by the rewrite-rule?
     
    chops, Apr 23, 2008 IP
  4. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Whoa, you lost me there lol. But I think to answer your question; no- the code I gave you is based off GET values, meaning the ?searchtext= part. If you wanted their search text to pass onto other pages I would suggest setting a cookie that contains the text, then if the cookie exists set the title to that.
     
    ToddMicheau, Apr 23, 2008 IP