为什么我的下拉菜单在我的foreach循环中不起作用?的PHP

詹姆斯·艾伦

我有一个foreach循环,可以在我选择要添加到购物车中的产品的会话中获取ID。它在那里工作正常。问题是..我有一个选择标记,它从其他表中获取数据。我在图库中选择的产品来自table1,而table1在我的foreach循环中效果很好。我的选择标记未在foreach循环中显示我的所有table2行。

它出什么问题了?

这是一些照片

我的桌子2 这是错的

这将获取ID并将其存储在会话中...

include_once '../incluedes/conn_cms.php'; 
if(isset($_GET['add'])){
    $select = "SELECT * FROM gallery2 WHERE id=" . escape_string($_GET['add'])." ";
    $run_selection = mysqli_query($conn,$select);
    while($rows = mysqli_fetch_assoc($run_selection)){
        if($rows['id'] != $_SESSION['product_'.$_GET['add']]){
            $_SESSION['product_' . $_GET['add']]+=1;
            header('Location: index.php');
        }else{
            $msg = "error";
            header('Location: checkout.php');
        }
    }
}

我的代码...

function cart(){
    global $conn;

    foreach ($_SESSION as $name => $value) {
        if($value > 0){
            if(substr($name, 0, 8 ) == "product_"){
                $length = strlen($name) -8;
                $item_id = substr($name,8 , $length);

                $query = "SELECT * 
                          FROM gallery2 
                          WHERE gallery2.id =".escape_string($item_id). "";
                $run_item = mysqli_query($conn,$query);

                $query2 = "SELECT * FROM almofadas";
                $run_item2 = mysqli_query($conn,$query2);

                while($rows = mysqli_fetch_assoc($run_item2)){
                        $fabric=$rows['tecido'];
                }

                while($rows = mysqli_fetch_assoc($run_item)){ 
                    $vari = $rows['variante'];
                    $num = $rows['title'];
                    $id = $rows['id'];

                    $btn_add='<a class="btn btn-success" href="cart.php?add='.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></a>';
                    $btn_remove = '<a class="btn btn-warning" href="cart.php?remove='.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></a>';
                    $btn_delete='<a class="btn btn-default delete_btn" href="cart.php?delete='.$id.'"><i class="fa fa-times fa-lg" aria-hidden="true"></i></a>';
                    if($rows['variante'] < 1){
                        $vari="";
                    }else{
                        $vari = "-".$rows['variante'];
                    }
                    $product = '
                        <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
                        <td>'.$num.''.$vari.'</td>
                        <td>
                            <select name="" class="form-control selectpicker" required="">
                                <option value="" required="">'.$fabric.'</option>
                            </select>
                        </td>
                        <td>'.$value.'</td>
                        <td>R$100,00</td>
                        <td>sub.total</td>
                        <td> 
                         '.$btn_add.' '.$btn_remove.' '.$btn_delete.'
                        </td>
                        </tr>';
                    echo $product;
                } 
            }
        }
    }
}
?>
里格斯愚蠢

每次在此处循环时,您都可以从下拉菜单中选择整个表格

$query2 = "SELECT * FROM almofadas";
$run_item2 = mysqli_query($conn,$query2);

while($rows = mysqli_fetch_assoc($run_item2)){
        $fabric=$rows['tecido'];
}

$fabric每次循环时都会覆盖变量

将代码移到循环外会更简单,更快和更有效,同时构建一个包含选项标签的字符串。

所以我建议改写一下

function cart(){
    global $conn;

    // build the fabric dropdown option tags once
    // use as many times as you have a row ro put them in
    $fabric_options = '';
    $query = "SELECT * FROM almofadas";
    $result = mysqli_query($conn,$query2);
    while($rows = mysqli_fetch_assoc($run_item2)){

        // oh you will need a value in value=""
        // or this wont be any use to you later

        $fabric_options .= "<option value='{$row['A_id']}'>{$rows['tecido']}</option>";

    }

    foreach ($_SESSION as $name => $value) {
        if($value > 0){
            if(substr($name, 0, 8 ) == "product_"){
                $length = strlen($name) -8;
                $item_id = substr($name,8 , $length);

                $query = "SELECT * 
                          FROM gallery2 
                          WHERE gallery2.id =".escape_string($item_id). "";
                $run_item = mysqli_query($conn,$query);



                while($rows = mysqli_fetch_assoc($run_item)){ 
                    $vari = $rows['variante'];
                    $num = $rows['title'];
                    $id = $rows['id'];

                    $btn_add='<a class="btn btn-success" href="cart.php?add='.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></a>';
                    $btn_remove = '<a class="btn btn-warning" href="cart.php?remove='.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></a>';
                    $btn_delete='<a class="btn btn-default delete_btn" href="cart.php?delete='.$id.'"><i class="fa fa-times fa-lg" aria-hidden="true"></i></a>';
                    if($rows['variante'] < 1){
                        $vari="";
                    }else{
                        $vari = "-".$rows['variante'];
                    }

                    // now concatenate the $fabric_options string
                    // in between this string after the select

                    $product = '
                        <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
                        <td>'.$num.''.$vari.'</td>
                        <td>
                            <select name="" class="form-control selectpicker" required="">'
                            . $fabric_options . '     
                            </select>
                        </td>
                        <td>'.$value.'</td>
                        <td>R$100,00</td>
                        <td>sub.total</td>
                        <td> 
                         '.$btn_add.' '.$btn_remove.' '.$btn_delete.'
                        </td>
                        </tr>';
                    echo $product;
                } 
            }
        }
    }
}
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的下拉菜单在 div 中不起作用?

我的下拉菜单不起作用

为什么下拉菜单在bootstrap5导航栏中不起作用?

为什么我的下拉菜单不起作用?(HTML&CSS)

为什么在我的jQuery循环中返回不起作用?

为什么 strptime() 在我的 for 循环中不起作用?

为什么我的函数在循环中不起作用?

为什么这个下拉菜单不起作用?

为什么引导程序下拉菜单不起作用?

CSS下拉菜单在Chrome中不起作用

下拉菜单在 div 内或动态不起作用

下拉菜单在照明元件中不起作用

CSS下拉菜单在悬停时不起作用

下拉菜单在Webview中不起作用

我的可点击 jQuery 下拉菜单不起作用

为什么我的foreach循环不起作用?

为什么我的菜单在RichFaces 4.3.4中不起作用?

为什么我的嵌套子菜单在 vue-router 中不起作用?

为什么我的菜单不起作用?

Bootstrap 下拉菜单在 php codeigniter 中不起作用

Laravel 为什么我的下拉菜单在悬停时显示所有元素?

为什么 For 循环中的 While 循环在我的程序中不起作用?

下拉菜单在导航栏中不起作用,但在外部起作用

为什么这些continue语句在我的循环中不起作用?

Python:为什么我的写入功能在while循环中不起作用?

为什么我的do-while循环中的continue关键字不起作用?

CSS 菜单,当内容较长时,下拉菜单在桌面视图上不起作用

foreach 循环中的下拉菜单

我的响应菜单在移动版本中不起作用