0

I have a table in a sample Database:

  • id: 1
  • smallest size: 23
  • biggest size: 27

  • id: 2
  • smallest size: 37
  • largest size: 43.

I want to display the data like this:

  • id: 1
  • size: 23 24 25 26 27 0 0

  • id: 2
  • size: 37 38 39 40 41 42 43

More specifically I want only 7 size units to be appeared, if there are less than 7 then it will display zeros(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

But output is:

  • Id 1 : 23 24 25 26 27 0 0
  • Id 2 : 23 24 25 26 27 37 38

Sample Data:

$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>";
                  }

[enter image description here] enter image description here

1 Answers1

1

So you would:

  • Loop over each individual subarray.
  • Get the min and max values from ukuran_terkecil and ukuran_terbesar.
  • Calculate the actual max that can be which is ukuran_terkecil + 7(exclusive off).
  • Iterate one by one from ukuran_terkecil to ukuran_terkecil + 7.
  • If ukuran_terbesar becomes less than $i(your loop variable), print 0 for those.

Snippet:

<?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>";
}

Full Code:(includes array data)

<?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>";
}

Update:

Your SQL while loop can look like below to print sizes line by line:

<?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>
        ";
 }
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • @EgaSuhandianto Yes. – nice_dev Jul 04 '20 at 11:28
  • well this works. but I have a variable $ t = query select. and $ y = mysqli fetch array ($ t). I want to display it in the " table ". where should i save the script ?, and if i use $ y the data size doesn't appear. but if $ t data appears, my script saves it in mysqli fetch array – Ega Suhandianto Jul 05 '20 at 07:13
  • @EgaSuhandianto Can you edit your post and add that code snippet so that I can have a look? – nice_dev Jul 05 '20 at 07:52
  • I want to display it in a table – Ega Suhandianto Jul 05 '20 at 08:14
  • @EgaSuhandianto Does your table have 7 columns for size or just 2 columns? I think I can give you some test code but you will have to change the key names accordingly, as those key names seems to be from some different language. – nice_dev Jul 05 '20 at 08:36
  • size has 7 sizes, the program code that you provided was successful in the photo, but I didn't include it in the original code, I want to enter data in a table. but I'm confused where to save it. Do you want to see the code that you also provided in the program? – Ega Suhandianto Jul 05 '20 at 08:56
  • @EgaSuhandianto Like you showed in the image, you want sizes to come line by line? – nice_dev Jul 05 '20 at 09:17
  • @EgaSuhandianto Is it in the `` tags you have left empty for size insertion? – nice_dev Jul 05 '20 at 09:29
  • @EgaSuhandianto I have added an updated. Hope it works for you. – nice_dev Jul 05 '20 at 11:15
  • 1
    it worked, thank you very much. sorry to bother you. – Ega Suhandianto Jul 05 '20 at 13:01
  • hi ... can you help me ?, I have another problem, I want to ask via posting questions. but my account is closed can not ask. – Ega Suhandianto Jul 06 '20 at 08:49
  • @EgaSuhandianto Why is your account closed? Make a new account maybe. – nice_dev Jul 06 '20 at 11:16
  • Can you check : https://stackoverflow.com/questions/62771686/how-to-use-sessions-and-arrays-in-a-database-query-to-store-and-display-data – Ega Suhandianto Jul 07 '20 at 09:06