Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP. Building a drop down menu . Passing around object array is sloppy how do I simplify this
    primarykey
    data
    text
    <p>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</p> <p>I access the database and create an object array with</p> <pre><code>$Dog_array = $sth-&gt;fetchAll(PDO::FETCH_CLASS, 'Dog'); </code></pre> <p>Simplified but my class looks like this.</p> <pre><code>class Dog { private $name; function get_name { return $this-&gt;name; } </code></pre> <p>list that build my table</p> <pre><code>function edit_table($Dog) { echo "&lt;table&gt;&lt;tr&gt;&lt;form action='update.php' method ='post'&gt;"; echo "&lt;input type='hidden' name='id' value='" . $id ."'&gt;"; echo "&lt;td&gt;". $Dog-&gt;get_id() ."&lt;/td&gt;"; ?&gt; &lt;td&gt;&lt;input type='text' name='rname' value="&lt;?php echo $Dog-&gt;get_rname(); ?&gt;"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='cname' value="&lt;?php echo $Dog-&gt;get_cname(); ?&gt;"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='dob' value="&lt;?php echo $Dog-&gt;get_dob(); ?&gt;"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='radio' name='gender' value='male' &lt;?PHP if($Dog-&gt;get_gender() == 'male'){ echo "checked=\"checked\""; } ?&gt; /&gt; Male &lt;input type='radio' name='gender' value='female' &lt;?PHP if($Dog-&gt;get_gender() == 'female'){ echo "checked=\"checked\""; } ?&gt; /&gt;Female&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='sire' value="&lt;?PHP echo "builddropdown" ?&gt; "&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='dam' value="&lt;?PHP drop_menu() ?&gt;"&gt;&lt;/td&gt; &lt;?php if ($Dog-&gt;get_available()) { $checked = "checked=\"checked\""; } else { $checked = NULL; echo "&lt;td&gt;&lt;input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/&gt;&lt;/td&gt;"; if ($Dog-&gt;get_display()) { $checked = "checked=\"checked\""; } else { $checked = NULL; } echo "&lt;td&gt;&lt;input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;"; ?&gt; &lt;input type='submit' value='change image'/&gt;&lt;/form&gt; &lt;FORM METHOD="LINK" ACTION="upload.php"&gt; &lt;INPUT TYPE="submit" VALUE="Change Image Thumb"&gt; &lt;/FORM&gt; &lt;input type='submit' value='change image'/&gt;&lt;/form&gt; &lt;FORM METHOD="LINK" ACTION="upload.php"&gt; &lt;INPUT TYPE="submit" VALUE="Change Image Thumb"&gt; &lt;/FORM&gt; &lt;?php } ?&gt; &lt;input type='submit' value='update'/&gt;&lt;/form&gt; &lt;FORM METHOD="LINK" ACTION="index.php"&gt; &lt;INPUT TYPE="submit" VALUE="Back"&gt; &lt;/FORM&gt; &lt;?php } ?&gt; </code></pre> <p>My drop down</p> <pre><code>function drop_menu() { ?&gt; &lt;form name ="dropdown"&gt; &lt;select ="alldogs"&gt; &lt;?php foreach ($Dog_array as $Dogs) { ?&gt; &lt;option value="&lt;?php echo $Dogs-&gt;get_id() . "\"&gt;" . $Dogs-&gt;get_rname(); ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/form&gt; &lt;? } </code></pre> <p>My drop down menu creates a list of all of the dogs.</p> <p>I get the error Invalid argument supplied for foreach</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload