Im trying to imprive my skills and move away from procedural style coding, however the switch is not coming easy and I am very much new to the OOP concept so please keep this in mind when reading / answering.
I have the following method inside a class called Jobs{}
The task of the method is pretty simple query the database for all the different job categories in a table and return it.
The following is an extract from the code inside my class
function dispCategories(){
$sql = "SELECT DISTINCT category FROM jobs";
$stmnt = $db->prepare($sql);
$stmnt->execute();
return $stmnt->fetchAll();
}
Now inside my view or html page I do the following
$obj = new Jobs();
$categories = $obj->dispCategories()
?>
<!--DISPLAY CATEGORIES -->
<form id="getJobByCategory" method="post" action="">
<select name="selectedCategory">
<?php
foreach ($categories as $category){
?>
<option value="<?php echo $category['category'] ?>">
<?php echo $category['category'] ?>
</option>
<?php
}
?>
My Question
When you look at my code where I initiate the object Jobs{} and call the method categories $categories = $obj->dispCategories()
I use a foreach loop combined with some html & php. Is this an acceptable way of doing things in the OOP realm?
- Should I rather create a seperate method like
getCategories()where I loop over the results returned by thedispCategories()method? - Alternatively should I just process the data returned by the
dispCategories()method inside the actualdispCategories()method...
Hope this make sense. Just looking for some best practices and guidance here, however please keep in mind im a rookie.