dongtan8122 2018-06-04 14:55
浏览 45
已采纳

从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>';


        }
    }
?>
  • 写回答

2条回答 默认 最新

  • duanboshe0001 2018-06-04 15:52
    关注

    You should put the "foreach" outside of the "while"

           while($row = $result->fetch_assoc()) {
    
                array_push($arrayResults, $row['date']);
    
           }
    
           foreach($arrayResults as $value){
    
                    ${'var'.$i} = $value;
                    $i++;
    
           }
    

    Please note that this make loop twice over the results. You could instantiate the "$varx" directly inside the while. And if you have no other use for $arrayResults you can skip it.

    while($row = $result->fetch_assoc()) {
    
            array_push($arrayResults, $row['date']);//this line is not compulsory. Depends if you need $arrayResults later.
            ${'var'.$i} = $row['date'];
            $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置