Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:33:17+00:00 2026-05-24T17:33:17+00:00

I’m trying to convert my procedural code to oop. <?php $dbc = get_dbc(); $info

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:33:17+00:00Added an answer on May 24, 2026 at 5:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.