Page 1 of 1

Functions within functions: Setting variables

Posted: Tue Apr 22, 2003 9:52 pm
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

Posted: Wed Apr 23, 2003 12:53 am
by volka
for all other variables/properties you used this->name, for all but $color. why?

Posted: Wed Apr 23, 2003 6:01 am
by Jim
I'm guessing you mean within the SQL statement? $this->color didn't work, so I wanted to simply try color.

Posted: Wed Apr 23, 2003 12:38 pm
by Jim
Excuse me while I...

... *bump*

Posted: Wed Apr 23, 2003 1:04 pm
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();
    }       
  }
}

Posted: Wed Apr 23, 2003 6:59 pm
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)

Posted: Thu Apr 24, 2003 12:07 am
by Jim
Worked like a charm, Volka!

Thanks for the help!