我有一个CSV文件,2列名称;图像,
//--&燃气轮机;这是应该发生的事情<--//
当用户执行搜索时,他们在文本字段中键入一些内容,自动完成下拉列表显示所有与查询匹配的可用名称,他们单击搜索,加载图标显示几秒钟,同时代码检查匹配情况,然后使用jQuery将结果显示给用户(将在主搜索框下使用向下滑动操作来显示结果)
我有一个问题,从搜索中获取信息,以匹配我的数组值,并带回名称值的相关图像。。我所需要的是搜索字段值带回与该名称关联的相关图像。。。并在结果区向用户显示图片和标题。。。
<?php
$maxlinelength = 1000;
$fh = fopen('tartans.csv', 'r');
$firstline = fgetcsv($fh, $maxlinelength);
$cols = count($firstline);
$row = 0;
$inventory = array();
while ( ($nextline = fgetcsv($fh, $maxlinelength)) !== FALSE )
{
for ( $i = 0; $i < $cols; ++$i )
{
$inventory[$firstline[$i]][$row] = $nextline[$i];
}
++$row;
}
fclose($fh);
?>
这将从csv生成名称列表,用逗号分隔,然后使用javascript split()函数加载auto complete to函数的所有名称。。
<script type="text/javascript">
$(document).ready(function(){
var data = '<?php foreach( $inventory['NAME'] as $key => $value){
echo $value.",";
}?>'.split(",");
$("#s").autocomplete(data);
})
</script>
//jquery来处理表单提交并返回结果
<script type="text/javascript">
$(document).ready(function() {
$('#loader').hide();
$('#tartanResults').hide();
$('#submit').click(function() {
$.ajax({
url: '/select_tartan.php', // current page
beforeSend: function() {
$('#loader').show();
},
complete: function() {
$('#loader').hide();
$('#tartanResults').show();
}
});
return false;
});// submit
});// doc ready
</srcipt>
这就是我遇到的问题,试图返回数组中加载的特定名称的匹配键值,并将其与其图像匹配,
使用print_r($inventory);//虽然它大了很多,
Array
(
[NAME] => Array
(
[0] => Abercrombie Ancient
[1] => Abercrombie Modern
[2] => Aberdeen Tartan Modern
[3] => Agnew Ancient
[4] => Allison Ancient
[5] => Anderson Ancient
[6] => Anderson Modern
[7] => Anderson Weathered
[8] => Angus Ancient
[9] => Angus Modern
)
[IMAGE] => Array
(
[0] => images/rare/abc_ar.jpg
[1] => images/rare/abc_mr.jpg
[2] => images/rare/abd_mr.jpg
[3] => images/rare/agw_ar.jpg
[4] => images/rare/all_ar.jpg
[5] => images/rare/and_ar.jpg
[6] => images/rare/and_mr.jpg
[7] => images/rare/and_wr.jpg
[8] => images/rare/ang_ar.jpg
[9] => images/rare/ang_mr.jpg
)
)
我只需要显示返回的名称和图像,就像
<?php
echo $inventory['NAME'][8];
echo $inventory['IMAGE'][8];
?>
事实证明,这比我最初想象的要困难。。
任何帮忙的人都很感激。。
马蒂
<!-- SEARCH FORM / LOADER-->
<div id="searchbox">
<h3>FIND YOUR TARTAN <small>press enter to search.</small></h3>
<form id="tartanSearch" action="select_tartan.php" method="get">
<div class="search">
<input type="text" size="70" class="inputbox" alt="Search" id="s" name="search" />
<input value="Search" id="submit" type="submit" />
</div>
</form>
</div>
<div id="loader"><img src="images/indicator.gif" width="16" height="16" /></div>
<!-- RESULTS -->
<div id="tartanResults">
<h3><!-- WERE THE NAME LOADS --></h3>
<div class="tartan_img"><!-- WERE THE IMAGE LOADS --></div>
<div class="tartan_text">
<ul style="list-style:none;">
<li>Description text here</li>
<li>With more text here aswell</li>
<li>We can have some here also</li>
</ul>
</div>
</div>