Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Classes Ex. #2: Multiple Select Field Objects

In the last PHP exercise, the Select class may have seemed like an awful lot of code to write for a simple select field. The real value of classes and objects doesn't become apparent until you have reason to reuse the code. So this time, you will expand your user registration form to use several select fields.

Assume that you have good reason to need data about your users' browsing capabilities. Either you want to tune your site, the content of your site concerns these issues, or something similar. Using your select class, you can reuse the class code as often as you like to create select fields.

To build this new version of the registration form, start with the script you wrote for Classes Ex. #1. Add the value of None as the first value in the $browsers array. Write two more arrays: $speeds, including values Unknown, DSL, T1, Cable, Dialup, Other; and $os, including Windows, Linux, Macintosh, Other. (Of course, these could be screen resolution or flash version or any other relevant capability.)

You want data for how the user browses both at home and work. Above the browser select field, add the subheading Work Access, and rename the browser label Primary Browser. (We all know that many people use more than one.) Below that, add labels and select field objects for Connection Speed and Operating System. Next, add the subheading Home Access, with three new select fields corresponding to the ones you created for Work Access.

Since you are using so many objects in this script, it's a good idea to destroy each one after it has done its work. This will free up the memory the object occupied.

When the user hits the submit button, return the user's select field choices in two bulleted lists under the same headings (Work Access, Home Access).

 

Comments


<?php
    $browsers = array('None', 'Firefox', 'Chrome', 'Internet Explorer', 'Safari', 'Opera', 'Other');
	$speeds = array('Unknown', 'DSL', 'T1', 'Cable', 'Dialup', 'Other');
	$os = array('Windows', 'Linux', 'Macintosh', 'Other');
	class Select {
		private $name;
		private $value;
		
		//Setter
		public function setName($name) {
			$this->name = $name;
		}
		public function setValue($value) {
			// is_array($value) ? 'Array' : 'not an Array';    OR
			if (!is_array($value)) {
				die ("Error: value is not an array.");
			}
			$this->value = $value;
		}
		
		//Getter
		public function getName() {
			return $this->name;
		}
		public function getValue() {
			return $this->value;
		}
		
		public function makeSelect() {
		echo "getName()."\">";
			$this->makeOptions($this->getValue());
		echo "";
		}
		public function makeOptions($val) {
			foreach($val as $va) {
				$this->options($va);
			}
		}
		public function options($v) {
			echo "".ucfirst($v)."";
		}
		public function __destruct() {
			//echo 'Object was just destroyed ';
		}
	}
	
	if( !isset($_POST['submit']) ) {
		echo "<h2>User Registration<br /></h2>";
		echo "<form action=\"test.php\" method=\"POST\">";
		echo "Name: <input type=\"text\" name=\"name\"  size=\"60\" /><br/>";
		echo "Username: <input type=\"text\" name=\"username\"  size=\"60\" /><br/>";
		echo "Email: <input type=\"text\" name=\"email\"  size=\"60\" /><br/>";
		echo "<p><strong>Work Access</strong></p>";
		echo "Primary Browser: ";
			$browser_work = new Select;
			$browser_work->setName('browser_work');
			$browser_work->setValue($browsers);
			$browser_work->makeSelect();
		echo "<br />Connection Speed: ";
			$speed_work = new Select;
			$speed_work->setName('speed_work');
			$speed_work->setValue($speeds);
			$speed_work->makeSelect();
		echo "<br />Operating System: ";
			$os_work = new Select;
			$os_work->setName('os_work');
			$os_work->setValue($os);
			$os_work->makeSelect();
		echo "<p><strong>Home Access</strong></p>";
		echo "Primary Browser: ";
			$browser_home = new Select;
			$browser_home->setName('browser_home');
			$browser_home->setValue($browsers);
			$browser_home->makeSelect();
		echo "<br />Connection Speed: ";
			$speed_home = new Select;
			$speed_home->setName('speed_home');
			$speed_home->setValue($speeds);
			$speed_home->makeSelect();
		echo "<br />Operating System: ";
			$ops_home = new Select;
			$ops_home->setName('os_home');
			$ops_home->setValue($os);
			$ops_home->makeSelect();
		echo "<br />";
		echo "</form>";
	} else {
		$input_local = array($_POST['name'], $_POST['username'], $_POST['email']);
		$work_access = array($_POST['browser_work'], $_POST['speed_work'], $_POST['os_work']);
		$home_access = array($_POST['browser_home'], $_POST['speed_home'], $_POST['os_home']);
		
		echo "<p>The following data has been saved for $input_local[0]: </p>\n";
		echo "<p>Username: $input_local[1]<br />\n";
		echo "Email: $input_local[2]</p>\n";
		
		echo "Work Access";
		echo "<ul>";
			foreach($work_access as $value) {
				echo "<li>".$value ."</li>";
			}
		echo "</ul>";
		echo "Hom Access";
		echo "<ul>";
			foreach($home_access as $value) {
				echo "<li>".$value ."</li>";
			}
		echo "</ul>";
	}
	?>

I've written everything from scratch while just following the instruction. Not a piece of code was borrowed from the solution script, so this baby is kinda unique. I've thrown a few CSS line to somewhat beautify the fields. Here's the complete code:

<?php
	$browserList=array('None','Firefox','Chrome','Internet Explorer','Safari','Opera','Other');
	$speedList=array('Unknown','DSL','T1','Cable','DialUp','Other');
	$osList=array('Windows','Linux','Macintosh','Others');
	
	class selections {
		public $selectionName;
		public $optionList;
		
		function __construct ($name, $optList){
			$this->selectionName=$name;
			$this->optionList=$optList;
		}		
		function setName($name){
			$this->selectionName=$name;
		}		
		function getName(){
			return $this->selectionName;
		}		
		function setValue($optList){
			if (is_array($optList))
			$this->optionList=$optList;
			else echo "<br/><em>Problem occured while pulling option list.</em><br/>";
		}
		function getValue(){
			return $this->optionList;
		}
		function createSelections(){
			echo "<select name=\"$this->selectionName\" multiple='yes' size='6'>";
			$this->createOptions($this->optionList);
			echo "</select>";
		}
		function createOptions($optList){		
			foreach($optList as $option){
				echo "<option value=\"$option\">".strtoupper($option)."</option>";
			}
		}	
	}
	
	function showSelected($keyWord){
		if (isset($_POST[("browser$keyWord")])) echo "<li>".$_POST[("browser$keyWord")]."</li>";
		if (isset($_POST[("speed$keyWord")])) echo "<li>".$_POST[("speed$keyWord")]."</li>";
		if (isset($_POST[("os$keyWord")])) echo "<li>".$_POST[("os$keyWord")]."</li>";
	}
?>
<html>
<head>
<title>Jose Palayen - PHP Ground Zero</title>
<style>
.selectBox {float: left; position: relative;}
.clearBox {clear: both;}
.userInfo {width: 250px;}
label {float: left;}
.userInfo input {float: right;}
</style>
</head>
<body>
	<?php		
		if (isset($_POST['nameReal']) && isset($_POST['nameUser']) && isset($_POST['email'])){
			echo "You entered this information: <br/>";
			echo "Name is: <em>".$_POST['nameReal']."</em><br/>";
			echo "UserName is: <em>".$_POST['nameUser']."</em><br/>";
			echo "email is: <em>".$_POST['email']."</em><br/>";
			//Show selection list
			echo "<br/> Work Internet Access Details:";
			echo "<ul>";
			showSelected("SelectWork");
			echo "</ul>";
			echo "<br/> Home Internet Access Details:";
			echo "<ul>";
			showSelected("SelectHome");
			echo "</ul>";
		}
		else {
		echo "Please Fill The Form: <br/>";	
		echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";	
		echo "<div class='userInfo'>";
		echo"<label for='nr'>Name:</label><input name='nameReal' type='text' maxlength='30' id='nr'/> <br/>";
		echo"<label for='nu'>UserName:</label><input name='nameUser' type='text' maxlength='30' id='nu'/> <br/>";
		echo"<label for='mail'>Email:</label><input name='email' type='text' maxlength='100' id='mail'/> <br/>";
		echo"</div>";
			echo "<br/ class='clearBox'>Work Access:</em><br/>";
			echo "<div class='selectBox'>";
			echo " Primary Browser:";
			$selectField = new selections("browserSelectWork", $GLOBALS['browserList']);
			$selectField->createSelections();			
			unset($selectField);
			echo "</div>";
			echo "<div class='selectBox'>";
			echo " Speed:<br/>";
			$selectField = new selections("speedSelectWork", $GLOBALS['speedList']);
			$selectField->createSelections();
			unset($selectField);
			echo "</div>";
			echo "<div class='selectBox'>";
			echo " OS:<br/>";
			$selectField = new selections("osSelectWork", $GLOBALS['osList']);
			$selectField->createSelections();
			unset($selectField);
			echo"</div>";
			
			echo "<br/ class='clearBox'>Home Access:";			
			echo "<div class='selectBox'>";
			echo " Primary Browser:";
			$selectField = new selections("browserSelectHome", $GLOBALS['browserList']);
			$selectField->createSelections();
			unset($selectField);
			echo "</div>";
			echo "<div class='selectBox'>";
			echo " Speed:<br/>";
			$selectField = new selections("speedSelectHome", $GLOBALS['speedList']);
			$selectField->createSelections();
			unset($selectField);
			echo "</div>";
			echo "<div class='selectBox'>";
			echo " OS:<br/>";
			$selectField = new selections("osSelectHome", $GLOBALS['osList']);
			$selectField->createSelections();
			unset($selectField);
			echo "</div>";
			echo "<br/ class='clearBox'>";
		echo "</form>";}
	?>
</body>
</html>

First of all I am new in developing and this site rocks!!! I would like someone to explain why using the unset function after every list.Is there any reason why do I have to destroy the variables? Aren't they being destroyed every time someone fills the form?