在循环中将条件应用于列

时间:2014-02-13 06:56:55

标签: php

我有一个列名为DATE,TIME,A1,A2,A3,A4的表。我编写了一个循环,从表中获取完整的数据并构造了Html表。我想仅为列A1,A2,A3,A4应用公式。我不想将其应用于DATETIME列。循环如下:

$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>';

1 个答案:

答案 0 :(得分:1)

好的,这就是我在评论中说的,添加一个简单的if语句来检查你所使用的数字列应该做什么

$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>';