I’m trying to grasp OOP and I decided to build a site that accesses a sql database.
It’s been working out great so far I have several tables, but I’ve run into a snag
I access the database and create an object array with
$Dog_array = $sth->fetchAll(PDO::FETCH_CLASS, 'Dog');
Simplified but my class looks like this.
class Dog {
private $name;
function get_name {
return $this->name;
}
list that build my table
function edit_table($Dog) {
echo "<table><tr><form action='update.php' method ='post'>";
echo "<input type='hidden' name='id' value='" . $id ."'>";
echo "<td>". $Dog->get_id() ."</td>";
?>
<td><input type='text' name='rname' value="<?php echo $Dog->get_rname(); ?>"></td>
<td><input type='text' name='cname' value="<?php echo $Dog->get_cname(); ?>"></td>
<td><input type='text' name='dob' value="<?php echo $Dog->get_dob(); ?>"></td>
<td><input type='radio' name='gender' value='male' <?PHP if($Dog->get_gender() == 'male'){ echo "checked=\"checked\""; } ?> /> Male
<input type='radio' name='gender' value='female' <?PHP if($Dog->get_gender() == 'female'){ echo "checked=\"checked\""; } ?> />Female</td>
<td><input type='text' name='sire' value="<?PHP echo "builddropdown" ?> "></td>
<td><input type='text' name='dam' value="<?PHP drop_menu() ?>"></td>
<?php
if ($Dog->get_available()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
echo "<td><input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/></td>";
if ($Dog->get_display()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
}
echo "<td><input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/></td></tr></table>";
?>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<?php
}
?>
<input type='submit' value='update'/></form>
<FORM METHOD="LINK" ACTION="index.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>
<?php
}
?>
My drop down
function drop_menu() {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?></option>
<?php } ?>
</select>
</form>
<? }
My drop down menu creates a list of all of the dogs.
I get the error Invalid argument supplied for foreach
I get why I get the error I just don’t know how to clean this up so I don’t have to pass my Object array into my edit_table function so I can pass it into drop down function
.
I thought about creating a child class but the menu is built from an array of the objects not one object so I don’t know how to make this work either. New to forum posting, so feel free to recommend some corrections in how I ask for help.
It’s a problem with the arrage $Dog_array scope.
You need to pass the $Dog_array to the drop_menu function.
And whenever you need to call the function, pass the array normally.
Also you can pass by reference
In this case any manipulation to the array inside the function will impact the original array directly.