Functions within functions: Setting variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Functions within functions: Setting variables

Post by Jim »

Hey all.... quick question. I have the following code for function do_news:

Code: Select all

function do_news() {

	$this->site = $_GET['site'];
	$this->author = $_COOKIE['gp_user'];
	$this->headline = $_POST['headline'];
	$this->caption = $_POST['caption'];
	$this->news = $_POST['news'];
	$this->color = $color;


	$this -> select_category();
	//$this -> text_news();

		$sql = "insert into ".$this->site."_news ( author , headline , caption , news , color ) values ('$this->author' , '$this->headline' , '$this->caption' , '$this->news' , '$color')";
		$act = mysql_query($sql);

			if (!$act) { echo mysql_error(); }		
	

	}
As you can see, it calls the function select_category. (Yes, this is in a class, and I get no errors)

This is the code for select_category:

Code: Select all

function select_category() {

	$this->category = $_POST['category'];
	
		$sql = "select cat_hex from news_cat where cat_name = '$this->category'";
		$act = mysql_query($sql);

			while ($hex = mysql_fetch_array($act)) {

			$color = $hex['cat_hex'];

			echo $color;

			}

	}
What I want to do is set the variable $color within function select_cat (this works fine by itself, by the way) inside of function do_news, and insert the value of $color in to the SQL statement within do_news.

Any reason that won't work? Any ideas to solve the problem?

Help is appreciated!

-Jim
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

for all other variables/properties you used this->name, for all but $color. why?
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I'm guessing you mean within the SQL statement? $this->color didn't work, so I wanted to simply try color.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Excuse me while I...

... *bump*
chris22
Forum Newbie
Posts: 11
Joined: Tue Apr 22, 2003 9:45 pm

Post by chris22 »

Jim wrote:$this->color didn't work, so I wanted to simply try color.
Of course it isn't going to work. $color isn't defined when you're setting the class' $color property.

First off, OOP is supposed to be abstract. When you're setting properties to specific arbitrary request variables, you're losing the abstractness, which is the whole point of OOP. At that point, you may as well just straight code it. And really, the following code should not be done OO. But since you asked...

Code: Select all

class your_class{
  function select_category($category_name) { 
    $sql = "select cat_hex from news_cat where cat_name = '$category_name'"; 
    $act = mysql_query($sql); 

    // if you're only selecting one item from the database, using mysql_result is much faster than fetch_array.
    return mysql_result($act, 0, 0);
  }

  function do_news($site, $author, $headline, $caption, $news, $category) {
    $color = $this->select_category($category);
    $sql = "insert into ".$site."_news ( author , headline , caption , news , color ) values ('$author' , '$headline' , '$caption' , '$news' , '$color')"; 
    $act = mysql_query($sql); 

    if (!$act) {
      echo mysql_error();
    }       
  }
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

function select_category() {
...
$color = $hex['cat_hex'];
...
}
this sets a variable $color within the scope of select_category. method ends, varibale ends. try $this->color = $hex['cat_hex']; instead (and also for the sql-string)
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Worked like a charm, Volka!

Thanks for the help!
Post Reply