Solved

Access a STD Object in PHP

Posted on 2010-08-12
24
1,362 Views
Last Modified: 2012-05-10
This is basically from a row output on a database using CodeIgniter.
$total = $this->ticketquery->countArtistTotal();
   
I have used print_r to tell me the structure of the object:
stdClass Object ( [COUNT(artevtid)] => 13 )

I tried several ways of accessing it like an array [] and an object -> but can't find any luck.
0
Comment
Question by:namsu55
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
  • 2
  • +1
24 Comments
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422295
if $this->ticketquery is not a null class do this:


echo "<pre>";
echo print_r( get_class_methods($this->ticketquery) , true );
echo "</pre>";


you will see a list with $this->ticketquery methods (or funtions).
0
 

Author Comment

by:namsu55
ID: 33422355
Its not a null class because I gave the print out of the value which was 13 and that is correct but anyway I done what you said.

Array
(
    [0] => __construct
    [1] => insertItem
    [2] => checkEventField
    [3] => insertEvent
    [4] => insertImage
    [5] => insertJunctionImage
    [6] => getMainCat
    [7] => getSubCatMusic
    [8] => getArtist
    [9] => countArtistTotal
    [10] => Model
    [11] => _assign_libraries
)
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422422
sorry i was wrong. im not very sure what are u trying to get, but if you are trying to get the info of  "artevtid"

do what i said before but with the artevtid object to see its methods.
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

Author Comment

by:namsu55
ID: 33422453
I'm trying to get the 13 value.

I get the print_r to tell me that this is the structure:

stdClass Object ( [COUNT(artevtid)] => 13 )

I am just not sure how I can grab the data.

I have tried:

echo $total->COUNT;
echo $total->COUNT->artevtid;;
echo $total->COUNT['artevtid'];
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422561
ohh! great,

try this:

echo $total->[0];
or
echo $total->['COUNT'];
0
 

Author Comment

by:namsu55
ID: 33422580
Tried both gtagliani and they are syntax errors. Thanks for helping. Just trying to figure it out.
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422589
or this echo $total->['artevtid'];
0
 

Author Comment

by:namsu55
ID: 33422610
I think $variable->[] is always a syntax array, cannot access the object using the array brackets.
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422661
Are u sure that if you do:

$total = $this->ticketquery->countArtistTotal();
¿$total is not 13 there?.

i mean if u do this:

$total = $this->ticketquery->countArtistTotal();
echo $total;

what happens?
0
 

Author Comment

by:namsu55
ID: 33422674
I get:

Severity: 4096

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

Author Comment

by:namsu55
ID: 33422736
The actual database returned value is fine, which is 13. But I just cannot access the value.
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422757
echo $total->artevtid ; ?
0
 

Author Comment

by:namsu55
ID: 33422764
Message: Undefined property: stdClass::$artevtid
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422851
this is the solution i think:


echo $total->['COUNT(artevtid)'];  

0
 

Author Comment

by:namsu55
ID: 33422872
That is a syntax error:

Parse error: syntax error, unexpected '[', expecting T_STRING or T_VARIABLE or '{' or '$' in
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422873
sorry wrong
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33422931
well, i didnt want to give u this, because is not clean but.....


foreach ( $total as $oneTotal )
{
      echo $oneTotal;
}
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33423491
Please run this and post the output here:

var_dump( $this->ticketquery );
0
 
LVL 34

Expert Comment

by:Slick812
ID: 33423541
greetings namsu55, , if you want to expose the non-static properties of an object you might try and use the PHP
get_object_vars  (object $object); function which returns an array of properites (key and value) of an object.

var_dump(get_object_vars  ($total));

you might also try this -
echo $this->ticketquery->artevtid;
and if it shows an integer value, you might try
echo $total[$this->ticketquery->artevtid];

0
 

Author Comment

by:namsu55
ID: 33423703
Hi Ray, that output is just too much for the screen and I am 100% sure the class returns the correct data shown by print_r. Slick 812 get_object_vars gives me an output of an array and the statements below didn't work.

In my SQL I changed the query to this as advised at Code Igniter forum to:

SELECT COUNT(artevtid) FROM artevt AS count

so now what gtagliani said works:

foreach ( $total as $oneTotal )
{
      echo $oneTotal;
}

So I think it was more of an SQL query problem, too complicated to access for my liking through an object.
0
 
LVL 2

Accepted Solution

by:
gtagliani earned 500 total points
ID: 33424019
The problem is how php handles the return answer from de database to an object when in the db field object has a '(' or ')' in its name. try with this

SELECT COUNT(artevtid) as quantity FROM artevt AS count

Try to print_r the object again and the [COUNT(artevtid)] will be replaced with [quantity] so now u can

echo $total->quantity(i think xD);

0
 

Author Comment

by:namsu55
ID: 33426188
Gtagliani: That worked. I reduced the SQL statement to:  "SELECT COUNT(artevtid) AS quantity FROM artevt". But out of curiosity. If Print_r was showing the 13 value, how could I have accessed that. I tried several ways all which didn't work.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33426216
"If Print_r was showing the 13 value, how could I have accessed that."

If you produced the var_dump() that I asked for above, I would have been able to show you this, and more.  Sorry you didn't feel that was appropriate.  I use it all the time, to very good effect.

In the future you may want to set up smaller test cases so that you feel comfortable engaging in the kind of dialog that EE is best for.  Good luck with your project, ~Ray
0
 

Author Comment

by:namsu55
ID: 33426232
Hi Ray I would make the old query again in a short while and do the dump on a file. I would like to learn something about that inner array type of object.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question