Jump to content

php class problem


pouncer

Recommended Posts

[code=php:0]
class Profile_Details {
  var $username;
  var $sql;
  var $row;

  // Constructor.
  function Profile_Details($uname) {
  $this->username = $uname;
  $userid = $_SESSION['UserID'];
  $sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'");
  $row = mysql_fetch_assoc($sql);

  echo $row['Forename']; //this line echos correctly
  }

  function Get_Name() {
  return "gfgs" . $row['Forename'];
  }

[/code]

but the on the other page when i do

<?php echo $profile->Get_Name(); ?>

it just echos gfgs

and not the name after it, anyone see where im going wrong
Link to comment
Share on other sites

thanks jesirose. this worked

class Profile_Details {
var $username;
var $sql;
var $row;

// Constructor.
function Profile_Details($uname) {
$this->username = $uname;
$userid = $_SESSION['UserID'];
$this->sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'");
$this->row = mysql_fetch_assoc($this->sql);
}

function Get_Name() {
return "gfgs" . $this->row['Forename'];
}
}
Link to comment
Share on other sites

hm, but ive got lots of functions like this..

function Get_Name() {
return $this->row['Forename'];
}

function Get_Surname() {
return $this->row['Surname'];
}

function Get_Age() {
return $this->row['Age'];
}

function Get_Gender() {
return $this->row['Gender'];
}

function Get_Location() {
return $this->row['Location'];
}

should i put those all in the constructor?
Link to comment
Share on other sites

That's what I would do.
Think of the class as a blueprint.

Every user has an age, gender, name - those should all be vars for it.
var $age; var $gender;

Plus, that's less for you to write. $this->age is cleaner than $this->row['age'], right?

Redbull: you're right, they are valid but it doesn't follow what I've been taught about OOP design - so I'm just trying to explain how I've always done classes, the way my professors and boss showed me. I could be wrong. *shrug*
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.