【问题标题】:How do I get items by category wise如何按类别明智地获取项目
【发布时间】:2016-10-25 07:58:19
【问题描述】:

基本上,我有一个从 mysqli 迭代的类别列表

<div style="width:20%; float:left;">
            <h3>Categories</h3>
            <?php while($categories = $resultproductcategories->fetch_assoc()){?> 
            <div class="rows">
                <input type="button" name="<?php echo $categories['id'] ?>" value="<?php echo $categories['category?>" />;
            </div>
            <?php }?>
        </div>

现在我想要的是,如果我按任何类别,它应该相应地获取数据,这是我的获取代码

    $stmtproducts = $mysqli->prepare("SELECT * FROM store_products sp INNER JOIN store_product_categories spc ON sp.product_category=spc.id WHERE sp.store_id=? AND sp.category=? LIMIT $count, 10");
$stmtproducts->bind_param("ii",$_SESSION['storeid'],$_POST['']);
$stmtproducts->execute();
$resultproducts = $stmtproducts->get_result();
$num=$resultproducts->num_rows;
echo $num;
$stmtproducts->close();

我很困惑,如果有一个特定的输入名称,那么我会通过isset($_POST['name']) 得到它,但是没有特定的名称......我想不出如何将类别发送到 mysql。

<div id="divTransactional" style="width:70%;padding-left:5%; float:left;">
            <?php if($num>0) {?>
            <table class="table table-responsive table-hover" id="tableProducts">
                <h3>Products</h3>
                <tbody>
                    <?php for($i=0;$i<$num;$i++) { $products = $resultproducts->fetch_assoc();//while($products = $resultproducts->fetch_assoc()) {?>
                    <tr>
                        <td>Code: <?php echo $products['product_code'];?></td><td>Added On: <?php echo $products['product_date'];?></td>
                    </tr>
                    <tr>
                        <td>Name: <?php echo $products['product_name'];?></td>
                    </tr>
                    <tr>
                        <td>Category: <?php echo $products['category'];?></td> 
                    </tr>
                    <tr>
                        <td>Description: <?php echo $products['product_desc'];?></td> 
                    </tr>
                    <tr>
                        <td>Price: <?php echo $products['product_price'];?></td><td>Discount: <?php echo $products['product_discount'];?></td>  
                    </tr>

                    <tr style="width:100px; height:100px;">
                        <td><img src="../../uploads/store/products/<?php echo $products['product_image1'];?>"  style="width:100px; height:100px;"/></td>
                        <td><img src="../../uploads/store/products/<?php echo $products['product_image2'];?>"  style="width:100px; height:100px;"/></td>
                        <td><img src="../../uploads/store/products/<?php echo $products['product_image3'];?>"  style="width:100px; height:100px;"/></td>
                        <td><img src="../../uploads/store/products/<?php echo $products['product_image4'];?>"  style="width:100px; height:100px;"/></td>
                    </tr>
                    <?php }?>
                </tbody>
                <?php if($num >= 10) { ?>
                <a href="<?php echo $_SERVER['PHP_SELF'].'?count='.($count+10)?>">Next</a>&nbsp;
                <?php } ?>
                <?php $prev = $count - 10;
                if ($prev >= 0){?>
                <a href=" <?php echo $_SERVER['PHP_SELF'].'?count='.$prev ?>">Previous</a>
                <?php }?>



            </table>
                <?php if($num >= 10) { ?>
                <a href="<?php echo $_SERVER['PHP_SELF'].'?count='.($count+10)?>">Next</a>&nbsp;
                <?php } ?>
                <?php $prev = $count - 10;
                if ($prev >= 0){?>
                <a href=" <?php echo $_SERVER['PHP_SELF'].'?count='.$prev ?>">Previous</a>
                <?php }?>
            <?php } ?>

        </div>

【问题讨论】:

  • 你想显示数据还是从服务器获取数据?
  • 从服务器获取它...假设有两个类别迭代... category1 和 category2...当我单击 category1 时,应该只获取 category1 数据
  • 我建议使用 ajax。
  • 你熟悉javascript和JQuery吗?
  • 不多,但我也希望在 url 上附加类别!

标签: php mysqli


【解决方案1】:

