从php搜索表单中提取$关键字

时间:2022-08-22 11:29:14

Here is my search function :

这是我的搜索功能:

class Ads extends Controller
{
    public function search($format='velos', $cat='occasion', $location='france', $keyword, $page=1)
    {
        if($format == 'velos')
        {
            $query = '`private`=0';
        } else
        {
            $query = '`private`=1';
        }
    $this->images($query, $cat, $location,'MATCH(title) AGAINST ("'.db::escape($keyword).'" IN BOOLEAN MODE)', $page, '%s/%s/%s/'.$keyword.'/%d/', $row[0], $keyword);
    }
}

It's working fine, when I change the keyword in the URL, everything is okay But I would like to echo that keyword in the page, I don't know how ? I tried

它运行得很好,当我改变URL中的关键字时,一切都没问题但是我想在页面中重复这个关键字,我不知道怎么做?我试着

<?php echo $keyword; ?>

But the echo doesn't return anything. There might be a scope issue, but I'm not sure.

但是echo不会返回任何东西。可能会有范围问题,但我不确定。

EDIT Actually the $keyword comes from a search field, but from another class, that's maybe why I was thinking of a scoping issue

编辑实际上$关键字来自一个搜索字段,但是来自另一个类,这可能是我考虑一个范围问题的原因。

class UserModel extends Model
{
    public function search_form()
    {
    $form = new Form('search_form');

    $form->field('keyword', 'text', array
    (           
        'optional'      =>  true
    )); 
    if($data = $form->validate())
    {
        header('Location: '.WEB.sprintf($data['parts_search'].'/'.$data['category_search'].'/'.$data['location_search'].'/'.$data['keyword'].'/1/'));
    }
    return $form;
    }   
}   

Solution :

解决方案:

It might not be the best solution, but actually the only one I found :

这可能不是最好的解决办法,但实际上是我发现的唯一的办法:

$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$path = $parts['path'];
$keys = explode('/', $path);
echo $keys[4]; // `corresponding to the 4th url word`

1 个解决方案

#1


0  

I'm having some trouble understanding exaclty what the problem is, but based on your proposed solution, there is an auto-loaded CI class that can do what you've proposed.

我不太理解问题是什么,但是根据您提出的解决方案,有一个自动加载的CI类可以完成您提出的任务。

http://codeigniter.com/user_guide/libraries/uri.html

http://codeigniter.com/user_guide/libraries/uri.html

So, for http://yoursite.com/format/cat/location/yourKeyword

因此,对于http://yoursite.com/format/cat/location/yourKeyword

$keyword = $this->uri->segment(4); // get the 4th segment of my CI URI
echo $keyword; // prints yourKeyword

Hope this helps.

希望这个有帮助。

#1


0  

I'm having some trouble understanding exaclty what the problem is, but based on your proposed solution, there is an auto-loaded CI class that can do what you've proposed.

我不太理解问题是什么,但是根据您提出的解决方案,有一个自动加载的CI类可以完成您提出的任务。

http://codeigniter.com/user_guide/libraries/uri.html

http://codeigniter.com/user_guide/libraries/uri.html

So, for http://yoursite.com/format/cat/location/yourKeyword

因此,对于http://yoursite.com/format/cat/location/yourKeyword

$keyword = $this->uri->segment(4); // get the 4th segment of my CI URI
echo $keyword; // prints yourKeyword

Hope this helps.

希望这个有帮助。