PHP OOP How to Handle Data Returned by DB Query

I am trying to implement my skills and give up procedural coding, however the transition is not easy and I am very new to the OOP concept, so keep that in mind when reading / answering.

I have the following method inside a class named Jobs{}

The goal of the method is quite simple to query the database for all the different job categories in the table and return it.

Below is the code snippet inside my class

   function dispCategories(){
     $sql = "SELECT DISTINCT category FROM jobs";
     $stmnt = $db->prepare($sql);
     $stmnt->execute();
     return $stmnt->fetchAll();
 }

      

Now in my view page or html 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 start object Jobs{}

and call method categories$categories = $obj->dispCategories()

I am using a loop foreach

combined with some html and php. Is this an acceptable way of doing OOP business?

  • Should I create a separate method, like getCategories()

    where I view the results returned by the method dispCategories()

    ?
  • Alternatively, you just need to handle the data returned by the method dispCategories()

    inside the actual method dispCategories()

    ...

Hope this makes sense. Just look for recommendations and recommendations, but please keep the rookie in mind.

+3


source to share


1 answer


Should I create a separate method like getCategories () where I loop through the results returned by the dispCategories () method?

You are definitely on the right track with what you have now. Since your method dispCategories()

only returns one column, it makes sense that it will return a one-dimensional array, a simple list of values, rather than a 2D structure that preserves the characteristics of the SQL query that generated it. I would probably change the method to get all the strings and then flatten them into a single array, which you then return. This makes it unnecessary to create another method just to loop through the categories returned dispCategories()

.

Assuming PHP 5.5+ you can easily do this with array_column()

:

function dispCategories(){
     $sql = "SELECT DISTINCT category FROM jobs";
     $stmnt = $db->prepare($sql);
     $stmnt->execute();
     // Return a simple 1D array of the category only
     return array_column('category', $stmnt->fetchAll());
 }

      

Then it's simple foreach

to list them without having to access the array keys.



 foreach ($categories as $category){
 ?>
        <option value="<?php echo $category; ?>">
            <?php echo $category; ?>
        </option>
 <?php
 }

      

Remember that you may need to call htmlspecialchars()

by value $category

if it might contain characters that require HTML entity encoding.

Alternatively, you just need to handle the data returned by the dispCategories () method inside the dispCategories () method ...

No, I don't think you should do this. As you learn OOP, familiarize yourself with the separation of concerns concept and the MVC (model-view-controller) pattern. If you were to create a method in your class Jobs

that created this HTML block, it would then do the work that your view code more correctly does. The class Jobs

should be responsible for fetching elements from the database, providing information about them, and preparing them for the view or template, but the HTML or HTML view should be responsible for organizing the list of categories from the class Jobs

into markup suitable for you to display.

+2


source







All Articles