如何在数据库查询中使用会话和数组来存储和显示数据 [英] how to use sessions and arrays in a database query to store and display data

查看:73
本文介绍了如何在数据库查询中使用会话和数组来存储和显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to save data into an array and use session after inputting data. I have tried and succeeded. but I want to display some data taken from the database and save it to an array and session earlier. I have tried adding database queries, but the data can only hold one data only.

<?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);


}


 function display(){

if (!empty($_SESSION['merk'])) {
    $merk = $_SESSION['merk'];
$model =    $_SESSION['model'];
$warna =    $_SESSION['warna'];
$kuantiti =     $_SESSION['kuantiti'];
$harga =    $_SESSION['harga'];
$harga_tawar = $_SESSION['harga_tawar'];
$harga_jual = $_SESSION['harga_jual'];


$tgl = date('d-m-y');

echo '  <table class="table table-bordered table-striped text-gray-900" id="dataTable""> 
        <thead>

                <tr>
                <th colspan="5">'.$tgl.'</th>
                </tr>
                    <tr>

                      <th>Jenis Sepatu </th>
                      <th>Kuantiti</th>
                      <th>Harga</th>
                      <th>Harga Tawar</th>
                      <th>Total</th>
                    </tr>
              </thead>
              
    ';
$total=0;
$no = 1;
    for ($i = 0; $i < count($model); $i++ ) { 
        //$mk = implode("", $merk[$i]);
        //$md = implode("", $model[$i]);
        //$wr = implode("", $warna[$i]);
        
        echo '<tbody>
                    
                        <tr>
                        
                        <td>'.$merk[$i].' '.$model[$i].' '. $warna[$i].'</td>
                        <td>'.$kuantiti[$i].'</td>
                        <td>'.$harga[$i].'</td>
                        <td>'.$harga_tawar[$i].'</td>
                        <td>'.number_format($harga_jual[$i],0,',','.').'</td></tr>';

        $total = $total + $harga_jual[$i];
        

    }

    echo '<tr><td>Total</td>
            <td></td>
            <td></td>
            <td></td>
            
            
            <td>'.number_format($total,0,',','.').'</td>
            
            </tr> </tbody> </table>';

    }else {

    }
  }



  ?>

在此处输入图片描述

在此处输入图片描述

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

so it can only hold 1 data only. if new data is input, the previous data is lost.

推荐答案

您将覆盖while循环中的所有三个变量每次迭代,这就是为什么您只得到一个结果(这是在 while 循环内执行的最后一个数据集。

you are overwriting all three of your variables inside the while loop in each iteration, That's why you are just getting one result(which was the last data set that was executed inside the while loop.

基本上,您可以执行3件事来解决问题。

There are basically 3 things that you can do to solve your issue.

1。使用数组存储从循环中获得的所有结果

1.Use an array to store all the results that you get from the loop

    while ($y=mysqli_fetch_array($sql)) {
          $mrk[] = implode("", DekripCBC($y['nama_merk']));
          $mdl[] = implode("", DekripCBC($y['nama_model']));
          $wrn[] = implode("", DekripCBC($y['warna']));
     }

2。使用预定义的数组存储所有结果您可以使用 array_push 从循环中获取。

2.Use an pre-defined array to store all the results that you get from the loop using array_push.

  /**There are 3 ways to define an empty array. You can use any of the following
      1.$emptyArray = [];
      2.$emptyArray = array();
      3.$emptyArray = (array) null;
   * */

  $dataSet =[];

    while ($y=mysqli_fetch_array($sql)) {
          $data['mrk'] = implode("", DekripCBC($y['nama_merk']));
          $data['mdl'] = implode("", DekripCBC($y['nama_model']));
          $data['wrn'] = implode("", DekripCBC($y['warna']));

        array_push($dataSet ,$data);
     }

3。将值连接到变量中

3.concatenate the values into your variable

  while ($y=mysqli_fetch_array($sql)) {
    // 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
         $mrk .= implode("", DekripCBC($y['nama_merk'])).','; 
         $mdl .= implode("", DekripCBC($y['nama_model'])).',';
         $wrn .= implode("", DekripCBC($y['warna'])).',';
  }

我强烈建议您使用方法1或方法2。

I would highly recommend you to use method no.1 or no.2.

我希望这可以帮助您解决问题。

I hope this would help you to solve your issue.

如果您要使用查询来搜索多个ID :(因为我在评论中提到了

If you want to search for multiple ids using the query:(since i mentioned in the comments i'll add it to here.)

EX:

//You have multiple ids that you get from the session.

$jns = [12,13,4,5]; 

 //So now you can use a foreach loop to loop all the ids and use the query to get relevant data

 foreach($jns 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']));
     }
  }

通过此方法,您可以获取提及的ID的所有数据并将其分配给数组。

By this method you can get all the data for the ids that is mentioned above and assign it to the array.

这篇关于如何在数据库查询中使用会话和数组来存储和显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