根据CakePHP中字段数据的第一个字母获取结果

时间:2022-10-23 08:35:53

I'm trying to create a page where users can search through all the records of a particular model, in my case albums. In the view I use:

我正在尝试创建一个页面,用户可以在我的案例相册中搜索特定模型的所有记录。在视图中我使用:

    for ($i = 65; $i < 90; $i++) { 
        echo $html->link(chr($i), array('action' => 'letter_find', chr($i))) , ' - '; 
    }

to print out each letter of the alphabet followed by a '-' (can anyone tell me how not to have the '-' after the last letter?). The user clicks on a letter and is passed to the letter_find action with the appropriate letter passed as a variable.

打印出字母表中的每个字母后跟一个' - '(有人能告诉我如何在最后一个字母后面没有' - ')。用户单击一个字母并传递给letter_find操作,并将相应的字母作为变量传递。

Here's where I get stuck. I'm not completely sure how I'm meant to find all the albums beginning with the selected letter. Like I said in the title, I'm a total neub. This is probably something very simple to do?

这是我被卡住的地方。我不完全确定我是如何找到以所选字母开头的所有专辑。就像我在标题中所说的那样,我是一个完整的新星。这可能是非常简单的事情吗?

thanks in advance :)

提前致谢 :)

3 个解决方案

#1


0  

I think this should do it, and it will return the $results as an array:

我认为应该这样做,它会将$ results作为数组返回:

$results = $this->model->find('all',array('conditions'=>array('model.field LIKE'=>$letter.'%')));

#2


2  

foreach ( range( 'a', 'z' ) as $l ) {
    $links[] = $html->link($l, array('action' => 'letter_find', $l));
}

echo implode( ' - ', $links );

#3


1  

'Model.field LIKE ' . $letter . '%' is the condition you put in your find

'Model.field LIKE'。 $ letter。 '%'是您在查找中添加的条件

EDIT

you might also like this

你可能也喜欢这个

find available letters - https://github.com/infinitas/infinitas/blob/beta/core/libs/models/behaviors/infinitas.php#L525

找到可用的信件 - https://github.com/infinitas/infinitas/blob/beta/core/libs/models/behaviors/infinitas.php#L525

show a list of available letters - https://github.com/infinitas/infinitas/blob/beta/core/filter/views/helpers/filter.php#L140

显示可用字母列表 - https://github.com/infinitas/infinitas/blob/beta/core/filter/views/helpers/filter.php#L140

#1


0  

I think this should do it, and it will return the $results as an array:

我认为应该这样做,它会将$ results作为数组返回:

$results = $this->model->find('all',array('conditions'=>array('model.field LIKE'=>$letter.'%')));

#2


2  

foreach ( range( 'a', 'z' ) as $l ) {
    $links[] = $html->link($l, array('action' => 'letter_find', $l));
}

echo implode( ' - ', $links );

#3


1  

'Model.field LIKE ' . $letter . '%' is the condition you put in your find

'Model.field LIKE'。 $ letter。 '%'是您在查找中添加的条件

EDIT

you might also like this

你可能也喜欢这个

find available letters - https://github.com/infinitas/infinitas/blob/beta/core/libs/models/behaviors/infinitas.php#L525

找到可用的信件 - https://github.com/infinitas/infinitas/blob/beta/core/libs/models/behaviors/infinitas.php#L525

show a list of available letters - https://github.com/infinitas/infinitas/blob/beta/core/filter/views/helpers/filter.php#L140

显示可用字母列表 - https://github.com/infinitas/infinitas/blob/beta/core/filter/views/helpers/filter.php#L140