为什么我的下拉列表无法在我的foreach循环中工作? PHP

时间:2016-09-21 14:09:58

标签: php foreach

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

它有什么不对吗?

这里有一些照片

my table2 this is wrong

这会抓住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;
                } 
            }
        }
    }
}
?>

1 个答案:

答案 0 :(得分:2)

每次循环循环时,您都会为选项下拉列表选择整个表格

@if(Request::is('transactions/generate-invoice'))
    @include('generate-invoice-css')
@elseif(Request::is('transactions/users'))
    @include('users-css')
@endif

但每次循环时都会覆盖styles.blade.php变量

将代码移到循环之外会更简单,更快速,更高效,同时构建包含选项标记的字符串

所以我建议稍微改写一下

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

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