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

  • SEARCH
  • Home
  • 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 7071343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:40:35+00:00 2026-05-28T05:40:35+00:00

Class Account extends CI_Model { private $tbl_rest = array(); private $tbl_fields = array(‘bs_id’, ‘bs_name’,

  • 0
Class Account extends CI_Model {
    private $tbl_rest   = array();
    private $tbl_fields = array('bs_id', 'bs_name', 'bs_type', 'bs_sub');

    function get_data($dataid){
        $this->db->select( '*' );
        $this->db->from( $this->tbl_name );
        $this->db->where( $this->tbl_key, $id );

        $query  = $this->db->get(); 
        if( $query->num_rows() > 0 ) 
        {
            foreach ($query->result() as $row)
            {
                $this->tbl_rest[] .= '<li id="'.$row->bs_id.'">'.$row->bs_name.'</li>';
            }

            echo( json_encode( array( 'tdata' => $this->tbl_rest ) ) );
        } else {
            echo( false );
        }
    }
}

When I change $query->result() like this

'<li id="'.$row->$this->tbl_fields[0].'">'.$row->$this->tbl_fields[1].'</li>';

I starting getting error “Object of class could not be converted to string”
My Question is:

  1. Is possible to convert an array string to object?

  2. And how to make $row->$this->tbl_fields[0] happen, so I do not always have to write the name of the field.

  • 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-28T05:40:36+00:00Added an answer on May 28, 2026 at 5:40 am

    This is a basic order of operations issue. PHP doesn’t know if you want some dynamic $this property of the row, or the $this->tbl_fields[0] property of the row.

    Try this:

    <?php
     $this->tbl_rest[] .= '<li id="'.$row->{$this->tbl_fields[0]}.'">'.$row->{$this->tbl_fields[1]}.'</li>';
    

    Additionally, I would recommend defining the model’s $tbl_fields property as either static or constant, as it doesn’t change at run-time or for specific instances of the class:

    <?php
    Class Account extends CI_Model {
        private static $tbl_fields = array('bs_id', 'bs_name', 'bs_type', 'bs_sub');
        // ...
    
        $this->tbl_rest[] .= '<li id="'.$row->{self::$tbl_fields[0]}.'">'.$row->{self::$tbl_fields[1]}.'</li>';
    
        // ...
    }
    

    Furthermore, hard-coding array references to specific column names is somewhat counter-productive, unless you know that [0] will always be the ID, and [1] will always be the name (in which case, you might as well set a static prefix for the column names and use that instead of the $tbl_fields array).

    Better yet, because all you’re doing here is a key/value pair (I assume you want to re-use this pattern for other API methods…), you could simply select the fields the way you want to use them. Here’s what I would recommend:

    <?php
    Class Account extends CI_Model {
        private $tbl_rest   = array();
        private static $col_prefix = 'bs_';
    
        function get_data($dataid){
            $this->db->select(array(
                self::$col_prefix.'id AS key',
                self::$col_prefix.'name AS value'
            ));
            $this->db->from( $this->tbl_name );
            $this->db->where( $this->tbl_key, $id );
    
            $query  = $this->db->get(); 
            if( $query->num_rows() > 0 ) 
            {
                foreach ($query->result() as $row)
                {
                    $this->tbl_rest[] .= '<li id="'.$row->key.'">'.$row->value.'</li>';
                }
    
                echo( json_encode( array( 'tdata' => $this->tbl_rest ) ) );
            } else {
                echo( false );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following controller file: <?php class Test extends CI_Controller { function __construct()
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class Lthread extends JPanel{ private
Controller class PageController extends Zend_Controller_Action { public function editAction() { $this->view->attach('name' => 'crazor'); echo
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
public class Account { public string Username { get { return Username; } set
Say I have this class: public class Account { public int AccountID { get;
I have the following class: public class Account { public int AccountID { get;
I have a View class (OrderView.aspx) which shows the details of an order (Account
I have some C# class libraries, that were designed without taking into account things
With include('basemodel.php'); I get Fatal error: Class 'BaseModel' not found in C:\xampp\htdocs\allsides\account\model.php on line

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.