mark rammmy
mark rammmy

Reputation: 1498

Apply condition to column in a loop

I have a table with column names as DATE,TIME,A1,A2,A3,A4. I have written a loop which gets complete data from the table and constructs's Html table. I want to apply a formula only for the columns A1,A2,A3,A4. I don't want to apply it for the DATE and TIME column. The loop is given below:

$query1 = "select * from table"; 
$result1 = mysql_query($query1);
$i = 0; 
echo '<table border="1" style="border-collapse:collapse;"><tr>'; 
while ($i < mysql_num_fields($result1)) 
{ 
    $meta = mysql_fetch_field($result1, $i); 
    echo '<td>' . $meta->name . '</td>'; 
    $i = $i + 1; 
} 
echo '</tr>'; 
$i = 0; 
while ($row = mysql_fetch_row($result1)) 
{ 
    echo '<tr>';
    $count = count($row); 
    $y = 0; 
    while ($y < $count) 
    { 
        //Condition to apply formula for the columns A1,A2,A3,A4. I do not want to apply formula on the DATE and TIME columns
        $c_row = current($row); 
        echo '<td>' . $c_row . '</td>'; 
        next($row); 
        $y = $y + 1; 
    } 
    echo '</tr>'; 
    $i = $i + 1;
 } 
 echo '</table>';

Upvotes: 1

Views: 83

Answers (1)

Chitowns24
Chitowns24

Reputation: 960

Okay this is what I was saying on my comment adding a simple if statement to check what number column you are on should do the trick

$query1 = "select * from table"; 
$result1 = mysql_query($query1);
$i = 0; 
echo '<table border="1" style="border-collapse:collapse;"><tr>'; 
while ($i < mysql_num_fields($result1)) 
{ 
$meta = mysql_fetch_field($result1, $i); 
echo '<td>' . $meta->name . '</td>'; 
$i = $i + 1; 
} 
echo '</tr>'; 
$i = 0; 
while ($row = mysql_fetch_row($result1)) 
{ 
echo '<tr>';
$count = count($row); 
$y = 0; 
while ($y < $count) 
{ 
    //ADDED CODE BY ME
   if($y < 2)
   {
        //Code for Time and Date
    $c_row = current($row); 
    echo '<td>' . $c_row . '</td>'; 

    $y = $y + 1; 
   }
   if($y > 2)
   {
       //Code for A1, A2, A3, A4
    $c_row = current($row);
    $c_row = $c_row * 10; // This is now only applied to the columns after Date and Time


    echo '<td>' . $c_row . '</td>'; 

    $y = $y + 1; 
   }

 next($row);


} 
echo '</tr>'; 
$i = $i + 1;
} 
echo '</table>';

Upvotes: 1

Related Questions