Jump to content

[SOLVED] I need help in trying to assign value to a variable...


Jim R

Recommended Posts

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();

Link to comment
Share on other sites

-> 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?

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.