I'm pulling an array of objects from my database and iterating through them using PHP to display a table. Each result row of the table has the option of deleting that particular record from the database. Each row is also an object, and the object type has a method that will delete itself from the database.
The problem I'm having is thinking through how to get this to work in the view. Right now if you click the delete button, it sets a POST variable to "delete" and reloads the same page wherein a listener process detects the variable and initiates the delete via a hidden input that contains the records database id.
This is all well and good, but these are all objects. Shouldn't each object be able to access it's own methods? It seems the only barrier to this is the interplay between PHP script and HTML forms. My code is below. Ideally, when the delete button is pressed, that particular instance of the object teamleader can run its method teamleader->delete_team_leader().
<?php foreach($teamleaders as $teamleader) { ?>
<tr>
<td><?php echo $teamleader->first_name; ?></td>
<td><?php echo $teamleader->last_name; ?></td>
<td><img src="<?php echo '../img/' . $teamleader->photo;?>" alt="" class="img-fluid tiny-tl-img"></td>
<td><?php echo nl2br(substr($teamleader->bio, 0, 100));?>...</td>
<td><?php echo $teamleader->url_name;?></td>
<form action="index.php?a=edittl" method="post">
<td>
<input type="hidden" name="id" value="<?php echo $teamleader->id?>">
<input type="submit" class="btn btn-default" name="edit" value="Edit">
</td>
</form>
<form action="index.php" method="post">
<td>
<input type="hidden" value="<?php echo $teamleader->id?>" name="id">
<input type="submit" class="btn btn-danger" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete this team leader?')">
</td>
</form>
</tr>
<?php } ?>