Search code examples
phphtmloopprocedural

PHP Procedural To OOP


I'm trying to convert my procedural code to oop.

 <?php
 $dbc = get_dbc();
 $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
 while ($info_row = mysqli_fetch_array($info))
 {
      $info_id = $info_row['info_id'];
      $info_title = $info_row['info_title'];
 ?>
 <div style="width: 100%;">
      <div style="float: left;">
           <?php echo $info_id; ?>
      </div>
      <div style="float: left;">
           <?php echo $info_title; ?>
      </div>
      <div style="clear: both;"></div>
 </div>
 <?php } ?>

My incomplete attempt at classes/objects without the HTML styling:

 <?php
 class InfoTest {

      private $info_id;
      private $info_title;

      public function __construct() {
           $dbc = get_dbc();
           $info = $dbc->query ("SELECT info_id, info_title FROM text");
           if ($dbc->error) {
                printf("Error: %s\n", $dbc->error);
           }       
           while ($info_row = $info->fetch_array())
           {
                $info_id = $info_row['info_id'];
                $info_title = $info_row['info_title'];  
           }
           $info->free();
           $this->info_id = $info_id;
           $this->info_title = $info_title; 
      }


      public function setInfoID() {
           $this->info_id = $info_id;
      }

      public function getInfoID() { 
           return $this->info_id;
      }

      public function setInfoTitle() {
           $this->info_title = $info_title;
      }

      public function getInfoTitle() {
           return $this->info_title;
      }

      public function __destruct() {    
      }

 }

 ?>
 <?php
 $display = new InfoTest();
 echo $display->getInfoID();
 echo $display->getInfoTitle();
 ?>

My procedural code prints out: 1 One 2 Two.

My oop code prints out: 2 Two

From my understanding the oop prints out that way because $info_id and $info_title aren't arrays, and only print out the last stored information.

So, if I change:

$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];

To:

$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];

And print the arrays, it displays all the information I want, but how to display it in non-array form?

Is what I'm doing so far correct or am I approaching this wrong?


Solution

  • You're doing it wrong. In your procedural example you're iterating over the data a row at a time; in your OO example, if you treat them as arrays and then print them, you're going through the data a column at a time instead. Rather than separating the data into separate ids and titles, I would treat them as a bundle (i.e. similar to how you did it in the procedural version) - an id goes with a title, not other ids, right?

    So, for example, you might have a member variable

    private $texts = array();
    

    and then in your constructor, do:

    while ($info_row = $info->fetch_array()) {
        $text = array(
            'id' => $info_row['info_id'],
            'title' => $info_row['info_title']
        );
        $this->texts[] = $text;
    }
    

    and then provide a method to get at this array of arrays:

    public function getTexts() {
        return $this->texts;
    }
    

    Finally, you could iterate over it very similarly to how you did in the procedural example:

    <?php
    $display = new InfoTest();
    foreach ($display->getTexts() as $text) {
        ?>
        <!-- html goes here -->
        <?php echo $text['info_id']; ?>
        <!-- more html -->
        <?php echo $text['info_title']; ?>
        <!-- other html -->
        <?
    }
    ?>
    

    Stepping back - you could ask if all this is really necessary. There's nothing inherently wrong with procedural PHP - if it does what you need it to do and does it clearly, you might be better off favoring simple over complex here.