PHP getElementById

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

okay, so I'm trying to figure out how to get an attribute by id within my php document. I've looked high and low for the entire day; everything refers to doing so through loading an xml document. I don't want to do this. I have a dynamically generated span tag within my site. I need to find that span tag by id, and then return the value of it's class attribute. The span tag looks something like this, with the class being dynamically generated:

HTML Code: [ Select ]
<span id="auth_name" class="bob"></span>
 
  1. <span id="auth_name" class="bob"></span>
  2.  


So how do I simply find the span by id and get the value of the class attribute?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

I'm confused, you want to manipulate the DOM of a page that's already been generated? That's what Javascript is for.

With PHP you'd have to load the HTML document as a DOM tree and go from there.

Am I not understanding you correctly?
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I could use javascript, but it feels like it would take a lot more code for what I need to do. It's for a wordpress blog. I'm displaying tweets from a twitter feed in my footer, which is outside the loop (meaning I can't simply get the author of a post). basically, if I'm on anything other than a single article, I want to display tweets from the sites twitter account. If I'm on a single post, then I want to display tweets from the authors twitter account.

To work around the fact that I can't get the authors name from outside the loop, I insert the span tag from within the loop as a hidden and empty tag. This way the author's name is within the page and I can get to it from outside the loop.

If I were to use javascript, it requires an xmlHTTPrequest that I don't really want to write. It'd be easier just to use php since I already have the code written. All I need to do is get the authors name out of the class of the span.

Whew, so there's the long winded explanation. So how do I load the html document into a php DOM tree?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Have you considered referencing a global variable "global $my_holder;" from "within the loop" ? That way you can assign whatever data you need in the format you need it (array, object, string, etc) then still have it "outside the loop".
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

wow, that worked. Didn't expect it to be that easy...

Thanks JB
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Global variables get a bad rap from the OOP crowd, but they're actually very useful in some situations.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

joebert wrote:
Global variables get a bad rap from the OOP crowd, but they're actually very useful in some situations.


They get a bad rap from the functional crowd, too ;)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

That's new to me. The only people I've seen cursing them end up trying to instantiate a mobile home factory just to read the serial number on a gas stove. :scratchhead:

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 116 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.