Jim R Posted September 2, 2009 Share Posted September 2, 2009 What does this line mean: $tag = $wp_query->get_queried_object(); I don't follow what the " -> " is for, and it's hard to search for it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/ Share on other sites More sharing options...
Garethp Posted September 2, 2009 Share Posted September 2, 2009 It's saying in the class that's assigned to $wp_query, execute the function get_queried_object(); Basically, $wp_query was given a class, which is a collection of functions essentially. So $wp_query->get_queried_object(); is saying, in that collection of functions, run get_queried_object(); Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911013 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 -> is PHP's way of accessing member functions and member variables in an object. say I had a class foo, with a function bar that would output "Hello world!" to access it I would do the following $foo = new foo(); $foo->bar(); and the foo class itself would look like class foo { function bar(){ echo "Hello World!"; } } make sense? Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911017 Share on other sites More sharing options...
JonnoTheDev Posted September 2, 2009 Share Posted September 2, 2009 It's saying in the class that's assigned to $wp_query, execute the function get_queried_object(); $wp_query is an object that is an instance of a class (not assigned). get_queried_object is a method encapsulated within the class i.e <?php class foobar { public function sayHello() { print "Hello from foobar"; } } // usage $x = new foobar(); $x->sayHello(); ?> $x is an object of foobar. The class foobar has 1 internal method: sayHello. Therefore calling this method with the instantiated object is done using the following $x->sayHello(); Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911023 Share on other sites More sharing options...
Jim R Posted September 2, 2009 Author Share Posted September 2, 2009 Mike, So you're saying if that class had multiple functions, I would telling it which function to get within that class? I'm trying to apply what Garet said, because I'm mostly just going to be doing the "getting" and not much of the producing of code. I want to take a variable from one result and use it to drag information off of another MySql table. More specifically, I'm talking WordPress Tags. When an Post is written, it can have Tags assigned to it, which I'm sure everyone knows, but I'm new to WP. Those Tags have a number of a values associated to them, including an ID. When you click on the Tag on my site, the URL is nameofsite.com/tag/firstWord-secondWord. In most cases, my Tags are names of basketball players. So essentially when you click on a link, you're getting to an Archive.php page that has every Post Tagged with that player's name. From there I want to assign information from another table, as a way to create kind of a profile on their Archive page. So I have to get the Tag ID#. The rest, I think I can do. WP has ways of doing it, but I'm new enough and don't know enough basic PHP to really know what I was searching for. As I learn what to look for, I can at least start to learn how to apply it. But WP does have a lot of wonderful widgets on which to apply lots of functionality that in PHP would require lots of extra coding. Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911032 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 Mike, So you're saying if that class had multiple functions, I would telling it which function to get within that class? I'm trying to apply what Garet said, because I'm mostly just going to be doing the "getting" and not much of the producing of code. ehh more or less. You aren't so much telling it which function to get, you are calling a function much like a normal call, but the syntax is slightly different because that function is encapsulated (basically inside) the class itself. The main difference between normal functions and class functions is they have access to class variables (This is the basics of data encapsulation incase you are wondering) but for your project, that probably won't matter much to you. So in short, the following snippet (taken from your first post) $tag = $wp_query->get_queried_object(); will execute the get_queried_object function of whatever class the $wp_query object was instantiated as, and assign the return value of the function to the $tag variable Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911037 Share on other sites More sharing options...
Jim R Posted September 3, 2009 Author Share Posted September 3, 2009 Ok...I got the desired value out of WordPress. Take a look at this. When I echo out $wp_tagID, which is defined in the first line of the PHP code, it outputs the correct value. There is a corresponding ID in my playerRank table, and it should be echo'ing information from the one that matches. I'm putting this code in a PHP widget block in WordPress, but I'm getting no result. <?php $wp_tagID = get_query_var('tag_id'); mysql_select_db("jwrbloom_hhr"); $query = 'SELECT * FROM playerRank'; $results = mysql_query($query); while($line = mysql_fetch_assoc($results)) { if ($line['wpID'] = $wp_tagID) { echo '<div>' . $line['nameFirst'] . ' ' . $line['nameLast'] . '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911348 Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 Is get_query_var(); a defined function? Also, try turning all errors on Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911356 Share on other sites More sharing options...
Jim R Posted September 3, 2009 Author Share Posted September 3, 2009 Yes, it is a defined function within WordPress, and when on its own, echo'ing $wp_tagID produces the desired value. Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911365 Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 Try echo get_query_var('tag_id'); What's the result? Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911367 Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 Also if ($line['wpID'] = $wp_tagID) { Should be if ($line['wpID'] == $wp_tagID) { Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911368 Share on other sites More sharing options...
Jim R Posted September 3, 2009 Author Share Posted September 3, 2009 Still not producing any results. Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911372 Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 You opened a while loop, AND an if statement, but you forgot to close it. Could that be the problem Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911374 Share on other sites More sharing options...
Jim R Posted September 3, 2009 Author Share Posted September 3, 2009 That was it. I didn't close either one of them. THANKS!!! Quote Link to comment https://forums.phpfreaks.com/topic/172855-solved-i-need-help-in-trying-to-assign-value-to-a-variable/#findComment-911376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.