猿问

如何在数据库查询中使用会话和数组来存储和显示数据

我想将数据保存到数组中并在输入数据后使用会话。我已经尝试过并且成功了。但我想显示从数据库中获取的一些数据并将其保存到数组和会话中。我尝试过添加数据库查询,但数据只能保存一项数据。


<?php

if (!isset($_SESSION)) {

session_start();

# code...

}



include_once "AlgoCBC.php";


function additem($jns, $hrg, $hrg_tw, $total, $kt_satu, $kt_dua, $kt_tiga, $kt_empat, $kt_lima, $kt_enam, $kt_tujuh){

    


    if (empty($_SESSION['$jns'])) {


        include "koneksi.php";


        $_SESSION['jenis'] = array();

        $jen = array_push($_SESSION['jenis'], $jns);

    

        

        foreach ($jen as $id) {

            

        

        $sql = mysqli_query($kns, "Select stok.id_stok as id_stok, merk.nama_merk as nama_merk, model.nama_model as nama_model, stok.warna as warna FROM stok INNER JOIN model On stok.id_model=model.id_model INNER JOIN merk ON model.id_merk=merk.id_merk where id_stok = '$id' ") or die(mysqli_error($kns));




                 while ($y=mysqli_fetch_array($sql)) {

                    $mrk[] = implode("", DekripCBC($y['nama_merk']));

                    $mdl[] = implode("", DekripCBC($y['nama_model']));

                    $wrn[] = implode("", DekripCBC($y['warna']));

                 }


                }


    $_SESSION['merk'] = array();

    $_SESSION['model'] = array();

    $_SESSION['warna'] = array();

    $_SESSION['kuantiti'] = array();

    $_SESSION['harga'] = array();

    $_SESSION['harga_tawar'] = array();

    $_SESSION['harga_jual'] = array();

    }


    $kn = $kt_satu + $kt_dua + $kt_tiga + $kt_empat + $kt_lima + $kt_enam + $kt_tujuh;



    array_push($_SESSION['merk'], $mrk);

    array_push($_SESSION['model'], $mdl);

    array_push($_SESSION['warna'], $wrn);

    array_push($_SESSION['kuantiti'], $kn); 

    array_push($_SESSION['harga'], $hrg);

    array_push($_SESSION['harga_tawar'], $hrg_tw);

    array_push($_SESSION['harga_jual'],$total);



}


所以它只能保存1个数据。如果输入新数据,则先前的数据将丢失。


扬帆大鱼
浏览 106回答 2
2回答

Helenr

您在每次迭代中覆盖 while 循环内的所有三个变量,这就是为什么您只得到一个结果(这是循环内执行的最后一个数据集while。您基本上可以采取 3 件事来解决您的问题。1.使用数组存储循环得到的所有结果&nbsp; &nbsp; while ($y=mysqli_fetch_array($sql)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mrk[] = implode("", DekripCBC($y['nama_merk']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mdl[] = implode("", DekripCBC($y['nama_model']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $wrn[] = implode("", DekripCBC($y['warna']));&nbsp; &nbsp; &nbsp;}2.使用预定义的数组来存储使用 循环获得的所有结果array_push。&nbsp; /**There are 3 ways to define an empty array. You can use any of the following&nbsp; &nbsp; &nbsp; 1.$emptyArray = [];&nbsp; &nbsp; &nbsp; 2.$emptyArray = array();&nbsp; &nbsp; &nbsp; 3.$emptyArray = (array) null;&nbsp; &nbsp;* */&nbsp; $dataSet =[];&nbsp; &nbsp; while ($y=mysqli_fetch_array($sql)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['mrk'] = implode("", DekripCBC($y['nama_merk']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['mdl'] = implode("", DekripCBC($y['nama_model']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['wrn'] = implode("", DekripCBC($y['warna']));&nbsp; &nbsp; &nbsp; &nbsp; array_push($dataSet ,$data);&nbsp; &nbsp; &nbsp;}3.将值连接到变量中&nbsp; while ($y=mysqli_fetch_array($sql)) {&nbsp; &nbsp; // I have added a seprator(,) for all the variables so that there will be a , after every result and it wouldn't make you confused at the end&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$mrk .= implode("", DekripCBC($y['nama_merk'])).',';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$mdl .= implode("", DekripCBC($y['nama_model'])).',';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$wrn .= implode("", DekripCBC($y['warna'])).',';&nbsp; }我强烈建议您使用方法 1 或方法 2。我希望这能帮助您解决您的问题。如果您想使用查询搜索多个 id:(因为我在评论中提到,我会将其添加到此处。)前任://You have multiple ids that you get from the session.$jns = [12,13,4,5];&nbsp;&nbsp;//So now you can use a foreach loop to loop all the ids and use the query to get relevant data&nbsp;foreach($jns as $id){&nbsp; &nbsp; &nbsp;$sql = mysqli_query($kns, "Select stok.id_stok as id_stok, merk.nama_merk as nama_merk, model.nama_model as nama_model, stok.warna as warna FROM stok INNER JOIN model On stok.id_model=model.id_model INNER JOIN merk ON model.id_merk=merk.id_merk where id_stok = '$id' ") or die(mysqli_error($kns));&nbsp; &nbsp; while ($y=mysqli_fetch_array($sql)) {&nbsp; &nbsp; &nbsp; &nbsp;$mrk[] = implode("", DekripCBC($y['nama_merk']));&nbsp; &nbsp; &nbsp; &nbsp;$mdl[] = implode("", DekripCBC($y['nama_model']));&nbsp; &nbsp; &nbsp; &nbsp;$wrn[] = implode("", DekripCBC($y['warna']));&nbsp; &nbsp; &nbsp;}&nbsp; }通过这种方法,您可以获取上面提到的 ids 的所有数据并将其分配给数组。

慕娘9325324

导致此问题的原因是它添加到变量中,并且由于相同的变量名称,相同的变量每次都会被覆盖。您要么需要清空这些变量,要么将它们推送到其他地方并可以使用它。为此,我们可以使用 php 函数 array_push 将它们推送到主数组中。$master = array();while ($y=mysqli_fetch_array($sql)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['mrk'] = implode("", DekripCBC($y['nama_merk']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['mdl'] = implode("", DekripCBC($y['nama_model']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['wrn'] = implode("", DekripCBC($y['warna']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($master,$data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
随时随地看视频慕课网APP
我要回答