PHP Array Example
 

Feature: selectboxArrayFill($array, $selboxname) function
Credit:Imagenation

In this example we show the use of arrays to create and fill a select box or drop down list box. As a bonus we also show an alternate technique for filling a select box using values stored on a file. The file is a simple CSV(comma separated values) file with one line for each entry into the select box. The array method is preferred if you are loading data from a database or just statically initializing a select box; the CSV file method is preferred when you want to allow select box items to be available for simple editing using notepad or vi.

The figure below shows an example of the selectbox being filled:

Here is the PHP code that was used to produce the above web page:

<html>
<head>
<title>PHP Load SelectBox</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#CCCCCC">
<h1>Select Box - Automated Filling</h1>
<hr>
<form>
<?php
function selectboxFill($filename, $selboxname){
  if(!file_exists($filename)){ //If the file does not exist-Exit
    echo "<h1>Error file not found: $filename </h1>";
    return;
  }
  //put in Select statement - it is assumed the <form> already exists
  echo '<SELECT name="'. $selboxname.'">';
  $fp = fopen ($filename,"r"); //open the file for reading
  //fgetcsv() returns an array of values as delimited by commas here
  //at end of file it returns False

  while ($data = fgetcsv ($fp, 1000, ",")) {
    $num = count ($data); //$num is the number of items in the array $data
    //corrections if the count is not 3; any items above 3 are ignored
    if($num<=0)break;
    if($num==1){$data[1]="0"; $data[2]=" ";}
    if($num==2 || trim(strtolower($data[2])) != "selected") $data[2]=" ";
    //output the options where, key=data[0] is option value, value=data[1] is display value
    //and if present, the data[2] indicates selected keyword

    echo "\n <OPTION value='".$data[0]." ' ".$data[2].'> '.$data[1].'</OPTION>';
  }
  //Close file and select control
  fclose ($fp);
  echo "\n</SELECT>\n";
}
echo "File filled selectbox: ";
selectboxFill("phpselectboxvals.txt", "dropbox"); //Test the routine
function selectboxArrayFill(&$array, $selboxname){
  echo '<SELECT name="'. $selboxname.'">';
  //Note: the while(list)=each()) is equivalent to:
  //foreach($array as $key => $value){
  reset($array);
  while(list($key, $value)= each($array)){
    //Check for omitted values, and correct
    if(trim($key)=="")$key=$value;
    if(trim($value)=="")$value=$key;
    //Emit option
    echo "\n <OPTION value='".$key."'>".$value."</OPTION>";
  }
  //Close select control (not use of newlines for HTML formatting
  echo "\n</SELECT>\n";
}
$ratings=array('nochoice','poor','fair','good', 'better', 'best');
echo "<br><br><font color='red'>Array filled selectbox: </font>";
selectboxArrayFill($ratings, "Arraybox"); //Test the Array fill
?>
</form>
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-4281076-1"); pageTracker._initData(); pageTracker._trackPageview(); </script><script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-4281076-1"); pageTracker._initData(); pageTracker._trackPageview(); </script></body>
</html>

Fitting this code into a broader scheme of page generation is fairly straightforward. In fact, a variation on the selectboxArrayFill() routine can be found in the Moodle and postNuke source code. Developers will find that auto filling of select boxes adds to the flexibility of their programs.

 
Top of Page  Tutorials Home  PHP References  PHP Books
©Imagenation 2000-2004