Page 1 of 1

[SOLVED] Code repeatation : Advice

Posted: Sat May 14, 2005 8:15 am
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>

Posted: Sat May 14, 2005 8:29 am
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

Posted: Sat May 14, 2005 8:56 am
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";
  }

Posted: Sat May 14, 2005 9:26 am
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";

Posted: Sat May 14, 2005 9:27 am
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";