LemonInflux Posted October 19, 2007 Share Posted October 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73983-solved-variables-in-functions/ Share on other sites More sharing options...
pocobueno1388 Posted October 19, 2007 Share Posted October 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73983-solved-variables-in-functions/#findComment-373435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.