Jump to content

[SOLVED] Variables in functions?


LemonInflux

Recommended Posts

OK, basically, I've been looking through my self-written functions recently, and discovered a number of them are ridiculously big. So, I thought of an idea to free up some space, yet no idea how to do it. Basically, here's my idea:

 

Say, I have a function called queryForm() (I don't really, but it's an example). Within that, there are 3 textboxes: Name, email, and query. The form is displayed like this:

 

<table>

    <tr>

        <td>Name:</td>

        <td>*TEXTBOX*</a>

    </tr>

    <tr>

        <td>Email:</td>

        <td>*TEXTBOX*</a>

    </tr>

    <tr>

        <td>Query:</td>

        <td>*TEXTBOX*</a>

    </tr>

</table>

 

My idea is as follows. Have the function as,

queryForm('Name', 'Email', 'Query', 'output.php').

Then within the function, the function gets all the values in the function (separated by commas), and does the following:

first, outside the table, does this: <form action="$thelastvalue">, then has a loop like:

 

foreach($thing as $loop_item){

echo '<tr><td>'. $loop_item .'</td><td>*TEXTBOX WITH NAME'. $loop_item .'*';

}

 

But that's where I get stuck. I could write the function if I knew what values were going in, but I don't. I could probably use a count() to get the last item, but it's the bit about getting all the values from the query I get stuck on. Is this possible? Sorry if I'm unclear, just ask and I'll try and explain better.

Link to comment
Share on other sites

I just typed this up for you

 

<?php

function queryForm($field_array, $action){
   
   echo "<form action='$action' method='post'>";
   echo '<table>';
   
   foreach ($field_array as $field){
       echo '<tr>';
           echo "<td>$field:</td>";
           echo "<td><input type='text' name='$field'></td>";
       echo '</tr>';
   }
   
   echo '<td><input type="submit" name="submit" value="Submit"></td>';

   echo '</table>';
   echo '</form>';

}

?>

 

Then to use it, just do this

<?php

$fields = array("Name", "Email", "Query"); //<--You can put as many as you want.
queryForm($fields, 'script.php');

?>

 

You can put as many fields as you want in it, and it will automatically generate them all.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.