我建议使用 Ajax 在参数中使用 Id 调用服务器。
您需要为按钮添加一个类,并且确实需要链接 JS 文件和 JQuery。这是一个例子。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="path/to/your/file.js"></script>
<div style="width:20%; float:left;">
    <h3>Categories</h3>
    <?php while($categories = $resultproductcategories->fetch_assoc()){?> 
    <div class="rows">
    <input type="button" name="<?php echo $categories['id'] ?>" value="<?php echo $categories['category']?>" class="btnCategory"/>;
</div>
<?php }?>


然后,您可以使用 ajax 调用您的 php 文件,该文件返回您需要从数据库中获取的值。

$(document).ready(function() {
$('.btnCategory').on('click', function(){
    var id = $this.attr('name');
    $.ajax({
        url: 'path/to/your/file.php',
        method: "GET",
        data: {category_id: id}, 
        success: function(data) {
            //This is the data fetch from the server
            console.log(data);
        },
        error: function(err) {
            console.error('An error occurred : ' + err);
        }
    });
});
});

然后,当您的 PHP 文件收到参数 $_GET['category_id'] 时,您将调用您的数据库。

<?php
if(isset($_GET['category_id'])){
//An id was post in the url.
    $stmtproducts = $mysqli->prepare("SELECT * FROM store_products sp INNER JOIN store_product_categories spc ON sp.product_category=spc.id WHERE sp.store_id=? AND sp.category=? LIMIT $count, 10");
    $stmtproducts->bind_param("ii",$_SESSION['storeid'],$_POST['']);
    $stmtproducts->execute();
    $resultproducts = $stmtproducts->get_result();
    $num=$resultproducts->num_rows;
    $stmtproducts->close();
    $result = array('row_count' => $num, 'result' => $resultproducts);
    echo json_encode($result);
}

然后返回结果的 json 数组。这就是我会做的。 希望能帮助到你 !

  • 尼克

【讨论】:

  • 你先生是真正的MVP
  • 是的,最后一件事你能修改它,让我得到 file.php 的 html 并在当前页面上显示它吗?
  • 你可以,你需要做的就是通过删除url参数来调用同一个页面。
  • 并为 PHP 添加结束标签?>
  • 嗯......我的 jquery 脚本并不多。不好意思可以给演示一下吗?
【解决方案2】:

您要求演示,这里是 .

<?php
if(isset($_GET['category_id'])){
//An id was post in the url.
    $stmtproducts = $mysqli->prepare("SELECT * FROM store_products sp INNER JOIN store_product_categories spc ON sp.product_category=spc.id WHERE sp.store_id=? AND sp.category=? LIMIT $count, 10");
    $stmtproducts->bind_param("ii",$_SESSION['storeid'],$_POST['']);
    $stmtproducts->execute();
    $resultproducts = $stmtproducts->get_result();
    $num=$resultproducts->num_rows;
    $stmtproducts->close();
    $result = array('row_count' => $num, 'result' => $resultproducts);
    echo json_encode($result);
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div style="width:20%; float:left;">
<h3>Categories</h3>
<?php while($categories = $resultproductcategories->fetch_assoc()){?> 
<div class="rows">
    <input type="button" name="<?php echo $categories['id'] ?>" value="<?php echo $categories['category']?>" class="btnCategory"/>;
</div>
<?php }?>
</div>

<script>
$(document).ready(function() {
$('.btnCategory').on('click', function(){
    var id = $this.attr('name');
    $.ajax({
        method: "GET",
        data: {category_id: id}, 
        success: function(data) {
            //This is the data fetch from the server
            console.log(data);
        },
        error: function(err) {
            console.error('An error occurred : ' + err);
        }
    });
});
});
</script>

【讨论】:

  • 它完美地在控制台上提供数据我如何用新的网址刷新页面? manageproducts.php?category_id=21 这是通过检查元素。
  • 你不只是使用它返回的数据并玩弄它,
  • 我很少做 jquery 的东西......而且我们学院没有教 ajax!
  • ajax 请求有效,您所要做的就是使用它返回的数据并使用 html()、append() 和 prepend() 函数更改页面数据。我不能为你做所有的工作。
  • 是的,我正在考虑这个问题!但它不会刷新网址不是吗?
猜你喜欢
  • 2021-10-08
  • 2022-12-03
  • 2021-11-24
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
相关资源
最近更新 更多