dongqian5569 2015-06-15 17:45
浏览 14
已采纳

PHP使用函数中的数组填充html select

OK, so, this may seem like a dumb question. I've looked through the other threads with this thing and I'm not finding QUITE what I'm looking for.

I get this error:

Warning: Invalid argument supplied for foreach() in entry.php on line 118

line 118 is the foreach that I'm using here:

<select id="Location" name="Location" class="text">
<option selected="selected"> - Choose Location - </option>
<?php 
    $locations = getLocationList();//this returns an array from separate function
    foreach($locations as $location) {//<-Line 118
        echo "<option value=". $location['locationID'] .">".$location['locationName']."</option> ";
    }
?>
</select>

It's not populating, it's only throwing the error. Thoughts?

OK EDIT This is how I'm pulling the data in a separate function:

function getLocationList()
{
    $mydb = new myDBC();//<-this calls my secure connection class
    $table = "LocationTable";
    $sql = "SELECT `locationID`, `locationName` FROM " .$table;
    $rez = $mydb->runQuery($sql);//<-this connects runs my query
    if(isset($rez))
    {
        $newRow = mysqli_fetch_array($rez);//<-is this not returning an array?
        return $newRow;
    }
}
  • 写回答

2条回答 默认 最新

  • dongtu4028 2015-06-15 17:52
    关注

    When you have errors like Warning: Invalid argument supplied for foreach() it literally means that whatever variable you are using in your foreach() is not an array nor an object. Verify that $locations is either an array or an object. One you fix that, it would work.

    Ok, after checking your update, try making these changes:

    Change this:

    if (isset($rez)) {
        $newRow = mysqli_fetch_array($rez); //<-is this not returning an array?
        return $newRow;
    }
    

    To this:

    if (isset($rez)) {
        return $rez->fetch_assoc();
    }
    

    OK, seems like your class/library is not a full library and because of this you have to add your own code to enhance it. In order to make this work how you would like it to, add this function somewhere in your DBClass.php

    function convert_result_to_arr($result) {
        if ($result) {
            while ($row = $result->fetch_assoc()) {
                $arr[] = $row;
            }
        }
        return $arr;
    }
    

    This will convert your results into an appropriate muilti dimensional array so that you can iterate and get your info.

    Now you can use it like this:

    $rez = $mydb->runQuery($sql); //<-this connects runs my query
    $rez_converted = $mydb->convert_result_to_arr($rez);
    if(isset($rez_converted )){
            return $rez_converted ;
    }
    

    Now you can iterate like you originally wanted to:

    foreach($locations as $location) {//<-Line 118
            echo "<option value=". $location['locationID'] .">".$location['locationName']."</option> ";
        }
    

    I would also highly recommend that you transition into an MVC framework. I recommend Codeigniter to start. You wouldnt need to worry about all this along with other problems that come along.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?