Page 1 of 1

PHP Static Class Problem

Posted: Wed Feb 08, 2012 1:35 am
by artofwork
I've been playing around with this code I wrote all day and I cannot find a solution besides using a loop and parameters to pass to this class which i'm trying to avoid, could someone take a look and tell what the heck im doing wrong. Classes are very new to me.

Code: Select all

<?php

class CreateField
{
	private static $field = array();
	private static $index = 0;
	
	function __construct($_POST)
	{
		switch($_POST)
		{
			case 'add':
				self::createField();
				break;
			case 'sub':
				self::destroyField();
				break;
			default:
				break;
		}
	}

	
	private static function createField()
	{
		self::$field[self::addIndex()] = "<textarea name=\"text_".self::$index."\"></textarea><br />";
	}
	
	private static function destroyField()
	{
		self::$field[self::subIndex()] = " ";
	}
	
	public function getField()
	{
		return self::$field;
	}
	private static function addIndex()
	{
		return self::$index++;
	}
	
	private static function subIndex()
	{
		return self::$index--;
	}
}
?>

Re: PHP Static Class Problem

Posted: Wed Feb 08, 2012 1:39 am
by artofwork
Forgot to mention the problem lol... its late.. The problem is I'm trying to generate more then 1 textarea in the area, it creates the box and remove it but doesn't create more then 1.

Re: PHP Static Class Problem

Posted: Sat Feb 11, 2012 3:20 pm
by Mordred
Did ... did you just write 30+ lines of code to do that?
*shudder*
Anyway, try ++$index. Or even better, Ctrl+A, Del