All pastes #674954 Raw Edit

Something

public text v1 · immutable
#674954 ·published 2007-08-29 18:05 UTC
rendered paste body
<?php
//require('MyMsql.php');
//
//$db = new MyMysql('localhost', 'root', '', 'test');
//$db->query('SELECT * FROM andmed');
//
//while ($row = mysql_fetch_object($db->getResult())) {
//	echo $row->username . ' <br />';
//}
//$conn = mysql_connect('localhost', 'root', '');
//mysql_select_db('test');
//
//$query = 'SELECT username, email FROM andmed';
//$result = mysql_query($query);
//
//while ($row = mysql_fetch_object($result)) {
//	echo $row->username . ' '. $row->email .'<br />';
//}
//
//mysql_close($conn);
class Error {
	
	private $errors = array();
	private $data = array();
	
	public  function __construct() {
		$this->getPOSTdata();
	}
	
	/**
	 * Checks if there is errors
	 *
	 * @return true if there is no errors
	 */
	public function isErrors() {
		if (sizeof($this->errors) > 0) {
			return true;
		}
	}
	
	/**
	 * Sets error on field
	 *
	 * @param field $field
	 * @param error $error
	 */
	public function setError($field, $error) {
		$this->errors[$field] = $error;
	}
	
	/**
	 * Returns data from $this->data variable
	 *
	 * @return unknown
	 */
	public function getData() {
		return $this->data;
	}
	
	/**
	 * Returns errors from var $this->errors
	 *
	 * @return unknown
	 */
	public function getErrors() {
		return $this->errors;
	}

	/**
	 * Gets the $_POST data and sets to local object variablese
	 *
	 */
	public function getPOSTdata() {
		foreach ($_POST as $field => $value) {
			$this->__set($field, $value);
		}
	}
	
	public function __set($field, $value) {
		$this->data[$field] = $value;
	}
	
	public function __get($field) {
		if (array_key_exists($field, $this->data)) {
			return $this->data[$field];
		}
	}
	
	public function __destruct() {
		unset($this->errors);
	}
}


if (isset($_POST['post'])) {
	$val = new Error();	
	
	//print_r($val->getData());
	if ($val->name == '') {
		$val->setError('Name', 'field is empty!');
	}
	
	if ($val->email == '') {
		$val->setError('Email', ' field is empty!');
	}
	
	if (!$val->isErrors()) {
		echo 'We have here success!';
	} else {
		foreach ($val->getErrors() as $key => $value) {
			echo $key .' : '. $value .'<br />';
		}
	}
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table>
 <tr>
  <td><label>Name:</label></td>
  <td><input type="text" name="name" value="<?php echo $_POST['name']; ?>" /></td>
 </tr>
 <tr>
  <td><label>E-mail:</label></td>
  <td><input type="text" name="email" value="<?php echo $_POST['email']; ?>" /></td>
 </tr>
 <tr>
  <td valign="top"><label>Text:</label></td>
  <td><textarea name="text" cols="30" rows="5"><?php echo $_POST['text']; ?></textarea></td>
 </tr>
  <tr>
  <td colspan="2" align="right"><input type="submit" name="post" value="Post" /></td>
 </tr>
</table>
</form>