Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do while iteration

Status
Not open for further replies.

murushanmugham

IS-IT--Management
Jan 2, 2003
33
I have a table with fields line1, line2, line3 ... upto line50.
How do i display the lines in a loop incrementing the counter ctr. Can i have the value of ctr with the field name like line1 as line(ctr).


I have a routine like this

$ctr=1
do
{
if(!$line1=="")
{
if(is_numeric($t1)
{
?>
<font color=maroon size=5><? echo '<pre>' .$line1. '</pre>';?></font>
<?
}
++$ctr;
while($ctr<41);

 
You probably want to use a for loop here and store your information in arrays:
Code:
<?
 for ($i=1;$i<41;$i++) 
   if ($line[$i] != '' && is_numeric($t[$i]))
     echo '<span style="color:maroon; font-size:200%"><pre>' . $line[$i] . '</pre></span>';
?>

Ken
 
The other option is to use variable variables. You can do something like this by saying

Code:
for ($i=1; $i<41; $i++)
{
  $line_var = "line" . $i;
  $table_var = "t" . $i;
  if (!$$line_var=="")
  {
    if(is_numeric($$table_var))
    {
      ?>
        <font color=maroon size=5>
        <?echo '<pre>' .$$line_var. '</pre>';?></font>
      <?
    } // end if(is_numeric)
  } // end if not empty
} // end for loop

Notice that the variable variables allow you to actually create a dynamic variable name. Therefore, however many lines you pass it, you can call them line1, line2, ... and you don't have to try to pass an array. Then you can actually create a loop that handles the variables as line1, line2, .... The variable name can actually be built in your loop. I find this really useful when I don't know exactly how many variables or table rows I will be passing like in a form that allows you to add an undefined number of lines.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top