Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access a STD Object in PHP

Posted on 2010-08-12
24
Medium Priority
?
1,364 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 111

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 2000 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 111

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

782 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