问题
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 {
}
}
?>
enter image description here
enter image description here
so it can only hold 1 data only. if new data is input, the previous data is lost.
回答1:
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.
There are basically 3 things that you can do to solve your issue.
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.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.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'])).',';
}
I would highly recommend you to use method no.1 or no.2.
I hope this would help you to solve your issue.
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']));
}
}
By this method you can get all the data for the ids that is mentioned above and assign it to the array.
回答2:
The thing which is causing this issue is because it is adding to variable and same variable gets overrite everytime because of same variable name. You eitheir need to empty those variables push them to some other place and can use it.
For this we can use php function array_push where we will push them into master array.
$master = array();
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($master,$data);
}
来源:https://stackoverflow.com/questions/62771686/how-to-use-sessions-and-arrays-in-a-database-query-to-store-and-display-data