Welcome Guest, Not a member yet? Register   Sign In
20.03.19 Message: Object of class stdClass could not be converted to string
#1

(This post was last modified: 03-20-2019, 01:27 AM by Porto.)

Hello Guys, please i need a Light!

I've already read some cases identical as my issue here in the Forum, but i don't get STILL see what should i do.

The best answer was:
-------------------------------------------------------
eluser]Massaki[/eluser]

The variable $var is an object.

If you want to show an attribute from the object, then

Code:
echo $var->name_of_the_attribute
-------------------------------------------------------

But i still stuck.

I'm using this block of code from the documentation:
https://www.codeigniter.com/user_guide/d...esult-rows

This is my Controller/Method
_____________________________

Code:
        public function votecounting($page = 'vote_detail1')
        {
            if ( ! file_exists(APPPATH.'views/content/'.$page.'.php'))
                {
                       // Whoops, we don't have a page for that!
                       show_404();
                }
        
               $data['title'] = ucfirst($page); // Capitalize the first letter
            $data['totalrows'] = $this->Voting_counter_model->totalparticipants();

            $this->load->view('theme/header');
            $this->load->view('content/vote_detail1', $data);
            $this->load->view('theme/footer');
        }

This is my Model
_____________________________

Code:
    public function totalparticipants()
    {
        $query = $this->db->query("SELECT count(v_id) FROM ci_voting_counter WHERE v_column IN ('A', 'B', 'C', 'D', 'E')");

        $resultrows = $query->row();

        return $resultrows;
    }


This is my VIEW
_____________________________

Code:
  <div class="XXX">
       <span class="current">Votes<strong> <?php print_r($totalrows); ?></strong> </span>
   </div>
   
       <div class="block lrg">
           <div class="title">Vote results</div>
           <div class="info">The number who participated in the Vote yet <strong> <?php echo print_r($totalrows); ?> </strong> VOTER</div>

           <div class="info">The number who participated in the Vote yet <strong> <?php echo $totalrows; ?> </strong> VOTER</div>


If i try to show the variable this way:  <?php echo $totalrows; ?> i got the error message.

Code:
A PHP Error was encountered
Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: content/vote_detail1.php

Line Number: 24
Backtrace:

File: C:\xampp\htdocs\poll\application\views\content\vote_detail1.php
Line: 24

If i try to show the variable this way:  <?php echo $totalrows->v_id; ?> i got the error message.

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: stdClass::$v_id

Filename: content/vote_detail1.php

Line Number: 24

This is the result of my First print_r() <?php print_r($totalrows); ?>
_____________________________

Code:
Votes stdClass Object ( [count(v_id)] => 498 )

This is the result of my Second print_r() <?php print_r($totalrows); ?>
_____________________________

Code:
The number who participated in the Vote yet stdClass Object ( [count(v_id)] => 498 ) 1 VOTER


What should i do please? v_id is the table_field_id and i just want to show a single row as explained in the documentation.

i've already tried using row_array(), But i stuck the same way.

Thank you so much in advance!
Reply
#2

SEE:

CodeIgniter User's Guide - Generating Query Results
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(03-20-2019, 03:54 AM)InsiteFX Wrote: SEE:

CodeIgniter User's Guide - Generating Query Results

Hello InsiteFX,

I thank you to send me back to the documentation, but, unfortunatly this doens't helped me! This is the Standardt error that i get!

PHP Code:
A PHP Error was encountered
Severity
4096

Message
Object of class stdClass could not be converted to string

Filename
models/Voting_counter_model.php

Line Number
183 

I know that it would be such a ridiculous question, but i need to ask anyway because there is no ridiculous question in this world.

This is the first example from CI documentation - Result Arrays

Where should go where please?

The first line belongs to the CI Model file:

Code:
1° $query = $this->db->query("YOUR QUERY");

Where should go this "foreach" please? Does it work together with the line above in the same file "model file" or does he go to the view file?

Code:
foreach ($query->result() as $row)
{
       echo $row->title;
       echo $row->name;
       echo $row->body;
}

I will try to practice every example from the Database Reference page,
https://www.codeigniter.com/user_guide/d...index.html

because if you just are not programmer 25 hours a day, it comes a moment that you forget the simple things.

Thank you!
Reply
#4
Thumbs Up 

(03-20-2019, 04:19 AM)Porto Wrote:
(03-20-2019, 03:54 AM)InsiteFX Wrote: SEE:

CodeIgniter User's Guide - Generating Query Results

Hello InsiteFX,

I thank you to send me back to the documentation, but, unfortunatly this doens't helped me! This is the Standardt error that i get!

PHP Code:
A PHP Error was encountered
Severity
4096

Message
Object of class stdClass could not be converted to string

Filename
models/Voting_counter_model.php

Line Number
183 

I know that it would be such a ridiculous question, but i need to ask anyway because there is no ridiculous question in this world.

This is the first example from CI documentation - Result Arrays

Where should go where please?

The first line belongs to the CI Model file:

Code:
1° $query = $this->db->query("YOUR QUERY");

Where should go this "foreach" please? Does it work together with the line above in the same file "model file" or does he go to the view file?

Code:
foreach ($query->result() as $row)
{
       echo $row->title;
       echo $row->name;
       echo $row->body;
}

I will try to practice every example from the Database Reference page,
https://www.codeigniter.com/user_guide/d...index.html

because if you just are not programmer 25 hours a day, it comes a moment that you forget the simple things.

Thank you!

Issue Solved on  20.03.2019 13:59

The Problem was the lack of a index in the Query (CI Model File).

SELECT count(v_id) AS v_id FROM
Reply
#5

SOLVED ISSUE
Reply




Theme © iAndrew 2016 - Forum software by © MyBB