猿问

for函数怎么做是根据id,有数据库中的数据

我在示例数据库中有一个表:

  • 编号: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

  • 编号 2:23 24 25 26 27 37 38


莫回无
浏览 108回答 1
1回答

翻翻过去那场雪

所以你会:循环每个单独的子数组。ukuran_terkecil从和获取最小值和最大值ukuran_terbesar。计算实际最大值为ukuran_terkecil+ 7(不包括关闭)。ukuran_terkecil从到+ 7逐一迭代ukuran_terkecil。如果ukuran_terbesar变得小于$i(您的循环变量),则打印0这些值。片段:<?phpforeach($data as $values){&nbsp; &nbsp; $min = intval($values['ukuran_terkecil']);&nbsp; &nbsp; $max = intval($values['ukuran_terbesar']);&nbsp; &nbsp; $new_max = $min + 7;&nbsp; &nbsp; echo "ID => ",$values['id'],"<br/>";&nbsp; &nbsp; echo "Size => ";&nbsp; &nbsp; for($i = $min; $i < $new_max; ++$i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($i > $max) echo " 0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else echo " ",$i;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<br/><hr>";}完整代码:(包括数组数据)<?php&nbsp;$data = array (&nbsp; &nbsp; 0 =>&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; &nbsp; 0 => '0C529E7D34AE7196',&nbsp; &nbsp; &nbsp; &nbsp; 'id' => '0C529E7D34AE7196',&nbsp; &nbsp; &nbsp; &nbsp; 1 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stoksatu' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 2 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stokdua' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 3 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktiga' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 4 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stokempat' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 5 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoklima' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 6 => '1',&nbsp; &nbsp; &nbsp; &nbsp; 'stokenam' => '1',&nbsp; &nbsp; &nbsp; &nbsp; 7 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktujuh' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 8 => '2C2C243A1C7A',&nbsp; &nbsp; &nbsp; &nbsp; 'warna' => '2C2C243A1C7A',&nbsp; &nbsp; &nbsp; &nbsp; 9 => '37',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terkecil' => '37',&nbsp; &nbsp; &nbsp; &nbsp; 10 => '43',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terbesar' => '43',&nbsp; &nbsp; &nbsp; &nbsp; 11 => 'hugob026.png',&nbsp; &nbsp; &nbsp; &nbsp; 'foto' => 'hugob026.png',&nbsp; &nbsp; &nbsp; &nbsp; 12 => 'SpHu26Co.png',&nbsp; &nbsp; &nbsp; &nbsp; 'qrcode' => 'SpHu26Co.png',&nbsp; &nbsp; &nbsp; &nbsp; 13 => '3A340C6CF62780C7',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_model' => '3A340C6CF62780C7',&nbsp; &nbsp; &nbsp; &nbsp; 14 => '0C52CCD7EDB3',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_merk' => '0C52CCD7EDB3',&nbsp; &nbsp; ),&nbsp; &nbsp; 1 =>&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; &nbsp; 0 => '281E300870803B0E',&nbsp; &nbsp; &nbsp; &nbsp; 'id' => '281E300870803B0E',&nbsp; &nbsp; &nbsp; &nbsp; 1 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoksatu' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 2 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stokdua' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 3 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktiga' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 4 => '1',&nbsp; &nbsp; &nbsp; &nbsp; 'stokempat' => '1',&nbsp; &nbsp; &nbsp; &nbsp; 5 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stoklima' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 6 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stokenam' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 7 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktujuh' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 8 => '3A0C5ADCC9',&nbsp; &nbsp; &nbsp; &nbsp; 'warna' => '3A0C5ADCC9',&nbsp; &nbsp; &nbsp; &nbsp; 9 => '24',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terkecil' => '24',&nbsp; &nbsp; &nbsp; &nbsp; 10 => '27',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terbesar' => '27',&nbsp; &nbsp; &nbsp; &nbsp; 11 => '3.PNG',&nbsp; &nbsp; &nbsp; &nbsp; 'foto' => '3.PNG',&nbsp; &nbsp; &nbsp; &nbsp; 12 => 'ArSaeeHi.png',&nbsp; &nbsp; &nbsp; &nbsp; 'qrcode' => 'ArSaeeHi.png',&nbsp; &nbsp; &nbsp; &nbsp; 13 => '0C70AE351850C0',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_model' => '0C70AE351850C0',&nbsp; &nbsp; &nbsp; &nbsp; 14 => '281E5EC4FB9763',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_merk' => '281E5EC4FB9763',&nbsp; &nbsp; ),&nbsp; &nbsp; 2 =>&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; &nbsp; 0 => '222C60825DE2FF870572',&nbsp; &nbsp; &nbsp; &nbsp; 'id' => '222C60825DE2FF870572',&nbsp; &nbsp; &nbsp; &nbsp; 1 => '1',&nbsp; &nbsp; &nbsp; &nbsp; 'stoksatu' => '1',&nbsp; &nbsp; &nbsp; &nbsp; 2 => '1',&nbsp; &nbsp; &nbsp; &nbsp; 'stokdua' => '1',&nbsp; &nbsp; &nbsp; &nbsp; 3 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktiga' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 4 => '1',&nbsp; &nbsp; &nbsp; &nbsp; 'stokempat' => '1',&nbsp; &nbsp; &nbsp; &nbsp; 5 => '2',&nbsp; &nbsp; &nbsp; &nbsp; 'stoklima' => '2',&nbsp; &nbsp; &nbsp; &nbsp; 6 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stokenam' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 7 => '0',&nbsp; &nbsp; &nbsp; &nbsp; 'stoktujuh' => '0',&nbsp; &nbsp; &nbsp; &nbsp; 8 => '3A0C5ADCC979F88965B6',&nbsp; &nbsp; &nbsp; &nbsp; 'warna' => '3A0C5ADCC979F88965B6',&nbsp; &nbsp; &nbsp; &nbsp; 9 => '40',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terkecil' => '40',&nbsp; &nbsp; &nbsp; &nbsp; 10 => '44',&nbsp; &nbsp; &nbsp; &nbsp; 'ukuran_terbesar' => '44',&nbsp; &nbsp; &nbsp; &nbsp; 11 => 'IMG-20190428-WA0018.jpg',&nbsp; &nbsp; &nbsp; &nbsp; 'foto' => 'IMG-20190428-WA0018.jpg',&nbsp; &nbsp; &nbsp; &nbsp; 12 => 'DaItyyHiPi.png',&nbsp; &nbsp; &nbsp; &nbsp; 'qrcode' => 'DaItyyHiPi.png',&nbsp; &nbsp; &nbsp; &nbsp; 13 => '38327AAC01',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_model' => '38327AAC01',&nbsp; &nbsp; &nbsp; &nbsp; 14 => '222C2A262404',&nbsp; &nbsp; &nbsp; &nbsp; 'nama_merk' => '222C2A262404',&nbsp; &nbsp; ),);foreach($data as $values){&nbsp; &nbsp; $min = intval($values['ukuran_terkecil']);&nbsp; &nbsp; $max = intval($values['ukuran_terbesar']);&nbsp; &nbsp; $new_max = $min + 7;&nbsp; &nbsp; echo "ID => ",$values['id'],"<br/>";&nbsp; &nbsp; echo "Size => ";&nbsp; &nbsp; for($i = $min; $i < $new_max; ++$i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($i > $max) echo " 0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else echo " ",$i;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<br/><hr>";}更新:您的 SQL while 循环如下所示,逐行打印大小:<?phpwhile ($y=mysqli_fetch_array($t)) {&nbsp; &nbsp; $mrk = implode("", DekripCBC($y['nama_merk']));&nbsp; &nbsp; $mdl = implode("", DekripCBC($y['nama_model']));&nbsp; &nbsp; $wr = implode("", DekripCBC($y['warna']));&nbsp; &nbsp; $no++;&nbsp; &nbsp; $sizes = [];&nbsp; &nbsp; $min = intval($y['ukuran_terkecil']);&nbsp; &nbsp; $max = intval($y['ukuran_terbesar']);&nbsp; &nbsp; $new_max = $min + 7;&nbsp; &nbsp; for($i = $min; $i < $new_max; ++$i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($i > $max) $sizes[] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else $sizes[] = $i;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;echo "&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$no</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$mrk $mdl $wr<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src=view/foto_sepatu/$y[foto] width=140px>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".implode("<br/>",$sizes)."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$y[stoksatu]<br> $y[stokdua]<br>$y[stoktiga]<br>$y[stokempat]<br>$y[stoklima]<br>$y[stokenam]<br>$y[stoktujuh]</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; ";&nbsp;}
随时随地看视频慕课网APP
我要回答