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!

Anyone comfortable using variable variables?

Status
Not open for further replies.

clonny2

MIS
Jan 28, 2003
470
US
I have a section of code i use over and over and I would like to turn it into a function passing in the args.

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.
4: also $query, $result, $numrows will need to be renamed as a variable.

I already wrote this reusing the code over and over with cut and paste. I would like to make it cleaner using a function.  
 
I'll answer, but out of order.

3: You can do this one any one of four ways: [ul][li]have the function return the value of $display_block ([/li][li]hand the function a variable via pass-by-reference ([/li][li]use the global keyword ( to use a copy of $display_block which is outside the function;[/li][li]or reference the $GLOBALS superglobal array ( as $GLOBALS['display_block'] to instantiate the variable $display_block permanently outside the function.[/li][/ul]I would simply return the string.

2: I don't understand why you need to pass this variable in or out.

4: Those are variables.

1: You are passing in the query-string. Want the best answers? Ask the best questions: TANSTAAFL!
 
Let me see if I can clarify,

I am creating a table that has several db-driven drop down select boxes and each one is created the same way with a slight query string diff. I changed the variable names slightly to match each drop down, however this has produced alot of cut and paste with slight changes. I would like to make it a reusable function so that i can just call the function and pass the parameters so the code can be condensed and cleaned up.
He is some of the old code that I want to use this function:

//Auto-Auto=100F
$aa_query = (&quot;SELECT * FROM modes where speed !='auto'&quot;);
$aa_result = @pg_query ($conn,$aa_query);
if (!$aa_result) { echo (&quot;Can't make query&quot;); exit; }

$aa_numrows = @pg_num_rows ($aa_result) ;

if (!$aa_numrows) { echo (&quot;Can't get numrows&quot;); exit; }

$iLoop =0;

while ($iLoop < $aa_numrows) {
$aa_types = pg_fetch_result ($aa_result,$iLoop,0);

$aa_option_block .= &quot;<option value='$aa_types'>$aa_types</option>&quot;

$iLoop ++:

$aa_display_block =&quot; <select name=\&quot;eaa\&quot;>
$aa_option_block </select>&quot;;


I use this over and over and over. I am trying to turn that into a function that I can 1: pass the query string 2: return the display_block.

The HTML is a table
<FORM>
<TABLE>
<TR>
<TD><?php echo $display_block; ?> </TD>
</TR>
</TABLE>
</FORM>
But there are lots more cells, etc.. But that is the jist of it. Since it is a form, each cell needs to have a different name for the select statement becaue they are indepdant form items.

 
I would create a function which accepts as its input parameter the SQL query string you want to use, which then queries the database and outputs the HTML necessary to create the <option>...</option> tags of your <SELECT> element.

You could then call it as:

print '<SELECT name=&quot;foo&quot;>';

output_options (&quot;SELECT * FROM modes where speed !='auto'&quot;);

print '</SELECT>'; Want the best answers? Ask the best questions: TANSTAAFL!
 
And if you want to wrap it all into one, just do an output_select(&quot;foo&quot;, &quot;SELECT....&quot;);

then output_select looks something like
function output_select($var_name, $query_string) {
echo '<SELECT name=&quot;'.$var_name.'&quot;>';
....
echo '</SELECT>';
}

Just my way way of doing it...

-Rob
 
What I've been doing with my site involves using two files, one is a &quot;include&quot; file which contains all the class and/or functions that are used on a regular basis. That way, you can call that same function from any page on your site:) But, as far as creating a function, from what I've seen on your post, you've already got it set up as a function. Try putting it in &quot;../include/pages.inc&quot; then at the top of your script
include(&quot;../include/pages.inc&quot;);
In that include file, create a class.....
class pages
{
Your_Function($param)
{
various code goes here;
return $result;
}
}
In the page that you are wanting to use this on, before you can use it.......
$welcome=new Pages();
Then, when you want to call that function....
$rs=$welcome->Your_Function($param);
The nice thing about using classes is that you can also set up global variables which can be accessed from any page as well:)
I realize this is a bit sketchy, but hope it helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top