Noob code question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryank
Forum Newbie
Posts: 11
Joined: Sat Jul 19, 2008 2:56 pm

Noob code question

Post by ryank »

Code: Select all

    
    function searchSQL ($word, $method) {
        $sterm = tterm RLIKE '$word';
        $sdef = tdefinition RLIKE '$word';
        if (3 == $method) $word = "^$word$";
        if (1 == $method) $word = '^'.$word;
        return ('sterm','sdef' );
    }
   
The above code gives me the error:

Parse error: syntax error, unexpected T_STRING in /home/anned/public_html/components/com_glossary/c-classes/glossary_list_Controller.php on line 106

I assume it is only this part of the code int he file because this is the only code in a file (that previously worked) that I changed. I also know that these T errors are often because of stupid or ignorant syntax mistakes. Can anyone help me out?
ryank
Forum Newbie
Posts: 11
Joined: Sat Jul 19, 2008 2:56 pm

Re: Noob code question

Post by ryank »

Okay solved that problem leading to a new one, fixed the syntax with:

Code: Select all

    function searchSQL ($word, $method) {
        if (3 == $method) $word = "^$word$";
        if (1 == $method) $word = '^'.$word;
        $sterm = "tterm RLIKE '$word'";
        $sdef = "tdefinition RLIKE '$word'";
        return compact('sterm', 'sdef');
    }
So now my question is this.....

What I am trying to do is take a php component that has a search function and modify it. Originally this program would search only for words in the title of an item (tterm). But there is another column for definition (tdefinition). I'd like the program to return all results that have the search word ($word) in EITHER the title OR the defenition. This is what the code originally looked like:

Code: Select all

    
    function searchSQL ($word, $method) {
        if (3 == $method) $word = "^$word$";
        if (1 == $method) $word = '^'.$word;
        return ("tterm RLIKE '$word'");
    }
and this is the code for the entire file that this code is in... any help is VERY VERY appreciated.

Code: Select all

<?php
 
class glossary_list_Controller extends glossaryUserControllers {
    
