[FX.php List] Creating Reusable Code

Jonathan Schwartz jonathan at eschwartz.com
Sat Sep 9 19:05:48 MDT 2006


Hmmmm...

So I'm asking classic questions now.  I like the sound of that. Other 
have a different name for that. ;-)

HOWEVER....I don't see how your example will allow me to pull the 
values out of several arrays at a time to compose the single (input) 
field I need.  There's no mention of the second and third arrays.

Let me think out loud...

I have multiple arrays, each with 10 values:
	$fieldnames = array('A', 'B',...'J');
	$fieldwidths = array('5', '10',...'5');
	$fieldlabels = array('this', that',... 'the_other');

I need to construct the following 10 input tags in html:
	<input name="A" size="5" id="this type="text">
	<input name="B" size="10" id="that" type="text">
	...
	<input name="J" size="5" id="the_other" type="text">


If I go back to Andy Gaunt's suggestion, but insert your array suggestion...

<?php
echo '<tr  class="ver10" valign="top">';
$total = 10;
$counter = 1;
while ($counter <= $total) {
	echo '<td><input name="'$fieldnames[$ counter].'" type="text"
id="'.$fieldlabels[$ counter].'" size="'.$fieldwidths[$ counter].'"></td>';
	$ counter ++; 
}
echo '</tr>';
?>

Does this work, in principle?  I'm sure that the syntax is off.

Jonathan



At 8:37 PM -0400 9/9/06, DC wrote:
>right, great question!
>
>that's a classic array usage question. you have to use the numeric 
>keys that come along for free in every simple array (associative 
>arrrays are arrays where the keys are 'named') or you can use 
>external counter variables to keep the multiple arrays in sync on 
>output if keys are not available.
>
><tr>
><? foreach {
>($fieldnames as $key=>$fieldname);
>echo '<td><input name="'.$fieldname.'" size="'.$fieldwidth[$key].'"  >';
>}
>echo '></td>';
>?>
></tr>
>
>cheers,
>dan
>
>On Sep 9, 2006, at 8:02 PM, Jonathan Schwartz wrote:
>
>>Dan,
>>
>>Thanks for the tip.
>>
>>I switched over to creating arrays to hold the 10 values in 2 categories.
>>
>>And while I was able to use your suggested script to echo out the 
>>values in the first array, my success quickly went down the tubes 
>>when trying to repeat that process for the other three arrays, 
>>while attempting insert the values into a single line.
>>
>>I know this is wrong, but this is my last attempt before I ran out 
>>of patience:
>>
>><tr>
>><? foreach {
>>($fieldnames as $fieldname);
>>($fieldwidths as $fieldwidth);
>>echo '<td><input name="'.$fieldname.'" size="'.$fieldwidth.'"  >';
>>}
>>echo '></td>';
>>?>
>></tr>
>>
>>In short, I'm looking to extract the first value from each array 
>>for the first line, the second value from each array for the second 
>>line...for 10 lines.
>>
>>How does one extract one value from multiple arrays and then 
>>compile a single line of html based on those value?
>>
>>This is what I get for trying to be efficient. ;-)
>>
>>J
>>
>>>jonathan,
>>>
>>>unless your field names are purposefully named with number 
>>>endings, why don't you simplify it and remove the need for 
>>>$counter and while loop. it looks like you are trying to create 
>>>variable names on the fly by appending a number... if you really 
>>>want to do it this way you need to use 'varaible variables' but 
>>>they are pretty weird so it's best to keep it simple. try double 
>>>dollar sign $$term instead of \$term. but, really you should read 
>>>up on variable variables to get it right.
>>>
>>>if you want to make it simple, change the approach and make a 
>>>field name array:
>>>just put the field names you want in an array at the top of the 
>>>page and iterate over the array to get your field names out into 
>>>the table. same thing with any fieldwidths or field values you 
>>>want. just make sure the arrays are all the same size.
>>>
>>>so instead of:
>>>
>>>><? $term1 = "fieldnamesoand so"; ?> ... etc...
>>>
>>>use:
>>><?php
>>>$fields_array = array('list', 'of', 'field', 'names','here');
>>>foreach ($fields_array as $fieldname) {
>>>	echo '<td><input name="'.$fieldname.'"></td>';
>>>{
>>>?>
>>>
>>>HTH,
>>>dan
>>>
>>>On Sep 9, 2006, at 6:06 PM, Jonathan Schwartz wrote:
>>>
>>>>Andy,
>>>>
>>>>Thanks for the help.
>>>>
>>>>I understand the approach now, but can't get the code to work.  I 
>>>>reworked it a couple dozen times and still am getting the literal 
>>>>value of "$term1" instead of the value of the variable, 
>>>>previously defined on the page.
>>>>
>>>>From the resulting html source code:
>>>><tr>
>>>><td><input name="$term1"></td><td><input name="$term2"></td>
>>>>
>>>>
>>>>Here's my current php code:
>>>><? $term1 = "fieldnamesoand so"; ?>
>>>><snip>
>>>><snip>
>>>><tr>
>>>><?php
>>>>$total = 11;
>>>>$counter = 1;
>>>>while ($counter <= $total) {
>>>>	$beginning="\$term";
>>>>	$termcurrent = $beginning.$counter;
>>>>	echo '<td><input name="'.$termcurrent.'"></td>';
>>>>	$counter ++; }
>>>>echo "</tr>";
>>>>?>
>>>>
>>>>
>>>>
>>>>Jonathan
>>>>
>>>>>Jonathon,
>>>>>
>>>>>Try the following.
>>>>>
>>>>><?php
>>>>>echo '<tr  class="ver10" valign="top">';
>>>>>$total = 10;
>>>>>$counter = 1;
>>>>>while ($counter <= $total) {
>>>>>	echo '<td><input name="'.$term.$ counter.'" type="text"
>>>>>id="'.$term.$ counter.'" size="'.$termwidth.$ counter.'"></td>';
>>>>>	$ counter ++; }
>>>>>echo '</tr>';
>>>>>?>
>>>>>
>>>>>Changing the value for the while statement will let you increase 
>>>>>or decrease
>>>>>the number of cells created.
>>>>>
>>>>>Another option is a 'for' loop
>>>>>
>>>>>
>>>>>$total = 20;
>>>>>echo '<tr  class="ver10" valign="top">';
>>>>>for($counter=0; $counter < $total; $counter++) {
>>>>>	echo '<td><input name="'.$term.$counter.'" type="text"
>>>>>id="'.$term.$counter.'" size="'.$termwidth.$counter.'"></td>';
>>>>>}
>>>>>echo '</tr>';
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>Andy Gaunt
>>>>>Office: 321.206.3658
>>>>>Mobile: 407.810.4722
>>>>>andy at fmpug.com
>>>>>http://www.fmpug.com
>>>>>2006 FileMaker Excellence Award Winner
>>>>>Recipient of FileMaker's 2005 "Mad Dog" Public Relations Award
>>>>>
>>>>>For chapter locations, dates & times please visit the website at
>>>>>http://www.fmpug.com If you can make it to a meeting, please RSVP at
>>>>>http://www.fmpug.com/rsvp.php
>>>>>
>>>>>-----Original Message-----
>>>>>From: fx.php_list-bounces at mail.iviking.org
>>>>>[mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of 
>>>>>Jonathan Schwartz
>>>>>Sent: Saturday, September 09, 2006 11:54 AM
>>>>>To: FX.php Discussion List
>>>>>Subject: [FX.php List] Creating Reusable Code
>>>>>
>>>>>Hi Folks,
>>>>>
>>>>>I'm attempting to get more efficient with scripting.
>>>>>
>>>>>My first step was to remove hard-code field names from my favorite
>>>>>search and display script, creating variables for each field and
>>>>>defining those variables at the top of the document.  This works
>>>>>great.  Now, I can re-purpose the script for another db and layout by
>>>>>just changing the field variables at the top of the page, instead of
>>>>>ripping apart the entire document.
>>>>>
>>>>>The second area I'm looking at is to reduce the repetition of
>>>>>repetitive code (example below actually has 10+ iterations) and use
>>>>>an iterative loop to accomplish the same, counting from 1 to n lines.
>>>>>
>>>>>Is that possible/reasonable/reccomended/etc?
>>>>>
>>>>>Thanks
>>>>>
>>>>>Jonathan
>>>>>
>>>>>
>>>>><tr  class="ver10" valign="top">
>>>>>	<td><input name="<? echo $term1; ?>" type="text" id="<? echo
>>>>>$term1; ?>" size="<? echo $termwidth1; ?>"></td>
>>>>>	<td><input name="<? echo $term2; ?>" type="text" id="<? echo
>>>>>$term2; ?>" size="<? echo $termwidth2; ?>"></td>
>>>>>	<td><input name="<? echo $term3; ?>" type="text" id="<? echo
>>>>>$term3; ?>" size="<? echo $termwidth3; ?>"></td>
>>>>>--
>>>>>
>>>>>Jonathan Schwartz
>>>>>FileMaker 8 Certified  Developer
>>>>>Associate Member, FileMaker Solutions Alliance
>>>>>Schwartz & Company
>>>>>jonathan at eschwartz.com
>>>>>http://www.eschwartz.com
>>>>>http://www.exit445.com
>>>>>415-381-1852
>>>>>
>>>>>_______________________________________________
>>>>>FX.php_List mailing list
>>>>>FX.php_List at mail.iviking.org
>>>>>http://www.iviking.org/mailman/listinfo/fx.php_list
>>>>>
>>>>>
>>>>>_______________________________________________
>>>>>FX.php_List mailing list
>>>>>FX.php_List at mail.iviking.org
>>>>>http://www.iviking.org/mailman/listinfo/fx.php_list
>>>>
>>>>
>>>>--
>>>>
>>>>Jonathan Schwartz
>>>>FileMaker 8 Certified  Developer
>>>>Associate Member, FileMaker Solutions Alliance
>>>>Schwartz & Company
>>>>jonathan at eschwartz.com
>>>>http://www.eschwartz.com
>>>>http://www.exit445.com
>>>>415-381-1852
>>>>
>>>>_______________________________________________
>>>>FX.php_List mailing list
>>>>FX.php_List at mail.iviking.org
>>>>http://www.iviking.org/mailman/listinfo/fx.php_list
>>>
>>>_______________________________________________
>>>FX.php_List mailing list
>>>FX.php_List at mail.iviking.org
>>>http://www.iviking.org/mailman/listinfo/fx.php_list
>>
>>
>>--
>>
>>Jonathan Schwartz
>>FileMaker 8 Certified  Developer
>>Associate Member, FileMaker Solutions Alliance
>>Schwartz & Company
>>jonathan at eschwartz.com
>>http://www.eschwartz.com
>>http://www.exit445.com
>>415-381-1852
>>
>>_______________________________________________
>>FX.php_List mailing list
>>FX.php_List at mail.iviking.org
>>http://www.iviking.org/mailman/listinfo/fx.php_list
>
>_______________________________________________
>FX.php_List mailing list
>FX.php_List at mail.iviking.org
>http://www.iviking.org/mailman/listinfo/fx.php_list


-- 

Jonathan Schwartz
FileMaker 8 Certified  Developer
Associate Member, FileMaker Solutions Alliance
Schwartz & Company
jonathan at eschwartz.com
http://www.eschwartz.com
http://www.exit445.com
415-381-1852



More information about the FX.php_List mailing list