[SOLVED] Code repeatation : Advice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[SOLVED] Code repeatation : Advice

Post by facets »

Hi Gang,

I have the following code on a page that I have repeated ALOT of times (At least 250 (split into functions etc)).
My question is how could I code it without having to copy/paste this multiple time.
Obviously the Title (Dry Strength) and name (strength) change for each table.

Any ideas?

TIA, Wil

Code: Select all

<tr><td width=200px colspan=2 valign=top>Dry Strength</td>
<td width=50px colspan=2><input type='text' name='strength'SIZE="20"></td</tr>
Last edited by facets on Sat May 14, 2005 9:01 pm, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

a for or while loop

Code: Select all

$title=array('dry strength','wet strength', 'damp strength', 'moist strength');
$name = array('dry','wet', 'damp', 'moist');
for($x = 0; $x<count($title); $x++)
{
  echo "<tr><td width=\"200px\" colspan=\"2\" valign=\"top\">".$title[$]."</td>\n";
  echo <td width=\"50px\" colspan=\"2\"><input type=\"text\" name=\"".$name[$x]." SIZE=\"20\"></td></tr>\n";
}
or you can use a 2d array it you wish

http://uk.php.net/types.array
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

thanks for the swift reply.

I'm getting an error "Parse error: parse error, expecting `T_VARIABLE' or `'$'' in c:\program files\easyphp1-8\www\materialsregister\auaddsummary.php on line 243"
(Which is the first echo line).

Any ideas?

Code: Select all

$desc=array('Description','Basis Weight');
$name = array('$description','$basisWeight');
for($x = 0; $x<count($desc); $x++)
{
  echo "<tr><td width=\"200px\" colspan=\"2\" valign=\"top\">".$desc[$]."</td>\n";
  echo "<td width="50px\" colspan=\"2\">".$name[$x]."</td></tr>\n";
  }
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You forgot to escape one of your quotes.

..should be..

Code: Select all

echo "<td width=\"50px\" colspan=\"2\">".$name[$x]."</td></tr>\n";
Last edited by John Cartwright on Sat May 14, 2005 9:35 am, edited 1 time in total.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

You realize that this sets the array to text, right?

Code: Select all

$name = array('$description','$basisWeight');
Here's your error.
This:

Code: Select all

echo "<tr><td width=\"200px\" colspan=\"2\" valign=\"top\">".$desc[$]."</td>\n";
should be:

Code: Select all

echo "<tr><td width=\"200px\" colspan=\"2\" valign=\"top\">".$desc[$i]."</td>\n";
Post Reply