我能指数JavaScript数组来匹配一组PHP变量的结果 [英] Can I index javascript array to match a set of results of php variables
问题描述
我送到一个jquery窗口搜索结果 - 10要准确。如果用户选择第4的结果,我想分配给一个javascript变量,具体的结果。朝下方code的底部,是我想我需要指数的JavaScript变量,不知何故?然后在这个code的顶部,我想我可以分配多少选择的用户。我已经想通了如何让用户选择号码。此外,在低于code的底部,你会看到我在索引注释掉了我尝试的JavaScript变量匹配相同的PHP变量。
<脚本>
$(函数(){
$(#dialog).dialog({身高:550,宽度:450});
$(.submit)。点击(函数(){
//if(this.id.indexOf('select')>-1){风险的id =(this.id.split())[1];的console.log(ID);}
// bookSelect = ID;
$阿贾克斯({
键入:POST,
网址:'书meta.php',
异步:真实,
数据类型:JSON,
//赋值给变量传递给经由数据服务器
数据:{B:B,封面:封面,标题:标题,作者:作者,出版:出版,ISBN:ISBN,
说明:说明,网页:页,出版商:出版商},
成功:功能(数据)
{
//确定在服务器侧独特递变量,这是
//如何图书数据获取,为BB preSS论坛输入字段
$(输入[名称='bookCover'])VAL(data.cover)。
$(输入[名称='bbp_topic_title'])VAL(data.title)。
$(输入[名称='BOOKAUTHOR'])VAL(data.author)。
$(输入[名称='bookPublished'])VAL(data.published)。
$(输入[名称='bookDescription'])VAL(data.description)。
$(输入[名称='bookPages'])VAL(data.pages)。
$(输入[名称='bookPublisher'])VAL(data.publisher)。
$(输入[名称='bookISBN'])VAL(data.ISBN)。
//$(\"input[name='selection']\").val(data.bookSelect);
//警报(B);
},
错误:功能(errorThrown){
警报('错误');
},
});
$(#dialog).dialog(亲密);
}); });
< / SCRIPT>
<强>< p风格=FONT-SIZE:16px的;文本对齐:中心;?>前10个结果和放大器; QUOT;< PHP的回声@ $ _ POST ['Q']; ?>&安培; QUOT;< / P>< / STRONG>
<强>< p风格=FONT-SIZE:14px的;文本对齐:中心;>选一本书选作专题< / P>< / STRONG>&安培; NBSP;
<表样式=宽度:400像素>
<西WIDTH =325>
<西WIDTH =75>
< PHP $ I = 0;的foreach($数据['项目']为$项目){$ I ++; ?>
&所述; TR>
&所述; TD>
<强>< U>< DIV的风格=FONT-SIZE:14px的;?>< PHP的printf($项目['volumeInfo'] ['标题']);
$ b_title [$ i] = $项目['volumeInfo'] ['标题']>< / U>< / DIV>< / STRONG>
<强>作者:< / STRONG>< PHP的printf($项目['volumeInfo'] ['作者'] [0]);
$ b_author [$ i] = $项目['volumeInfo'] ['作者'] [0]>< BR />
<强>发布时间:< / STRONG>< PHP的printf($项目['volumeInfo'] ['publishedDate']);
$ b_published [$ i] = $项目['volumeInfo'] ['publishedDate']>< BR />
<强>页面(S):< / STRONG>< PHP的printf($项目['volumeInfo'] ['页页次']);
$ b_pages [$ i] = $项目['volumeInfo'] ['页页次']>< BR />
<强>出版商:?< / STRONG>< PHP的printf($项目['volumeInfo'] ['发行人']);
$ b_publisher [$ i] = $项目['volumeInfo'] ['发行人']>< BR />
<强>类别:< / STRONG>< PHP的printf(用strtolower($项目['volumeInfo'] ['平面广告类型']),.strtolower($项目['volumeInfo'] ['类' ] [0])); ?>&安培; NBSP;&安培; NBSP;
<强> ISBN:< / STRONG>< PHP的printf($项目['volumeInfo'] ['industryIdentifiers'] [0] ['标识']);
$ b_ISBN [$ i] = $项目['volumeInfo'] ['industryIdentifiers'] [0] ['标识']>< / TD>
< TD>< P>< PHP的回声<输入类型=提交方法='后'的名字='提交'值=选择类='提交'ID ='选择$ I'/ >中; ?>&下; / P>
< IMG SRC =< PHP的printf(rawurlde code($项目['volumeInfo'] ['imageLinks'] ['smallThumbnail']));
$ b_cover [$ i] = $项目['volumeInfo'] ['imageLinks'] ['smallThumbnail']>/>
< / TD>
< TR>< TD风格=WIDTH:420px>< P><强>简介:< / STRONG>< PHP的printf($项目['volumeInfo'] ['说明']) ;
$ b_description [$ i] = $项目['volumeInfo'] ['说明']>< BR />< / P>< / TD>
< / TR>
< / TR>
<脚本>
$(.submit)。点击(函数(){
如果(this.id.indexOf(选择)-1)〜{VAR ID =(this.id.split())[1];的console.log(ID);}
B = ID; });
//为(VAR I = 0,LEN = 11; I< LEN,我++)
// VAR标题= [];
VAR盖=<?PHP的回声json_en code($ b_cover [3]); ?取代;
// VAR标题[i] =<?PHP的回声json_en code($ b_title [$ i]); ?取代;
VAR标题=<?PHP的回声json_en code($ b_title [3]); ?取代;
VAR作者= LT;?PHP的回声json_en code($ b_author [3]); ?取代;
VAR公布=<?PHP的回声json_en code($ b_published [3]); ?取代;
Var描述=<?PHP的回声json_en code($ b_description [3]); ?取代;
VAR页=<?PHP的回声json_en code($ b_pages [3]); ?取代;
VAR出版商= LT;?PHP的回声json_en code($ b_publisher [3]); ?取代;
VAR ISBN =<?PHP的回声json_en code($ b_ISBN [3]); ?取代;
< / SCRIPT>
在情况下,有人搜索了这一点,我找到了答案,以我自己的问题。两个警告:1)这个答案看起来杂乱无章我。有可能是一个更简单的方法。 2)@elclanrs在他的评论中正确的;我打算清理这code成单独的文件。如果你看一下上面的问题,我已经修改了底层脚本,并在顶部脚本修改的变量。这里是底部的脚本(我用一个javascript的foreach阵列,并放在所有可能的结果数组中的 - 有可能是一个办法做到这一点,而不列出他们全部?)。为简便起见,我只包括在第一两个可变阵列
<脚本>
$(.submit)。点击(函数(){
//获取按钮的ID号,该索引为每个结果,用户选择
//变量B从ID递减1,因为JavaScript数组开头[0]
如果(this.id.indexOf(选择)-1)〜{VAR ID =(this.id.split())[1];的console.log(ID);}
B = ID - 1; });
//创建所有10个结果的数组。有可能是一个更清洁的方式来做到这一点
VAR b_cover = [<?PHP的回声json_en code($ b_cover [1]); ?>,<?PHP的回声json_en code($ b_cover [2]); ?>,<?PHP的回声json_en code($ b_cover [3]); ?>,<?PHP的回声json_en code($ b_cover [4]); ?>中
<?PHP的回声json_en code($ b_cover [5]); ?>,<?PHP的回声json_en code($ b_cover [6]); ?>,<?PHP的回声json_en code($ b_cover [7]); ?>,<?PHP的回声json_en code($ b_cover [8]); ?>中
<?PHP的回声json_en code($ b_cover [9]); ?>,<?PHP的回声json_en code($ b_cover [10]); ?>]
//这是JavaScript的foreach
b_cover.forEach(功能(输入){执行console.log(输入);}); VAR b_title = [<?PHP的回声json_en code($ b_title [1]); ?>,<?PHP的回声json_en code($ b_title [2]); ?>,<?PHP的回声json_en code($ b_title [3]); ?>中
<?PHP的回声json_en code($ b_title [4]); ?>,<?PHP的回声json_en code($ b_title [5]); ?>,<?PHP的回声json_en code($ b_title [6]); ?>中
<?PHP的回声json_en code($ b_title [7]); ?>,<?PHP的回声json_en code($ b_title [8]); ?>,<?PHP的回声json_en code($ b_title [9]); ?>,<?PHP的回声json_en code($ b_title [10]); ?>]
b_title.forEach(功能(输入){执行console.log(输入);});
和这里的变量是如何在顶级脚本分配的:
$阿贾克斯({
键入:POST,
网址:'书meta.php',
异步:真实,
数据类型:JSON,
//将值分配给要传递到经由数据服务器中的变量,根据其B位置
//在foreach数组中。
数据:{B:B,盖:b_cover [B]标题:b_title [B],作者:b_author [B]发表:b_published [B]
ISBN:b_ISBN [B]的说明:b_description [B]的网页:b_pages [B],出版商:b_publisher [b]},
成功:功能(数据)
I have search results sent to a jquery window - 10 to be exact. If a user selects the 4th result, I would like that specific result assigned to a javascript variable. Toward the bottom of the below code is where I think I need to index the javascript variables somehow? Then at the top of this code I am thinking I can assign the number the user selected. I have already figured out how to get the number the user selects. Also at the bottom of the below code you will see where I commented out my attempt at indexing javascript variables to match the same php variables.
<script>
$(function() {
$( "#dialog" ).dialog({height: 550, width: 450});
$( ".submit" ).click(function(){
//if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
//bookSelect = id;
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
//assign values to the variables to be passed to the server via data
data: { B : B, cover : cover, title : title, author : author, published : published, ISBN : ISBN,
description : description, pages : pages, publisher : publisher},
success: function(data)
{
//identify the variables for unique handing on the server side, this is
//how the book data gets to the input fields for bbpress forums
$("input[name='bookCover']").val(data.cover);
$("input[name='bbp_topic_title']").val(data.title);
$("input[name='bookAuthor']").val(data.author);
$("input[name='bookPublished']").val(data.published);
$("input[name='bookDescription']").val(data.description);
$("input[name='bookPages']").val(data.pages);
$("input[name='bookPublisher']").val(data.publisher);
$("input[name='bookISBN']").val(data.ISBN);
//$("input[name='selection']").val(data.bookSelect);
//alert(B);
},
error: function(errorThrown){
alert('error');
},
});
$( "#dialog" ).dialog( "close" );
}); });
</script>
<strong><p style="font-size: 16px; text-align: center";>Top 10 Results for "<?php echo @$_POST['q']; ?>"</p></strong>
<strong><p style="font-size: 14px; text-align: center";>choose a book to select as your topic</p></strong>
<table style="width:400px">
<col width="325">
<col width="75">
<?php $i=0; foreach ($data['items'] as $item) { $i++; ?>
<tr>
<td>
<strong><u><div style="font-size: 14px";><?php printf($item['volumeInfo']['title']);
$b_title[$i] = $item['volumeInfo']['title']?></u></div></strong>
<strong>Author: </strong><?php printf( $item['volumeInfo']['authors'][0]);
$b_author[$i] = $item['volumeInfo']['authors'][0]?><br />
<strong>Published: </strong><?php printf( $item['volumeInfo']['publishedDate']);
$b_published[$i] = $item['volumeInfo']['publishedDate'] ?><br />
<strong>Page(s): </strong><?php printf( $item['volumeInfo']['pageCount']);
$b_pages[$i] = $item['volumeInfo']['pageCount'] ?><br />
<strong>Publisher: </strong><?php printf( $item['volumeInfo']['publisher']);
$b_publisher[$i] = $item['volumeInfo']['publisher'] ?><br />
<strong>Category: </strong><?php printf( strtolower($item['volumeInfo']['printType']).', '.strtolower($item['volumeInfo']['categories'][0])); ?>
<strong>ISBN: </strong><?php printf( $item['volumeInfo']['industryIdentifiers'][0]['identifier']);
$b_ISBN[$i] = $item['volumeInfo']['industryIdentifiers'][0]['identifier'] ?></td>
<td><p><?php echo "<input type='submit' method='post' name='submit' value='Select' class='submit' id='select $i' />"; ?></p>
<img src="<?php printf( rawurldecode($item['volumeInfo']['imageLinks']['smallThumbnail']));
$b_cover[$i] = $item['volumeInfo']['imageLinks']['smallThumbnail'] ?>" />
</td>
<tr><td style="width:420px"><p><strong>Description: </strong><?php printf( $item['volumeInfo']['description']);
$b_description[$i] = $item['volumeInfo']['description'] ?><br /></p></td>
</tr>
</tr>
<script>
$( ".submit" ).click(function(){
if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
B = id; });
//for(var i = 0, len = 11; i < len; i++)
//var title = [];
var cover = <?php echo json_encode($b_cover[3]); ?>;
//var title[i] = <?php echo json_encode($b_title[$i]); ?>;
var title = <?php echo json_encode($b_title[3]); ?>;
var author = <?php echo json_encode($b_author[3]); ?>;
var published = <?php echo json_encode($b_published[3]); ?>;
var description = <?php echo json_encode($b_description[3]); ?>;
var pages = <?php echo json_encode($b_pages[3]); ?>;
var publisher = <?php echo json_encode($b_publisher[3]); ?>;
var ISBN = <?php echo json_encode($b_ISBN[3]); ?>;
</script>
In case someone searches this out, I found an answer to my own question. Two warnings: 1) this answer looks messy to me. There is probably an easier way. 2) @elclanrs is correct in his comment; I plan to clean this code up into separate files. If you look at the above question, I have modified the bottom script, and modified the variables in the top script. Here is the bottom script (I used a javascript foreach array, and placed all possible results in the array - there is probably a way to do that without listing them all?). For brevity, I only included the first two variable arrays.
<script>
$( ".submit" ).click(function(){
//get the id number, which is indexed for each result, of the button the user selects
//the variable B is decremented 1 from id because javascript arrays begin with [0]
if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
B = id - 1; });
//create an array for all 10 results. There is probably a cleaner way to do this
var b_cover = [<?php echo json_encode($b_cover[1]); ?>,<?php echo json_encode($b_cover[2]); ?>,<?php echo json_encode($b_cover[3]); ?>,<?php echo json_encode($b_cover[4]); ?>,
<?php echo json_encode($b_cover[5]); ?>,<?php echo json_encode($b_cover[6]); ?>,<?php echo json_encode($b_cover[7]); ?>,<?php echo json_encode($b_cover[8]); ?>,
<?php echo json_encode($b_cover[9]); ?>,<?php echo json_encode($b_cover[10]); ?>];
//this is the javascript foreach
b_cover.forEach(function(entry){console.log(entry);});
var b_title = [<?php echo json_encode($b_title[1]); ?>,<?php echo json_encode($b_title[2]); ?>,<?php echo json_encode($b_title[3]); ?>,
<?php echo json_encode($b_title[4]); ?>,<?php echo json_encode($b_title[5]); ?>,<?php echo json_encode($b_title[6]); ?>,
<?php echo json_encode($b_title[7]); ?>,<?php echo json_encode($b_title[8]); ?>,<?php echo json_encode($b_title[9]); ?>,<?php echo json_encode($b_title[10]); ?>];
b_title.forEach(function(entry){console.log(entry);});
And here is how the variables are assigned in the top script:
$.ajax({
type: "POST",
url: 'book-meta.php',
async:true,
dataType: 'json',
//assign values to the variables to be passed to the server via data, according to their B position
//within the foreach array.
data: { B : B, cover : b_cover[B], title : b_title[B], author : b_author[B], published : b_published[B],
ISBN : b_ISBN[B], description : b_description[B], pages : b_pages[B], publisher : b_publisher[B]},
success: function(data)
这篇关于我能指数JavaScript数组来匹配一组PHP变量的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!