    function glossary_list ($task) {
        $interface =& cmsapiInterface::getInstance();
        $database = $interface->getDB();
        $my = $interface->getUser();
        $gconfig = cmsapiConfiguration::getInstance();
 
        $id = $interface->getParam($_REQUEST, 'id', 0);
        if ($id) {
            $database->setQuery("SELECT * FROM #__glossary WHERE id = $id");
            $entries = $database->loadObjectList();
            if ($entries) {
                $glossid = $entries[0]->catid;
                $total = 1;
            }
        }
        if (empty($glossid)) $glossid = $interface->getParam($_REQUEST, 'glossid', 0);
        if (!$glossid) {
            $sql = "SELECT id FROM #__glossaries ORDER BY isdefault DESC, id";
            $database->setQuery($sql);
            $glossid = $database->loadResult();
        }
        $glossary = new glossaryGlossary($database);
        if ($glossid) $glossary->load($glossid);
        
        $glosshtml = '';
        if ($gconfig->showcategories) {
            $database->setQuery("SELECT * FROM #__glossaries WHERE id != $glossid");
            $glossaries = $database->loadObjectList();
            if ($glossaries) {
                require_once ($interface->getCfg('absolute_path').'/components/com_glossary/v-classes/glossaryGlossaryHTML.php');
                $glister = new glossaryGlossaryHTML();
                $glosshtml = $glister->view($glossaries);
            }
        }
 
        require_once ($interface->getCfg('absolute_path').'/components/com_glossary/v-classes/glossarySearchHTML.php');
        $searchword = $interface->getParam($_REQUEST, 'glossarysearchword');
        $searchword = $database->getEscaped($searchword);
        $searchmethod = $interface->getParam($_REQUEST, 'glossarysearchmethod', 1);
        $searching = new glossarySearchHTML;
        $searchhtml = $searching->view($glossary, $searchword, $searchmethod);
        
        if ($gconfig->show_alphabet) {
            require_once ($interface->getCfg('absolute_path').'/components/com_glossary/v-classes/glossaryAlphabetHTML.php');
            $database->setQuery("SELECT DISTINCT tletter FROM #__glossary ORDER BY tletter");
            $letters = $database->loadResultArray();
            $alphabet = new glossaryAlphabetHTML;
            $alphabethtml = $alphabet->view($letters, $letter);
        }
        else $alphabethtml = '';
        
        $letter = urldecode($interface->getParam($_REQUEST, 'letter', 'All'));
        $letter = $database->getEscaped($letter);
        
        if ($glossid) $where[] = "catid = $glossid";
        if ($letter AND 'All' != $letter) $where[] = "tletter = '$letter'";
        if ($searchword) $where[] = $this->searchSQL($searchword, $searchmethod);
 
        $sql = 'SELECT COUNT(*) FROM #__glossary';
        if ($gconfig->shownumberofentries) {
            $database->setQuery($sql." WHERE catid = $glossid");
            $grandtotal = $database->loadResult();
        }
        else $grandtotal = 0;
        
        if (empty($total)) {
            if (isset($where)) $sql .= ' WHERE '.implode(' AND ', $where);
            $database->setQuery($sql);
            $total = $database->loadResult();
        }
        
        if ($total) {
            if (empty($entries)) {
                $sql = "SELECT * FROM #__glossary";
                if (isset($where)) $sql .= ' WHERE '.implode(' AND ', $where);
                $page = $interface->getParam($_REQUEST, 'page', 1);
                require_once ($interface->getCfg('absolute_path').'/components/com_glossary/p-classes/cmsapiUserPage.php');
                $querystring = "&task=list&glossid=$glossid&letter=".urlencode($letter);
                if ($searchword) $querystring .= "&glossarysearchword=$searchword&glossarysearchmethod=$searchmethod";
                $pagecontrol =& new cmsapiUserPage ( $total, $my, $gconfig->perpage, $page, $querystring );
                $sql .= " ORDER BY tterm ASC LIMIT $pagecontrol->startItem, $pagecontrol->itemsperpage";
                $database->setQuery($sql);
                $entries = $database->loadObjectList();
            }
            else $pagecontrol = null;
            require_once ($interface->getCfg('absolute_path').'/components/com_glossary/v-classes/glossaryListHTML.php');
            $listing = new glossaryListHTML;
            $listhtml = $listing->view($entries, $letter, $grandtotal);
        }
        else {
            $listhtml = _GLOSSARY_IS_EMPTY;
            $pagecontrol = null;
        }
        
        $allowentry = ($gconfig->anonentry OR ($gconfig->allowentry AND $my->id));
        require_once ($interface->getCfg('absolute_path').'/components/com_glossary/v-classes/glossaryUserHTML.php');
        $lister = new glossaryUserHTML;
        $lister->view($glossary, $grandtotal, $allowentry, $glosshtml, $searchhtml, $alphabethtml, $listhtml, $pagecontrol);
    }
    
    function searchSQL ($word, $method) {
        if (3 == $method) $word = "^$word$";
        if (1 == $method) $word = '^'.$word;
        return ("tterm RLIKE '$word'");
    }
    
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Noob code question

Post by shiznatix »

Code: Select all

 
function searchSQL ($word, $method)
{
    if (3 == $method) $word = "^$word$";
    if (1 == $method) $word = '^'.$word;
    return ("(tterm RLIKE '$word' OR tdefinition LIKE '$word')");
}
 
That should do the trick.
ryank
Forum Newbie
Posts: 11
Joined: Sat Jul 19, 2008 2:56 pm

Re: Noob code question

Post by ryank »

OHOHO SWEET 8O 8O 8O 8) :mrgreen: . Had no idea you could do that. Thanks so much.

I really don't know much at all about php, I know only what I have learned by clunking around for about a 100 hours in prewritten scripts :banghead: . Which is a lot more than nothing and a lot less than being able to do much beyond "Hello World" in terms of complete scripts form scratch.

Note: I did have to change LIKE to RLIKE in that last line, dunno know what that changed but it made it work.

Thanks Again
Post Reply