Static array on drowpdown

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
ach8
User
Posts: 174

Static array on drowpdown

Post by ach8 »

I want to create my own dropdown field. I use lookup table and i want to replace with own code on Lookup_Selecting function:

if ($fld->Name == "yearfield"){

$th = date("Y");
$ar_th = "[";
for($x=2010;$x<=$th;$x++){
	$coma = ",";
	if($th == $x){
		$coma = "";
	}
	$ar_th .= "[".$x.",".$x."]".$coma;
}
$ar_th .= "]";
//echo $ar_th; exit();

$fld->Lookup->setOptions($ar_th);

}

what's wrong so it doesn't appear on the my dropdown?


mobhar
User
Posts: 11753

Post by mobhar »

It won't work, because you are actually creating a string instead of an array. The "setOptions" method needs a row as an array.

So, the closest approach for your case is to use "ExecuteRows" global function. It will return a row as an array. See the "Example 5" from "Lookup_Selecting" under "Server Events and Client Scripts" topic from PHPMaker Help menu for more info.

To do that, then you need to create a new table to store the Year values. Let's say the table name is "years" and it only has one field named "Year" and define it as Primary Key, then simply use this code:

if ($fld->Name == "yearfield"){
$fld->Lookup->setOptions(ExecuteRows("SELECT Year, Year FROM years WHERE Year <= " . date("Y"))); // Use data from ExecuteRows
}

Of course, there are pros and cons for the approach above.

Pros:

  • No need to write more code in order to create a loop; just get the values from Database using one single line of code.
  • You may enhance the "years" table; for example by adding another field(s) to be used as the Displayed Field.
  • You may dynamically add your business-logic into that SELECT SQL.

Cons:

  • You need to create additional table to store the values.
  • You need to maintain the values in Database and make sure the Year value of each new year has been inserted properly.

Post Reply