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