Welcome!

And don't forget to edit your signature & profile.

 

Icon

Statistics

  • Total posts 23318
  • Total topics 4048
  • Total members 5574
  • Our newest member
    ALF5583

TOP POSTERS

PHP Variable Variables

All problems and developments related to PHP, Ruby on Rails & Co. are discussed and resolved here.
   

PHP Variable Variables

Postby seventeen » Wed Oct 21, 2009 4:57 pm

I'm building a blog comment class right now and I thought I might try to use variable variable names in the constructor. The database columns match up exactly with the name of the class' data members. Does anyone have any idea why this code isnt working correctly? I'm not getting any error messages. It just doesnt appear to be assigning any values to any of the data members. I think the logic is right, and I'm just overlooking something simple. Maybe someone has better eyes than I do?

Thanks!

Code: Select all
class Comment {
   
   public $id;
   public $post_id;
   public $name;
   public $email;
   public $website;
   public $body;
   public $date;
   public $ip_address;
   
   function __construct($id) {
      
      global $db;

      $resc = $db->query("SELECT * FROM blog_comments WHERE id='$id' LIMIT 1");
      
      while($row = $db->fetch_assoc($resc)) {
         while ($comment = current($row)) {
             $key = key($row);
            $this->$$key = $comment[$key];
            next($row);
         }
      }
   }
}
User avatar
seventeen
Smashing <hr />
 
Posts: 58
Joined: Sat Aug 08, 2009 5:36 pm
Location: Denton, Tx
   

   

Re: PHP Variable Variables

Postby falkencreative » Wed Oct 21, 2009 5:16 pm

First thing I would do is check this line:

Code: Select all
$this->$$key = $comment[$key];


Looks like you have an extra "$".
Benjamin Falk | student : designer : developer
http://www.falkencreative.com | http://www.twitter.com/falkencreative
User avatar
falkencreative
Smashing <li>
 
Posts: 261
Joined: Sun Jan 18, 2009 7:25 pm
Location: Chico, CA
   

   

Re: PHP Variable Variables

Postby seventeen » Wed Oct 21, 2009 5:20 pm

Ahh, ofcourse, this:
Code: Select all
$this->$$key = $comment[$key];

has been changed to this:
Code: Select all
$this->$key = $comment[$key];


However, now I am only getting the first character in each field.

This is what print_r($obj); looks like:
Code: Select all
Comment Object
(
    [id] => 1
    [post_id] => 1
    [name] => J
    [email] => j
    [website] => h
    [body] => b
    [date] => 1
    [ip_address] => :
    [status] => 1
)


Any ideas as to why that might be happening?

This is what the actual query returns:
Image
User avatar
seventeen
Smashing <hr />
 
Posts: 58
Joined: Sat Aug 08, 2009 5:36 pm
Location: Denton, Tx
   

   

Re: PHP Variable Variables

Postby seventeen » Wed Oct 21, 2009 6:41 pm

Nevermind, figured it out. should have used [] instead of {} in accessing the array element. ofcourse.
User avatar
seventeen
Smashing <hr />
 
Posts: 58
Joined: Sat Aug 08, 2009 5:36 pm
Location: Denton, Tx
   


Return to Server-side Scripting



Who is online

Users browsing this forum: No registered users and 2 guests