简体   繁体   English

在同一页面的div中加载PHP回显链接的内容

[英]Load content of PHP echoed link in div on same page

I have SELECT data from a table from my database.This data come with links. 我有数据库表中的SELECT数据,这些数据带有链接。 What i want is that when i click on the link, it loads more of that data in a div instead of going to aa different page. 我想要的是,当我单击链接时,它会将更多的数据加载到div中,而不是转到另一个页面。 Each links comes with an id from which to know data to load in the div from the database.Below is the code 每个链接都有一个id ,从中可以知道要从数据库加载到div中的数据。下面是代码

<div class="pricepop"></div>
<div class="pricetab">
<table class="table">
                <thead>
                    <tr>
                      <th>COMMODITIES</th>
                      <th>UNITS</th>
                      <th>INTERNATIONAL PRICE($)</th>
                      <th>TERMS</th>
                      <th>LOCAL PRICE (&#8358;)</th>
                      <th>AS AT</th>
                      <th>MARKET</th>
                      <th>Full Details</th>
                    </tr>
                </thead>
                <tbody>
                <style>
                .table > thead > tr > th, .table > tbody > tr > td {
                    font-size: 10px !important;
                    font-family: 'Lato', sans-serif;
                    font-weight: bold;
                }
                </style>
                  <?php
                  $sql = $db->prepare("SELECT * FROM commodityprices");
                  $result = $sql->execute();
                  while ($row = $result->fetchArray(SQLITE3_ASSOC))
                  {
                      $id = $row['id'];
                      $_SESSION['id'] = $id;
                      $name = $row['name'];
                      $unit = $row['unit'];
                      $iprice = $row['iprice'];
                      $lprice = $row['lprice'];
                      $irate = $row['irate'];
                      $lrate = $row['lrate'];
                      $terms = $row['cterms'];
                      $asat = date('d/M/Y');
                      $market = $row['market'];

                          echo '<tr>
                          <td>'.$name.'</td>
                          <td>'.$unit.'</td>';
                          if ($irate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$iprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$iprice.'</td>';
                          }
                          echo '<td>'.$terms.'</td>';
                          if ($lrate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
                          }
                          echo '<td>'.$asat.'</td>
                          <td>'.$market.'</td>
                          <td><a class="comprice" href="pricedetails.php?id='.$id.'">View more</a></td>
                          </tr>';
                  }                 
                  ?>
                </tbody>
                </table>
</div>

Below i have a javascript that loads the data from the database into the .pricepop on link click 下面我有一个javascript将数据从数据库加载到链接上的.pricepop上单击

$(document).ready(function() {
    $(".comprice").on("click", function (e) {
        e.preventDefault();
        $(".pricepop").load("pricedetails.php");
    });
});

Below is the pricedetails.php code that gets the remaining price details. 以下是pricedetails.php代码,该代码获取剩余的价格详细信息。

                  <thead>
                    <tr>
                      <th>COMMODITIES</th>
                      <th>TERMS</th>
                      <th>LOCAL PRICE (&#8358;)</th>
                      <th>AS AT</th>
                      <th>MARKET</th>
                      <th>Price/bag</th>
                    </tr>
                </thead>
                <tbody>
                <style>
                .table > thead > tr > th, .table > tbody > tr > td {
                    font-size: 10px !important;
                    font-family: 'Lato', sans-serif;
                    font-weight: bold;
                }
                </style>
                  <?php
                  $priceId = $_SESSION['id'];
                  $sql = $db->prepare("SELECT * FROM commodityprices WHERE id = ?");
                  $sql->bindParam(1, $priceId, SQLITE3_INTEGER);
                  $result = $sql->execute();
                  while ($row = $result->fetchArray(SQLITE3_ASSOC))
                  {
                      $id = $row['id'];
                      $name = $row['name'];
                      $iprice = $row['iprice'];
                      $lprice = $row['lprice'];
                      $lrate = $row['lrate'];
                      $terms = $row['cterms'];
                      $asat = date('d/M/Y');
                      $market = $row['market'];
                      $priceperbags = $row['priceperbags'];

                          echo '<tr>
                          <td>'.$name.'</td>';
                          echo '<td>'.$terms.'</td>';
                          if ($lrate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
                          }
                          echo '<td>'.$asat.'</td>
                          <td>'.$market.'</td>
                          <td class="comprice">'.$priceperbags.'</td>
                          </tr>';
                  }
                  ?>
                </tbody>
                </table> 
</div>

The issue now is that no matter the link i click on from the echoed php data, the data displayed in the .pricepop is the details last echo link. 现在的问题是,无论我从回显的php数据中单击链接, .pricepop显示的数据都是最后一个echo链接的详细信息。

How can i fix this such that when i click on a link, details gotten from id of that link is echo and displayed in the .pricepop . 我该如何解决这个问题,以便当我单击链接时,从该链接的id获得的详细信息会echo并显示在.pricepop

You must use AJAX POST or GET statemant to get what you need. 您必须使用AJAX POST或GET statemant来获取所需的内容。 Simple jQuery .load() can't help you. 简单的jQuery .load()无法帮助您。

In basics, you must do some kind of pagination to load or append new data. 从根本上讲,您必须执行某种分页以加载或附加新数据。

Here is examples: 这是示例:

http://itsolutionstuff.com/post/php-infinite-scroll-pagination-using-jquery-ajax-exampleexample.html http://phppot.com/php/ajax-pagination-with-php/ http://itsolutionstuff.com/post/php-infinite-scroll-pagination-using-jquery-ajax-exampleexample.html http://phppot.com/php/ajax-pagination-with-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM