Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Classes Ex. #1: Select Field Class and Object

In this PHP exercise, you will build the beginnings of a user registration form. To do this, you will create a class for making the select field, then use an object derived from the class in the form. You can begin with the script you wrote for Functions Ex. #6, since you will be revising the two functions you created there as methods of your class.

First of all, write an array that includes browser types: Firefox, Chrome, Internet Explorer, Safari, Opera, Other.

Then begin to write the class Select. You will need two properties, $name for the name of the select field, and $value, an array to provide the option values. You will also need four methods in addition to the two methods you will adapt from Functions Ex. #6: setName, getName, setValue, getValue. Checking to be sure the value is an array belongs in the setValue method, so write that here, and delete it from from makeSelect if you are using your script from Functions #6.

Now we come to the two functions you wrote to generate the select field for Functions Ex. #6. Change the makeOptions function to iterate over the array argument's values rather than keys. This will be your fifth method. Then revise the makeSelect function to be the sixth method in your class.

Next comes the HTML. Write a user registration form asking for name, username, email, browser. Use text fields to collect the user data for the first three, then instantiate an object based on your class for the select field. When the user clicks the submit button, return the data as confirmation.

If you were creating a registration form to use on the Web, you would want to collect the data in a database. However, using PHP with mySQL or other databases is beyond the scope of this website.

 

Comments

Hi, this is my script:

<?php 
$browsersArray = array("Firefox", "Chrome", "Internet Explorer", "Safari", "Opera", "Other");
 
class select{
	
	function makeOptions($x){
		foreach($x as $value){
		echo "<option value='$value'>$value</option>\n";
		}
	}
	
	function makeSelect($x){
		echo "<select name='browser'>";
	$this->makeOptions($x);
		echo "</select>";
	}
}
?>

<html>
<title>Class Select - Browsers</title>
<body>
<h2>User Registration - Browser</h2>

<?php
if(!isset($_POST['submit'])){
?>

<form action="oop1.php" method="post">
Name:<br>
<input type="text" name="name" size="60"><p>
Username:<br>
<input type="text" name="username" size="60"><p>
Email:<br>
<input type="text" name="email" size="60"><p>
Browser:

<?php
$browser = new select;
$browsers = $browsersArray;
$browser->makeSelect($browsers);
?>

<p>
<input type="submit" name="submit" value="Go">
</form>

<?php
}
else{

$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$browser = $_POST['browser'];

echo "The following data has been saved for $name:<br>\n";
echo "Username: $username<br>\n";
echo "Email: $email<br>\n";
echo "Browser: $browser\n";
}
?>
</body>
</html>

I only used the two functions from ex. #6. Is my script ok? or do I need to revise it? any comments or suggestions on it will be much appreciated.

thank you

Readers: In case you're not familiar with the syntax highlighter's functions, just click 'expand source' in pogimesabiniate's post to see his solution.

I haven't had time to check whether your script works, but I can see that you did not follow the instructions in the exercise. You might be able to get this script to give the same output with just those two methods, but this script serves as the basis for the next two exercises, which need a more developed class. Also, your class does not include validation.

The four methods you left out are standard to many classes, so it will better prepare you to write classes in the future if you rewrite your script to include them.

"Checking to be sure the value is an array belongs in the getValue method"

getValue change to setValue
it's a little confusing given the fact in scirpt setValue is checking for array.

Thanks for catching that typo! We can see how that could be confusing. Now corrected.

I've written so many method with so little function while following the exercise instructions. Here's my version for this exercise:

  1. <?php
  2. 	$browserList=array('Firefox','Chrome','Internet Explorer','Safari','Opera','Other');
  3.  
  4. 	class selections {
  5. 		public $selectionName;
  6. 		public $optionList;
  7.  
  8. 		function __construct ($name, $optList){
  9. 			$this->selectionName=$name;
  10. 			$this->optionList=$optList;
  11. 		}		
  12. 		function setName($name){
  13. 			$this->selectionName=$name;
  14. 		}		
  15. 		function getName(){
  16. 			return $this->selectionName;
  17. 		}		
  18. 		function setValue($optList){
  19. 			if (is_array($optList)
  20. 			$this->optionList=$optList;
  21. 			else echo "<br/><em>Problem occured while pulling option list.</em><br/>";
  22. 		}
  23. 		function getValue(){
  24. 			return $this->optionList;
  25. 		}
  26. 		function createSelections(){
  27. 			echo "<select name=\"$this->selectionName\" multiple='yes' size='6'>";
  28. 			$this->createOptions($this->optionList);
  29. 			echo "</select>";
  30. 		}
  31. 		function createOptions($optList){		
  32. 			foreach($optList as $option){
  33. 				echo "<option value=\"$option\">".strtoupper($option)."</option>";
  34. 			}
  35. 		}	
  36. 	}
  37. ?>
  38. <html>
  39. <head>
  40. <title>Joe Play - Ground Zero</title>
  41. </head>
  42. <body>
  43. 	<?php		
  44. 		if (isset($_POST['nameReal']) && isset($_POST['nameUser']) && isset($_POST['email']) && isset($_POST['browserSelect'])){
  45. 			echo "You entered this information: <br/>";
  46. 			echo "Name is: <li>".$_POST['nameReal']."</li><br/>";
  47. 			echo "UserName is: <li>".$_POST['nameUser']."</li><br/>";
  48. 			echo "email is: <li>".$_POST['email']."</li><br/>";
  49. 			echo "Browser is: <li>".$_POST['browserSelect']."</li><br/>";
  50. 			echo "Submit:".$_POST['BUTTONES'].".";
  51. 		}
  52. 		echo "Please Fill The Form: <br/>";
  53. 	?>
  54. 	<form action="<?php echo"{$_SERVER['PHP_SELF']}";?>" method="POST">		
  55. 		Name:<input name="nameReal" type="text" maxlength="30" /> <br/>
  56. 		UserName:<input name="nameUser" type="text" maxlength="30" /> <br/>
  57. 		Email:<input name="email" type="text" maxlength="100" /> <br/>
  58. 		Browser: <br/>
  59. 		<?php 
  60. 			$selectField = new selections("browserSelect", $GLOBALS['browserList']);
  61. 			$selectField->createSelections();
  62. 		?>
  63. 		<br/><input type="submit" name="BUTTONES" value="Check it!"/>
  64. 	</form>
  65. </body>
  66. </html>