Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to turn dynamic select box into a function.

Status
Not open for further replies.

clonny2

MIS
Jan 28, 2003
470
US
Thanx in advance. :)

I am attempting to turn this piece of code into a reuseable function. I need to create several drop down select boxes from data pulled from postgresqldb. Getting the data is no trouble. I use the same piece of code over and over, just changing variable names. I would like to turn it into a function.

This is what I have so far:

function make_query ($query) {
$result = pg_query ($conn, $query);
$numrows = pg_num_rows ($result);
if (!$numrows) { echo ("Can't get numrows!"); exit; }

$iLoop=0;

while ( $iLoop < $numrows ) {
$var1 = pg_fetch_result ($result,$iLoop,0);
$option_block .=&quot;<option value='$var1'>$var1</option>&quot;;
$display_block = &quot;<select name=\&quot;$var2\&quot;>
$option_block </select>&quot;;

}

I need to pass in
1: The query string
2: var 2
3: The variable called $display_block will need to be passed in as a variable so i can call <?php echo $display_block; ?> and it will point to the correct information.

I already wrote this reusing the code over and over with cut and paste. I would like to make it cleaner using a function.

 
1. It seems like you already have that done.
2. Changing the function definition to something like [tt]function make_query($query, $var2)[/tt] should do it.
3. Put [tt]global $display_block;[/tt] as the first line of the function. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top