从MySQL设置数组然后将每个值设置为变量PHP

I'm trying to get 12 variables set depending on the data pulled from my database. At the moment I have it setting them into an array and then into a variable from there. However, for some reason the 12th value from my db isn't being entered into my array and then when I attempt an echo of each variable I get miss matched dates. Can anyone see where I'm going wrong here?

Example DB

date1 = 01/01/2018

date2 = 02/01/2018

date3 = 03/01/2018

date4 = 04/01/2018

date5 = 05/01/2018

date6 = 06/01/2018

date7 = 07/01/2018

date8 = 08/01/2018

date9 = 09/01/2018

date10 = 10/01/2018

date11 = 11/01/2018

date12 = 12/01/2018

Example Array

Array
(
    [0] =>; 01/01/2018
    [1] =>; 02/01/2018
    [2] =>; 03/01/2018
    [3] =>; 04/01/2018
    [4] =>; 05/01/2018
    [5] =>; 06/01/2018
    [6] =>; 07/01/2018
    [8] =>; 08/01/2018
    [9] =>; 09/01/2018
    [9] =>; 10/01/2018
    [10] =>; 11/01/2018
    [11] =>; 12/01/2018  //-----This isn't being added-----
)

Example Echo

$var1 = 01/01/2018 //-----Should be 01/01/2018-----

$var2 = 02/01/2018 //-----Should be 02/01/2018-----

$var3 = 01/01/2018 //-----Should be 03/01/2018-----

$var4 = 02/01/2018 //-----Should be 04/01/2018-----

$var5 = 03/01/2018 //-----Should be 05/01/2018-----

$var6 = 01/01/2018 //-----Should be 06/01/2018-----

$var7 = 02/01/2018 //-----Should be 07/01/2018-----

$var8 = 03/01/2018 //-----Should be 08/01/2018-----

$var9 = 04/01/2018 //-----Should be 09/01/2018-----

$var10 = 01/01/2018 //-----Should be 10/01/2018-----

$var11 = 02/01/2018 //-----Should be 11/01/2018-----

$var12 = 03/01/2018 //-----Should be 12/01/2018-----

<?php
    include 'db.php';

    if (isset($_GET['id'])) {

        $id = $_GET['id'];

        $sql = "SELECT * FROM system WHERE `id` = '$id' AND `status` = 'Not Set'";
        $result = $conn->query($sql);
        $row = $result->fetch_assoc();

        if ($result->num_rows > 0) {

            $arrayResults = array();
            $i = 0;

            while($row = $result->fetch_assoc()) {

                array_push($arrayResults, $row['date']);

                foreach($arrayResults as $value){

                    ${'var'.$i} = $value;
                    $i++;

                }

            }
            print_r($arrayResults);

            echo '<br><br>';

            echo $var1.'<br>';

            echo $var2.'<br>';

            echo $var3.'<br>';

            echo $var4.'<br>';

            echo $var5.'<br>';

            echo $var6.'<br>';

            echo $var7.'<br>';

            echo $var8.'<br>';

            echo $var9.'<br>';

            echo $var10.'<br>';

            echo $var11.'<br>';

            echo $var12.'<br>';

        }
    }
?>

UPDATE

The echo dates are now echoing in the correct order only issue now is it's still missing the first entry from the database so it's only creating 11 variables not 12. The array data prints all dates but the first, so the array entries are from [0] to [10] when it needs to be [0] to [11].

<?php
    include 'db.php';

    if (isset($_GET['id'])) {

        $id = $_GET['id'];

        $sql = "SELECT * FROM system WHERE `id` = '$id' AND `status` = 'No Set'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {

            $row = $result->fetch_assoc();

            $arrayResults = array();
            $i = 0;

            while($row = $result->fetch_assoc()) {

                array_push($arrayResults, $row['date']);
                ${'var'.$i} = $row['date'];
                $i++;

            }
            print_r($arrayResults);

            echo '<br><br>';

            echo $var0.'<br>';

            echo $var1.'<br>';

            echo $var2.'<br>';

            echo $var3.'<br>';

            echo $var4.'<br>';

            echo $var5.'<br>';

            echo $var6.'<br>';

            echo $var7.'<br>';

            echo $var8.'<br>';

            echo $var9.'<br>';

            echo $var10.'<br>';

            echo $var11.'<br>';


        }
    }
?>
查看全部
dongtan8122
dongtan8122
2018/06/04 14:55
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复