duanlaiyin2356 2017-05-25 10:10
浏览 31
已采纳

如何使用在类中获取的数组创建PHP循环?

I'm trying to create a loop in a page that populates a form with several options from a database. In this case the form is intended to create an ASSEMBLY and will require the selection of already existent PARTS in the database.

The approach I'm trying to achieve is the following:

A. I created a class containing the following function in a file called classes.php:

<?php 

$depth = "../";
require_once("connection.php");

////////////////////////////////////////////

class Parts
{
    // SELECTS ALL PARTS FROM DATABASE
    public function getPartInfo() {
        global $con;
        $partinfo = mysqli_query($con,"SELECT * FROM parts ORDER BY name");
        $row = mysqli_fetch_assoc($partinfo);
        return $row;
    }

} // end of Parts

?>

B. In another file (assemblies.php) I'm trying to catch the value of all fields in the array so I can create a loop listing all rows so the user can select the parts required for the assembly. I'm trying the following code with no success:

    <?php
    session_start();

    include "php_includes/classes.php";

    $parts = new Parts();
    $parts_detail = $parts->getPartInfo();

    ?>

    <div class="row">
       <div class="form-group col-md-12">
          <table width="100%" id="partsTable">
             <?php 
                foreach($parts_detail as $row) {
                   echo '<tr>
                         <td><input type="checkbox" name="parts[]" value="'.$row["id"].'" /></td>
                         <td><img src="https://lux365.s3.amazonaws.com/landing-pages/easilite/v1/img/easilite-logo.jpg" alt="" /></td>
                         <td>'.$row["name"].'</td>
                         <td><input type="number" name="partQuantity[]" min="0" /></td>
                         </tr>';
                 }
              ?>
           </table>
        </div>
    </div>

Where $row["name"] is trying to get the field NAME from the parts table and $row["id"] tries to call the field ID from the parts table.

But when I try this, it results in an Illegal string offset error, for both, name and id.

Any help is appreciated since my OOP skills are extremely limited.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dpkpaxhzffixp8426 2017-05-25 10:13
    关注

    getPartInfo() just returns the first row returned by the query. Then your for ($parts_detail as $row) loop is looping over the columns in that row, not all the rows. So $row is a string containing a column value, and $row['name'] gets an error because $row isn't an associative array.

    It should have a loop and return all the rows.

    public function getPartInfo() {
        global $con;
        $partinfo = mysqli_query($con,"SELECT * FROM parts ORDER BY name");
        $result = array();
        while ($row = mysqli_fetch_assoc($partinfo)) {
            $result[] = $row;
        }
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我