[FX.php List] Creating Reusable Code

Jonathan Schwartz jonathan at eschwartz.com
Sun Sep 10 23:57:08 MDT 2006


Actually, mine do too.  I dumbed it down to simplify the question.

But...mine aren't the same as yours.  Will have to think a bit about 
your assortment.

J




>Hmm my forms have 4 different field arrays
>
><input name="s[fieldname]" value="">
><input name="p[fieldname]" value="">
>
><input name="k[fieldname]" value=""> or <input name="s[-recid]" value="">
>
><input name="a" value="Add">
>
>s = submittable
>p = parsing required
>k = key
>a = action
>
>ggt667
>
>On 9/10/06, Jonathan Schwartz <jonathan at eschwartz.com> wrote:
>>Oh!  OK. I get it.
>>
>>The foreach (loop?) walks the first array and uses the built in $key.
>>Then, at the same time, you grab the value from the other arrays
>>using the $key from the first array.  No need to do the foreach for
>>every array.  That makes sense.  And it works.
>>
>>By the way, I was able to get the hybrid Andy/Dan script to work just
>>before receiving your last email, using the counter.  The counter
>>doesn't hurt, but it isn't needed in this case either.  Just had to
>>start the counter at "0", not "1", to accomodate the way the arrays
>>work. ;-)
>>
>>So now, with this enhancement, I've managed to separate the data from
>>the script almost completely.  No I can re-purpose the script for new
>>projects and have it working in minutes...not days.
>>
>>That's a good thing. ;-)
>>
>>Thanks very much folks.
>>
>>I assume that this is the natural and desired progression of things
>>in order to do better and more efficient work?
>>
>>Jonathan
>>
>>
>>
>>At 9:33 PM -0400 9/9/06, Andy Gaunt wrote:
>>>Jonathon,
>>>
>>>Close.
>>>
>>>Try this instead.
>>>
>>><?php
>>>        $fieldnames = array('A', 'B','J');
>>>        $fieldwidths = array('5', '10','5');
>>>        $fieldlabels = array('this', 'that','the_other');
>>>
>>>?>
>>><tr>
>>><?php
>>>foreach ($fieldnames as $key=>$fieldname)
>>>{
>>>    echo '<td><input name="'.$fieldname.'" size="'.$fieldwidths[$key].'"
>>>id="'.$fieldlabels[$key].'"  >';
>>>} echo '</td>'; ?>
>>>   </tr>
>>>
>>>
>>>As Dan pointed out, simple arrays have number keys, so while using a single
>>>foreach loop on one array can yield values from any other arrays (as long as
>>>the values are matched correctly).
>>>
>>>Here the $key is the numeric value of the position in the array, so you
>>>don't even need a counter variable.
>>>
>>>HTH
>>>
>>>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 9:06 PM
>>>To: FX.php Discussion List
>>>Subject: Re: [FX.php List] Creating Reusable Code
>>>
>>>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
>>>
>>>_______________________________________________
>>>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