php—如何基于id执行for函数,数据库中有数据

t30tvxxf  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(279)

我在示例数据库中有一个表:
编号:1
最小尺寸:23
最大尺寸:27
编号:2
最小尺寸:37
最大尺寸:43。
我想这样显示数据:
编号:1
尺码:23 24 25 26 27 0 0
编号:2
尺码:37 38 39 40 41 42 43
更具体地说,我希望只有7个大小单位出现,如果有小于7,那么它将显示零(0)。

$U = array();
$k=0;

for($i=$y['ukuran_terkecil']; $i <= $y['ukuran_terbesar'] ; $i++) { 
                  $U[$k]= $i;

                $k++;
              }
            //print_r($U);
              //echo implode("", $U);
              if(empty($U[0]))
                  {
                   $U[0]= 0;
                  }else {
                    $U[0] = $U[0];
                     }

                     if(empty($U[1]))
                  {
                   $U[1]= 0;
                  }else {
                    $U[1] = $U[1];
                     }

                     if(empty($U[2]))
                  {
                   $U[2]= 0;
                  }else {
                    $U[2] = $U[2];
                     }

                     if(empty($U[3]))
                  {
                   $U[3]= 0;
                  }else {
                    $U[3] = $U[3];
                     }

                     if(empty($U[4]))
                  {
                   $U[4]= 0;
                  }else {
                    $U[4] = $U[4];
                     }

                     if(empty($U[5]))
                  {
                   $U[5]= 0;
                  }else {
                    $U[5] = $U[5];
                     }

                     if(empty($U[6]))
                  {
                   $U[6]= 0;
                  }else {
                    $U[6] = $U[6];
              }
    echo “<tr><td>$U[0]<br>$U[1]<br>$U[2]<br>$U[3]<br>$U[4]<br>$U[5]<br>$U[6]</td></tr>”;
