php foreach循环生成结果-需要使用选择按钮确定用户选择

php foreach循环生成结果-需要使用选择按钮确定用户选择,php,select,foreach,multiple-results,Php,Select,Foreach,Multiple Results,我有一个搜索函数,通过GoogleBooksAPI搜索一本书,使用foreach循环以表格格式返回10个结果。几天来,我一直在试图弄清楚如何传递实际的搜索结果,这取决于用户选择了10个搜索结果中的哪一个。我找到了很多答案,但似乎没有一个符合我的具体要求。我想我需要为每个结果动态地创建一个变量,但也许我走错了方向?也许我需要动态地创建表id来选择数据表,但我不知道怎么做。还是动态更改输入选择行的id值 <strong><p style="font-size: 16px;

我有一个搜索函数,通过GoogleBooksAPI搜索一本书,使用foreach循环以表格格式返回10个结果。几天来,我一直在试图弄清楚如何传递实际的搜索结果,这取决于用户选择了10个搜索结果中的哪一个。我找到了很多答案,但似乎没有一个符合我的具体要求。我想我需要为每个结果动态地创建一个变量,但也许我走错了方向?也许我需要动态地创建表id来选择数据表,但我不知道怎么做。还是动态更改输入选择行的id值

     <strong><p style="font-size: 16px; text-align: center";>Top 10 Results for &quot;<?php echo @$_POST['q']; ?>&quot;</p></strong>    
    <strong><p style="font-size: 14px; text-align: center";>choose a book to select as your topic</p></strong>&nbsp;
    <table style="width:400px">
    <col width="325">
    <col width="75">
        <?php $i=0; foreach ($data['items'] as $item) { $i++;
    $b_title[$i] = $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]);?><br />
                     <strong>Published: </strong><?php printf( $item['volumeInfo']['publishedDate']); ?><br />                       
           <strong>Page(s): </strong><?php printf( $item['volumeInfo']['pageCount']); ?><br />
                     <strong>Publisher: </strong><?php printf( $item['volumeInfo']['publisher']); ?><br />
                     <strong>Category: </strong><?php printf( strtolower($item['volumeInfo']['printType']).', '.strtolower($item['volumeInfo']['categories'][0])); ?>&nbsp;&nbsp;
           <strong>ISBN: </strong><?php printf( $item['volumeInfo']['industryIdentifiers'][0]['identifier']); ?></td>
        <td><p><input type="submit" method="post" name="selectbook" value="Select" class="btn" id="book'.$i.'" /></p>
        <input type="hidden" name="value[]" value="<?=$i?>" />
        <img src="<?php printf( rawurldecode($item['volumeInfo']['imageLinks']['smallThumbnail'])); ?>" />
                </td>
        <tr><td style="width:420px"><p><strong>Description: </strong><?php printf( $item['volumeInfo']['description']); ?><br /></p></td>
        <?php
          //assigning values in order to pass to javascript variables - client to server
          if ( isset( $_POST['submit'] )) {
          $book_title = $b_title[$i];
          $book_cover = $book_cover_i;
          }
          $book_cover = $item['volumeInfo']['imageLinks']['smallThumbnail'];
          $book_author = $item['volumeInfo']['authors'][0];
          $book_published = $item['volumeInfo']['publishedDate'];
          $book_description = $item['volumeInfo']['description'];
          $book_pages = $item['volumeInfo']['pageCount'];
          $book_publisher = $item['volumeInfo']['publisher'];
          $book_ISBN = $item['volumeInfo']['industryIdentifiers'][0]['identifier'];          
         ?>

这基本上已经解决了。我可以增加值并输出我想要的精确值。下面我特别指定了2个,第二个结果肯定会进入php文件服务器端。请注意javascript、输入行以及如何将值分配给增量变量的更改

我的最后一个问题是将整数值传递给服务器,因此我不必手动输入2。我想我需要为那个做一条不同的线。也许这会帮助将来找到这条线索的人

  <script>
  $(function() {
  $( "#dialog" ).dialog({
    height: 550, width: 450});
    $( ".btn" ).click(function(){
    if(this.id.indexOf('select')>-1) { var id = (this.id.split("_"))[1]; console.log(id); }
    var bookSelect = id;
    //$("#select").val(bookSelect);
   $.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: { cover : cover, title : title, author : author, published : published, 
     description : description, pages : pages, publisher : publisher, ISBN : ISBN},
     bookSelect : bookSelect,
     success: function(data)
         {
         //identify the variables for unique handing on the server side, this is
         //how javascript variables (client side) are passed to the server
         $("input[name='bbp_extra_fieldc']").val(data.cover);
         $("input[name='bbp_topic_title']").val(data.title);
         $("input[name='bbp_extra_field1']").val(data.author);
         $("input[name='bbp_extra_field2']").val(data.published);
         $("input[name='bbp_extra_field3']").val(data.description);
         $("input[name='bbp_extra_field4']").val(data.pages);
         $("input[name='bbp_extra_field5']").val(data.publisher);
         $("input[name='bbp_extra_field6']").val(data.ISBN);
         $('#select').val(data.bookSelect);
         //alert(data);
         alert(bookSelect);
         },
         error: function(errorThrown){
         alert('error');
         }
         });
  $( "#dialog" ).dialog( "close" );  
  });
  });
  </script>  
        <strong><p style="font-size: 16px; text-align: center";>Top 10 Results for &quot;<?php echo @$_POST['q']; ?>&quot;</p></strong> 
    <strong><p style="font-size: 14px; text-align: center";>choose a book to select as your topic</p></strong>&nbsp;
    <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])); ?>&nbsp;&nbsp;
           <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='selectbook' value='Select' class='btn' 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>
        <?php
          //assigning values from index results according to user selection, then 
          //pass to javascript variables - client to server in below script
          //the if statement determines is someone selected a book
          if ( isset( $_POST['submit'] )) {
          $b = 2;
          $book_cover = $b_cover[$b];
          $book_title = $b_title[$b];
          $book_author = $b_author[$b];
          $book_published = $b_published[$b];
          $book_description = $b_description[$b];
          $book_pages = $b_pages[$b];
          $book_publisher = $b_publisher[$b];
          $book_ISBN = $b_ISBN[$b];
          }
         ?>
          <script>
          //assign values to each javascript variable with results on the server
          //this is how php variables are passed from server to the client - php to javascript
          var cover = <?php echo json_encode($book_cover); ?>;
          var title = <?php echo json_encode($book_title); ?>;
          var author = <?php echo json_encode($book_author); ?>;
          var published = <?php echo json_encode($book_published); ?>;
          var description = <?php echo json_encode($book_description); ?>;
          var pages = <?php echo json_encode($book_pages); ?>;
          var publisher = <?php echo json_encode($book_publisher); ?>;
          var ISBN = <?php echo json_encode($book_ISBN); ?>;
          //var book_count = <?php echo json_encode($count[$i]); ?>;
          </script>       
        </tr>
        </tr>