易優(yōu)搜索全站內(nèi)容并從文章內(nèi)容里搜索關(guān)鍵字解決方法
2022-11-30 加入收藏
以下文章來(lái)自網(wǎng)友夜月游弋:
易優(yōu)搜索默認(rèn)的是從文章標(biāo)題里面搜索,因?yàn)閿?shù)據(jù)庫(kù)儲(chǔ)存的標(biāo)題內(nèi)容是純文本,所以不用過(guò)濾。但是有些時(shí)候需要全站搜索并從文章內(nèi)容中檢索關(guān)鍵詞,這時(shí)候默認(rèn)的搜索就顯得捉襟見(jiàn)肘了。其實(shí)我們靈活運(yùn)用sql語(yǔ)句直接查詢(xún)就可以實(shí)現(xiàn)!如下:
1 2 3 4 5 6 7 8 9 10 11 12 | {eyou:sql sql= "select aid from ey_article_content where content like '%$keywords%' union select aid from ey_archives where title like '%$keywords%' union select aid from ey_product_content where content like '%$keywords%' limit 0,5" } {eyou:sql sql= "select aid from ey_archives where aid='$aid'" id= "field3" } <li class= "dot" > {eyou:arcview aid= "$field3.aid" id= "field2" addfields= "content" } <a href= "{$field2.arcurl}" title= "{$field2.title}" ><h1>{$keywords, "<font color='red'>$keywords</font>" ,$field2.title|str_replace}</h1></a> {eyou:assign name = "neirong" value= "$field2.content|html_msubstr" } <div>{$neirong,$keywords|diy_content}</div> <span>{$field2.add_time|MyDate= 'Y-m-d' ,###}</span> </li> {/eyou:arcview} {/eyou:sql} {/eyou:sql} |
這里是利用聯(lián)合查詢(xún)從文章內(nèi)容和標(biāo)題中查詢(xún)包含關(guān)鍵字的文章aid,并去重,然后將aid套入arcview標(biāo)簽,循環(huán)輸出文章。
這樣即可輸出文章內(nèi)容或標(biāo)題包含關(guān)鍵詞的文章列表,全站搜索同理可得。
但是有一個(gè)問(wèn)題,易優(yōu)數(shù)據(jù)庫(kù)content字段儲(chǔ)存的是帶有html標(biāo)簽的內(nèi)容,如果搜索像“a”“p”“br”這樣的html標(biāo)簽,就會(huì)出現(xiàn)全部的文章。勉強(qiáng)能用。