``` `$y : query select` 但输出是:
身份证1:23 24 25 26 27 0 0
身份证号码:23 24 25 26 27 37 38
样本数据:

$data = array (
0 =>
array (
0 => '0C529E7D34AE7196',
'id' => '0C529E7D34AE7196',
1 => '0',
'stoksatu' => '0',
2 => '0',
'stokdua' => '0',
3 => '2',
'stoktiga' => '2',
4 => '2',
'stokempat' => '2',
5 => '2',
'stoklima' => '2',
6 => '1',
'stokenam' => '1',
7 => '2',
'stoktujuh' => '2',
8 => '2C2C243A1C7A',
'warna' => '2C2C243A1C7A',
9 => '37',
'ukuran_terkecil' => '37',
10 => '43',
'ukuran_terbesar' => '43',
11 => 'hugob026.png',
'foto' => 'hugob026.png',
12 => 'SpHu26Co.png',
'qrcode' => 'SpHu26Co.png',
13 => '3A340C6CF62780C7',
'nama_model' => '3A340C6CF62780C7',
14 => '0C52CCD7EDB3',
'nama_merk' => '0C52CCD7EDB3',
),
1 =>
array (
0 => '281E300870803B0E',
'id' => '281E300870803B0E',
1 => '2',
'stoksatu' => '2',
2 => '2',
'stokdua' => '2',
3 => '2',
'stoktiga' => '2',
4 => '1',
'stokempat' => '1',
5 => '0',
'stoklima' => '0',
6 => '0',
'stokenam' => '0',
7 => '0',
'stoktujuh' => '0',
8 => '3A0C5ADCC9',
'warna' => '3A0C5ADCC9',
9 => '24',
'ukuran_terkecil' => '24',
10 => '27',
'ukuran_terbesar' => '27',
11 => '3.PNG',
'foto' => '3.PNG',
12 => 'ArSaeeHi.png',
'qrcode' => 'ArSaeeHi.png',
13 => '0C70AE351850C0',
'nama_model' => '0C70AE351850C0',
14 => '281E5EC4FB9763',
'nama_merk' => '281E5EC4FB9763',
),
2 =>
array (
0 => '222C60825DE2FF870572',
'id' => '222C60825DE2FF870572',
1 => '1',
'stoksatu' => '1',
2 => '1',
'stokdua' => '1',
3 => '2',
'stoktiga' => '2',
4 => '1',
'stokempat' => '1',
5 => '2',
'stoklima' => '2',
6 => '0',
'stokenam' => '0',
7 => '0',
'stoktujuh' => '0',
8 => '3A0C5ADCC979F88965B6',
'warna' => '3A0C5ADCC979F88965B6',
9 => '40',
'ukuran_terkecil' => '40',
10 => '44',
'ukuran_terbesar' => '44',
11 => 'IMG-20190428-WA0018.jpg',
'foto' => 'IMG-20190428-WA0018.jpg',
12 => 'DaItyyHiPi.png',
'qrcode' => 'DaItyyHiPi.png',
13 => '38327AAC01',
'nama_model' => '38327AAC01',
14 => '222C2A262404',
'nama_merk' => '222C2A262404',
),
);

$t=mysqli_query($kns,"select
stok.id_stok as id,
stok.st_satu as stoksatu,
stok.st_dua as stokdua,
stok.st_tiga as stoktiga,
stok.st_empat as stokempat,
stok.st_lima as stoklima,
stok.st_enam as stokenam,
stok.st_tujuh as stoktujuh,
stok.warna as warna,
stok.ukuran_terkecil as ukuran_terkecil,
stok.ukuran_terbesar as ukuran_terbesar,
stok.foto as foto,
stok.qrcode as qrcode,
model.nama_model as nama_model,
merk.nama_merk as nama_merk

                  FROM stok INNER JOIN  model ON stok.id_model=model.id_model INNER JOIN merk ON model.id_merk=merk.id_merk") or die(mysqli_error());

              if(mysqli_num_rows($t)>0){
                while ($y=mysqli_fetch_array($t)) {
                      $mrk = implode("", DekripCBC($y['nama_merk']));
                      $mdl = implode("", DekripCBC($y['nama_model']));
                      $wr = implode("", DekripCBC($y['warna']));
                  $no++;

                  echo "<tr>
                        <td>$no</td>
                        <td>$mrk $mdl $wr<br><br>
                            <img src=view/foto_sepatu/$y[foto] width=140px>
                        </td>

                       <td></td>

                        <td>$y[stoksatu]<br> $y[stokdua]<br>$y[stoktiga]<br>$y[stokempat]<br>$y[stoklima]<br>$y[stokenam]<br>$y[stoktujuh]</td>

                      </tr>

            ";
                 }

              }

              else{
                echo "<tr>
                        <td colspan=5><center>Data Tidak Ada</center></td>
                      </tr>";
              }
[
![](https://i.stack.imgur.com/G8Blc.png)
] 
![](https://i.stack.imgur.com/IQ343.png)
z8dt9xmd

z8dt9xmd1#

所以你会:
在每个单独的子阵列上循环。
从中获取最小值和最大值 ukuran_terkecil 以及 ukuran_terbesar .
计算实际最大值 ukuran_terkecil +7(排除关闭)。
从中逐个迭代 ukuran_terkecilukuran_terkecil + 7.
如果 ukuran_terbesar 小于 $i (循环变量),打印 0 为了那些。
代码段:

<?php

foreach($data as $values){
    $min = intval($values['ukuran_terkecil']);
    $max = intval($values['ukuran_terbesar']);
    $new_max = $min + 7;
    echo "ID => ",$values['id'],"<br/>";
    echo "Size => ";
    for($i = $min; $i < $new_max; ++$i){
         if($i > $max) echo " 0";
         else echo " ",$i;  
    }    

    echo "<br/><hr>";
}

完整代码:(包括数组数据)

<?php 

$data = array (
    0 =>
    array (
        0 => '0C529E7D34AE7196',
        'id' => '0C529E7D34AE7196',
        1 => '0',
        'stoksatu' => '0',
        2 => '0',
        'stokdua' => '0',
        3 => '2',
        'stoktiga' => '2',
        4 => '2',
        'stokempat' => '2',
        5 => '2',
        'stoklima' => '2',
        6 => '1',
        'stokenam' => '1',
        7 => '2',
        'stoktujuh' => '2',
        8 => '2C2C243A1C7A',
        'warna' => '2C2C243A1C7A',
        9 => '37',
        'ukuran_terkecil' => '37',
        10 => '43',
        'ukuran_terbesar' => '43',
        11 => 'hugob026.png',
        'foto' => 'hugob026.png',
        12 => 'SpHu26Co.png',
        'qrcode' => 'SpHu26Co.png',
        13 => '3A340C6CF62780C7',
        'nama_model' => '3A340C6CF62780C7',
        14 => '0C52CCD7EDB3',
        'nama_merk' => '0C52CCD7EDB3',
    ),
    1 =>
    array (
        0 => '281E300870803B0E',
        'id' => '281E300870803B0E',
        1 => '2',
        'stoksatu' => '2',
        2 => '2',
        'stokdua' => '2',
        3 => '2',
        'stoktiga' => '2',
        4 => '1',
        'stokempat' => '1',
        5 => '0',
        'stoklima' => '0',
        6 => '0',
        'stokenam' => '0',
        7 => '0',
        'stoktujuh' => '0',
        8 => '3A0C5ADCC9',
        'warna' => '3A0C5ADCC9',
        9 => '24',
        'ukuran_terkecil' => '24',
        10 => '27',
        'ukuran_terbesar' => '27',
        11 => '3.PNG',
        'foto' => '3.PNG',
        12 => 'ArSaeeHi.png',
        'qrcode' => 'ArSaeeHi.png',
        13 => '0C70AE351850C0',
        'nama_model' => '0C70AE351850C0',
        14 => '281E5EC4FB9763',
        'nama_merk' => '281E5EC4FB9763',
    ),
    2 =>
    array (
        0 => '222C60825DE2FF870572',
        'id' => '222C60825DE2FF870572',
        1 => '1',
        'stoksatu' => '1',
        2 => '1',
        'stokdua' => '1',
        3 => '2',
        'stoktiga' => '2',
        4 => '1',
        'stokempat' => '1',
        5 => '2',
        'stoklima' => '2',
        6 => '0',
        'stokenam' => '0',
        7 => '0',
        'stoktujuh' => '0',
        8 => '3A0C5ADCC979F88965B6',
        'warna' => '3A0C5ADCC979F88965B6',
        9 => '40',
        'ukuran_terkecil' => '40',
        10 => '44',
        'ukuran_terbesar' => '44',
        11 => 'IMG-20190428-WA0018.jpg',
        'foto' => 'IMG-20190428-WA0018.jpg',
        12 => 'DaItyyHiPi.png',
        'qrcode' => 'DaItyyHiPi.png',
        13 => '38327AAC01',
        'nama_model' => '38327AAC01',
        14 => '222C2A262404',
        'nama_merk' => '222C2A262404',
    ),
);

foreach($data as $values){
    $min = intval($values['ukuran_terkecil']);
    $max = intval($values['ukuran_terbesar']);
    $new_max = $min + 7;
    echo "ID => ",$values['id'],"<br/>";
    echo "Size => ";
    for($i = $min; $i < $new_max; ++$i){
         if($i > $max) echo " 0";
         else echo " ",$i;  
    }    

    echo "<br/><hr>";
}

更新:
sql while循环可以如下所示逐行打印大小:

<?php

while ($y=mysqli_fetch_array($t)) {
    $mrk = implode("", DekripCBC($y['nama_merk']));
    $mdl = implode("", DekripCBC($y['nama_model']));
    $wr = implode("", DekripCBC($y['warna']));
    $no++;

    $sizes = [];

    $min = intval($y['ukuran_terkecil']);
    $max = intval($y['ukuran_terbesar']);
    $new_max = $min + 7;
    for($i = $min; $i < $new_max; ++$i){
         if($i > $max) $sizes[] = 0;
         else $sizes[] = $i;  
    }    

   echo "
        <tr>
            <td>$no</td>
            <td>$mrk $mdl $wr<br><br>
                <img src=view/foto_sepatu/$y[foto] width=140px>
            </td>
            <td>".implode("<br/>",$sizes)."</td>
            <td>$y[stoksatu]<br> $y[stokdua]<br>$y[stoktiga]<br>$y[stokempat]<br>$y[stoklima]<br>$y[stokenam]<br>$y[stoktujuh]</td>
        </tr>
        ";
 }

相关问题