【问题标题】:Load more button value according "WHERE" Clouse condition根据“WHERE”子句条件加载更多按钮值
【发布时间】:2019-11-21 13:29:36
【问题描述】:

我正在使用加载更多按钮从同一网页上特定 ID 的数据库中加载内容。代码如下:

<div class="postList col-lg-12">
     <legend><h1 style="color:#298208;">Savings Bucks Details</h1> </legend>
<?php
    $busi_id = mysqli_real_escape_string($conn, $_SESSION['busi_id']);

    if (isset($busi_id)) {
        $query = "SELECT * FROM savingsbucks_business WHERE busi_id='$busi_id' ORDER BY sbb_id DESC LIMIT 2";
        $result = mysqli_query($conn, $query) or die('Query failed: ' . mysqli_error($conn));
        $numrows_savingsbucks = mysqli_num_rows($result);

    if ($numrows_savingsbucks == '0') {
        echo "<p style='text-align:center; color:#ff4400; margin-top:40px;'>No Data available!</p>";
    } else {
 ?>

<div class="table-responsive">
    <table border="1" class="table table-bordered">
        <thead>
        <th class="consumer_point_text">Shop ID</th>
        <th class="consumer_point_text">SenderID</th>
        <th class="consumer_point_text">Busi ID</th>
        <th class="consumer_point_text">Customer Type</th>
        <th class="consumer_point_text">Customer Name</th>
        <th class="consumer_point_text">Customer Email</th>
        <th class="consumer_point_text">Customer Phone</th>
        <th class="consumer_point_text">Two</th>
        <th class="consumer_point_text">Five</th>
        <th class="consumer_point_text">Ten</th>
        <th class="consumer_point_text">Twenty</th>
        <th class="consumer_point_text">Fifty</th>
        <th class="consumer_point_text">Hundred</th>
        <th class="consumer_point_text">Five Hundred</th>
        <th class="consumer_point_text">Total Savings Bucks</th>

    </thead>
<?php if($numrows_savingsbucks > 0){
    while ($row = mysqli_fetch_array($result)) {
        $sbb_id = $row['sbb_id'];
        $sender_id = $row['sender_id'];
        $busi_id = $row['busi_id'];
        $type = $row['type'];
        $consu_name = $row['consu_name'];
        $consu_email = $row['consu_email'];
        $consu_phone = $row['consu_phone'];
        $two = $row['two'];
        $five = $row['five'];
        $ten = $row['ten'];
        $twenty = $row['twenty'];
        $fifty = $row['fifty'];
        $hundred = $row['hundred'];
        $five_hundred = $row['five_hundred'];
        $total_two += $two;
        $total_five += $five;
        $total_ten += $ten;
        $total_twenty += $twenty;
        $total_fifty += $fifty;
        $total_hundred += $hundred;
        $total_five_hundred += $five_hundred;
        $total_bucks = $two+$five+$ten+$twenty+$fifty+$hundred+$five_hundred;
        $grand_total += $total_bucks;
?>

    <tr>
        <td class="consumer_point_text"><?=$sbb_id?></td>
        <td class="consumer_point_text"><?=$sender_id?></td>
        <td class="consumer_point_text"><?=$busi_id?></td>
        <td class="consumer_point_text" style="text-transform:capitalize;"><?=$type?></td>
        <td class="consumer_point_text"><?=$consu_name?></td>
        <td class="consumer_point_text"><?=$consu_email?></td>
        <td class="consumer_point_text"><?=$consu_phone?></td>
        <td class="consumer_point_text"><?=$two?></td>
        <td class="consumer_point_text"><?=$five?></td>
        <td class="consumer_point_text"><?=$ten?></td>
        <td class="consumer_point_text"><?=$twenty?></td>
        <td class="consumer_point_text"><?=$fifty?></td>
        <td class="consumer_point_text"><?=$hundred?></td>
        <td class="consumer_point_text"><?=$five_hundred?></td>
        <td class="consumer_point_text"><?=$total_bucks?></td>
    </tr>

<?php } ?>      
</table>

<div class="show_more_main" sbb_id="show_more_main<?php echo $sbb_id; ?>">
    <span sbb_id="<?php echo $sbb_id; ?>" class="show_more" title="Load more posts">Load more</span>
    <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script
<script src="http://demos.codexworld.com/includes/js/bootstrap.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
    $(document).on('click','.show_more',function(){
    var ID = $(this).attr('sbb_id');
    $('.show_more').hide();
    $('.loding').show();
    $.ajax({
        type:'POST',
        url:'ajax_more_business_shop.php',
        data:'sbb_id='+ID,
        success:function(html){
            $('#show_more_main'+ID).remove();
            $('.postList').append(html);
            }
        });
    });
});
</script>

<?php }  ?> 
</div> <!-- ./col-lg-12 -->
<?php } }?>

在此页面上显示 2 个具有共同 busi_id (WHERE busi_id=$busi_id) 的结果,当我单击加载更多按钮时,它也会显示与此页面上首先显示的“busi_id”不同的其他内容2 个结果。 这是ajax页面:

ajax_more_business_shop.php:

<?php
    ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); 

