Hi, I am working on a PHP page where I have 5 rate, 5 qty and 5 amount fields. When I am define all the fields manually, i.e. defining each field with a separate filed name, field id then it is working. But when I have used a loop to do that for me I failed. Is it possible to create the same using loop, or do I have to do it manually?


Sample code

<tr>
<td>

<input name="rate1" id="rate1" type="text" >

</td>

<td>

<input name="qty1" id="qty1" type="text"  >

</td>

<td>

<input name="amt1" id="amt1" type="text" readonly="readonly">

</td>

</tr>
<tr>
<td>

<input name="rate2" id="rate2" type="text" onkeyup="integeronly(this)" >

</td>

<td>

<input name="qty2" id="qty2" type="text" onkeyup="integeronly(this)" >

</td>

<td>

<input name="amt2" id="amt2" type="text" readonly="readonly">

</td>

</tr>

Can't it be written like this -

<?php
$i=0;
while ($i <= 10)
{
?>
<tr>
<td>

<input name="rate$i" id="rate$i" type="text" onkeyup="integeronly(this)" >

</td>

<td>

<input name="qty$i" id="qty$i" type="text" onkeyup="integeronly(this)" >

</td>

<td>

<input name="amt$i" id="amt$i" type="text" readonly="readonly">

</td>

</tr>
<?php
$i++;
}
?>

Sure, but only if you place the PHP variables within PHP tags.

<input name="rate$i" id="rate$i" type="text" onkeyup="integeronly(this)" >

Should be

<input name="rate<?php echo $i; ?>" id="rate<?php echo $i; ?>" type="text" onkeyup="integeronly(this)" >

Still better, put the whole HTML code within PHP tags. It's much easier to read and to maintain.

<?php
for ($i = 0; $i <= 10; $i++)
{
	echo "
	<tr>
		<td>
			<input name='rate$i' id='rate$i' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='qty$i' id='qty$i' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='amt$i' id='amt$i' type='text' readonly='readonly'>
		</td>
	</tr>"
	;
}
?>

There is little that can be avoided of mixing html with your php in this instance however, I think the alternative control structures make for a nicer read than having your block of html encased in a single echo statement.

If you want to remove your php even more from your html, HEREDOC and NOWDOC (php 5.3 +) make this very easy with a little sprintf/printf usage.

There really isn't a right or wrong in this case, except your original example, but that was corrected by jtreminio.

<?php for( $i = 0; $i <= 10; $i++ ): ?>
	<tr>
		<td>
			<input name='rate<?php echo $i; ?>' id='rate<?php echo $i; ?>' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='qty<?php echo $i; ?>' id='qty<?php echo $i; ?>' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='amt<?php echo $i; ?>' id='amt<?php echo $i; ?>' type='text' readonly='readonly'>
		</td>
	</tr>
<?php endfor; ?>

If you want to take the chance you can use the shorttag echo, but ymmv:

<?php for( $i = 0; $i <= 10; $i++ ): ?>
	<tr>
		<td>
			<input name='rate<?=$i?>' id='rate<?=$i?>' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='qty<?=$i?>' id='qty<?=$i?>' type='text' onkeyup='integeronly(this)' >
		</td>
		<td>
			<input name='amt<?=$i?>' id='amt<?=$i?>' type='text' readonly='readonly'>
		</td>
	</tr>
<?php endfor; ?>

You could also use HEREDOC with the sprintf functions, *BUT* you must escape the '$' placeholder. This will work with php < 5.3.

<?php
$html = <<<HTML
<tr>
	<td>
		<input name='rate%1\$s' id='rate%1\$s' type='text' onkeyup='integeronly(this)' >
	</td>
	<td>
		<input name='qty%1\$s' id='qty%1\$s' type='text' onkeyup='integeronly(this)' >
	</td>
	<td>
		<input name='amt%1\$s' id='amt%1\$s' type='text' readonly='readonly'>
	</td>
</tr>

HTML;

for( $i=0; $i<=10; $i++ ){
	printf( $html, $i );
}

Finally if you have the power of php 5.3.0 + to utilize, you can use NOWDOC which will not have an issue with the dollar signs.

<?php
$html = <<<'HTML'
<tr>
	<td>
		<input name='rate%1$s' id='rate%1$s' type='text' onkeyup='integeronly(this)' >
	</td>
	<td>
		<input name='qty%1$s' id='qty%1$s' type='text' onkeyup='integeronly(this)' >
	</td>
	<td>
		<input name='amt%1$s' id='amt%1$s' type='text' readonly='readonly'>
	</td>
</tr>

HTML;

for( $i=0; $i<=10; $i++ ){
	printf( $html, $i );
}

Thanks to all. It is great to have so much useful reply within so short time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.