从以逗号分隔的输入字段获取数组 - Joomla 1.7

发布于 2024-12-27 12:50:43 字数 1456 浏览 1 评论 0原文

使用1.7,

现在我有这个:

<input type="text" name="searchword" class="keyword_search_input" size="32" maxlength="255" value="<? echo JRequest::getString('searchword'); ?>" placeholder="<? echo JText::_('Keyword(s)'); ?>"/><br />

但我想将由空格或逗号分隔的关键字放入一个数组中,该数组将传递给这个:

$this->keywords = JRequest::getVar('searchword');

if ($keywords != "") $where_clause[] = ' s.keywords LIKE "%'.$keywords.'%"';

function getSearchword(){

        return $this->keywords;

    }

任何帮助将不胜感激...

尼克

更新:

我已经弄清楚了。感谢@travega 为我指明了正确的方向。这是我的输入字段:

<input type="text" name="searchword" class="keyword_search_input" size="32"    maxlength="255" value="<? echo JRequest::getString('searchword'); ?>" placeholder="<? echo JText::_('Keyword(s)'); ?>"/><br />

这将一个字符串传递给我的controller.php中的构造函数:

$this->keywords = JRequest::getVar('searchword');

该字符串中由逗号和空格分隔的单词然后通过explode放置到一个数组中:

$this->keywords_array = explode(", ",$this->keywords);

这个用户输入单词的数组是然后与 MYSQL 关键字数据库进行比较:

if ($keywords != "") $where_clause[] = ' s.keywords LIKE "%'.$this->keywords_array[0].'%" OR s.keywords LIKE "%'.$this->keywords_array[1].'%"  ';

这个解决方案的唯一问题是我的搜索仅限于 keywords_array 中的两个值。我想找到一种方法将搜索中的值的数量设置为等于数组中的值的数量...

Using 1.7,

Right now I have this:

<input type="text" name="searchword" class="keyword_search_input" size="32" maxlength="255" value="<? echo JRequest::getString('searchword'); ?>" placeholder="<? echo JText::_('Keyword(s)'); ?>"/><br />

But I want to put the keywords separated by a space or comma into an array that will be passed to this:

$this->keywords = JRequest::getVar('searchword');

if ($keywords != "") $where_clause[] = ' s.keywords LIKE "%'.$keywords.'%"';

function getSearchword(){

        return $this->keywords;

    }

Any help would be appreciated...

Nick

UPDATE:

I've figured this out. Thanks to @travega for pointing me in the right direction. Here is my input field:

<input type="text" name="searchword" class="keyword_search_input" size="32"    maxlength="255" value="<? echo JRequest::getString('searchword'); ?>" placeholder="<? echo JText::_('Keyword(s)'); ?>"/><br />

This passes a string to a construct function in my controller.php:

$this->keywords = JRequest::getVar('searchword');

This words in this string that are seperated by a comma and a space are then placed into an array by explode:

$this->keywords_array = explode(", ",$this->keywords);

This array of user inputed words is then compared to a MYSQL database of keywords:

if ($keywords != "") $where_clause[] = ' s.keywords LIKE "%'.$this->keywords_array[0].'%" OR s.keywords LIKE "%'.$this->keywords_array[1].'%"  ';

The only problem I have with this solution is that my search is limited to two values in keywords_array. I'd like to find a way to set the number of values in the search to equal to the number of values in the array...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2025-01-03 12:50:43

我不确定您的“searchword”字符串将采用什么格式,但如果我理解正确,您希望在“searchword”字符串中构建多个关键字,然后传递回 getSearchword() 函数。

为此,您可以使用 javaScript 在添加新搜索词时将其附加到字符串中。每次添加搜索词时,还要添加一个逗号来分隔它们。当您发送表单时,您的字符串将像这样到达:

hello,world,foo,bar

因此您可以使用 php 中的explode() 函数将其转换为“搜索词”数组

$keywords_array = explode(",",$this->keywords);

Im not sure what format you "searchword" string will be in but if I understand correctly you wan to have multiple keywords to be built up in your "searchword" string and then passed back to you getSearchword() function.

For this you could use javaScript to append new search words to your string as they are added. Each time you add a sear term also add a comma to separate them. When you send your form your string will arrive like this:

hello,world,foo,bar

so you can use the explode() function in php to turn it into an array of "searchwords"

$keywords_array = explode(",",$this->keywords);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文