if(!empty($_POST["sbb_id"]))  {
    require_once ('admin/includes/config.php');
    $query = "SELECT COUNT(*) as num_rows FROM savingsbucks_business WHERE sbb_id < ".$_POST['sbb_id']." ORDER BY sbb_id DESC";

$result = mysqli_query ($conn, $query);
$row = mysqli_fetch_assoc($result);
$totalRowCount = $row['num_rows'];
$busi_id = $row['busi_id'];
$showLimit = 2;

$query = "SELECT * FROM savingsbucks_business WHERE sbb_id < ".$_POST['sbb_id']." ORDER BY sbb_id DESC LIMIT $showLimit";

$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rowcount=mysqli_num_rows($result); 

?>

<table border="1" class="table table-bordered"> 
    <?php  
        if ($rowcount > '0') {
        while($row = mysqli_fetch_assoc($result)) 
    {
    $sbb_id = $row['sbb_id'];
    $sender_id = $row['sender_id'];
    $busi_id = $row['busi_id'];
    $type = $row['type'];
    $consu_name = $row['consu_name'];
    $consu_email = $row['consu_email'];
    $consu_phone = $row['consu_phone'];
    $two = $row['two'];
    $five = $row['five'];
    $ten = $row['ten'];
    $twenty = $row['twenty'];
    $fifty = $row['fifty'];
    $hundred = $row['hundred'];
    $five_hundred = $row['five_hundred'];
    $total_two += $two;
    $total_five += $five;
    $total_ten += $ten;
    $total_twenty += $twenty;
    $total_fifty += $fifty;
    $total_hundred += $hundred;
    $total_five_hundred += $five_hundred;
    $total_bucks = $two+$five+$ten+$twenty+$fifty+$hundred+$five_hundred;
    $grand_total += $total_bucks;
?>

    <tr>
    <td class="consumer_point_text"><?=$sbb_id?></td>
    <td class="consumer_point_text"><?=$sender_id?></td>
    <td class="consumer_point_text"><?=$busi_id?></td>
    <td class="consumer_point_text"><?=$type?></td>
    <td class="consumer_point_text"><?=$consu_name?></td>
    <td class="consumer_point_text"><?=$consu_email?></td>
    <td class="consumer_point_text"><?=$consu_phone?></td>
    <td class="consumer_point_text"><?=$two?></td>
    <td class="consumer_point_text"><?=$five?></td>
    <td class="consumer_point_text"><?=$ten?></td>
    <td class="consumer_point_text"><?=$twenty?></td>
    <td class="consumer_point_text"><?=$fifty?></td>
    <td class="consumer_point_text"><?=$hundred?></td>
    <td class="consumer_point_text"><?=$five_hundred?></td>
    <td class="consumer_point_text"><?=$total_bucks?></td>
    </tr>
    <?php } 
    ?>
</table>

<?php if($totalRowCount > $showLimit){ ?>

 <div class="show_more_main" sbb_id="show_more_main<?php echo $sbb_id; ?>">
        <span sbb_id="<?php echo $sbb_id; ?>" class="show_more" title="Load more posts">Show more</span>
        <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
    </div>
<?php } ?>
    <?php
    }
 }
?>

我想,当我点击“加载更多按钮”时,它必须显示/加载更多 2 行内容,其中 busi_id 很常见,我的意思是如果 busi_id=69,它应该只显示/加载 2 更多内容/结果busi_id 为 69 的数据库,而不是具有不同 busi_id 的其他数据。

我试图解释,

谢谢。

【问题讨论】:

  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。
  • @Dharman,请你帮忙,我来这里是为了什么..拜托?
  • 有人会帮助或建议我吗?

标签: php jquery mysql ajax


【解决方案1】:

我刚刚将“busi_id”传递给 Ajax 页面...带有加载更多按钮..

<div class="show_more_main" ds_id="show_more_main<?php echo $ds_id; ?>">
    <span ds_id="<?php echo $ds_id; ?>" busi_id="<?php echo $busi_id; ?>" class="show_more" title="Load more posts">Load more</span>
    <span class="" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>

还有..

<script type="text/javascript">
    $(document).ready(function(){
    $(document).on('click','.show_more',function(){
    var ID = $(this).attr('ds_id');
    var Busi_id = $(this).attr('busi_id');

    $('.show_more').hide();
    $('.loding').show();
    $.ajax({
        type:'POST',
        url:'ajax_more_consumer_shop.php',

        data: {
            'ds_id': ID,
            'busi_id': Busi_id
            },


        success:function(html){
            $('#show_more_main'+ID).remove();
            $('.postList').append(html);
        }
    });
});

});

关于 ajax_more_business_shop.php....

$query = "SELECT COUNT(*) as num_rows FROM savingsbucks_business WHERE ds_id < ".$_POST['ds_id']." AND busi_id=".$_POST["busi_id"]." ORDER BY ds_id DESC";
$result = mysqli_query ($conn, $query);
$row = mysqli_fetch_assoc($result);
$totalRowCount = $row['num_rows'];
$showLimit = 2;

$query = "SELECT * FROM savingsbucks_business WHERE ds_id < ".$_POST['ds_id']." AND busi_id=".$_POST["busi_id"]." ORDER BY ds_id DESC LIMIT $showLimit";
$result = mysqli_query ($conn, $query);
$rowcount=mysqli_num_rows($result);

修改查询..并获得所需的输出。

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 2020-08-03
    • 2022-11-30
    • 2015-09-29
    • 2018-06-10
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多