我正在编写一个程序,从mysql数据库中提取数据并将其显示在表格上,但我得到的标签不止一次。如何让它显示一个“普通”表格,标签只出现在顶部一次。
代码如下:
include('connection.php');
for ($x = 2; $x <= 4; $x++) {
$crop_farmer = mysql_fetch_array(mysql_query("SELECT crop FROM farmer where mem_id = '$x'"));
$crop_farmer = $crop_farmer[0];
$et0 = mysql_fetch_array(mysql_query("SELECT et0 FROM weather where timedate = 0"));
$et0 = $et0[0];
$texture = mysql_fetch_array(mysql_query("SELECT texture FROM farmer where mem_id = '$x'"));
$texture = $texture[0];
$contact = mysql_fetch_array(mysql_query("SELECT phone FROM farmer where mem_id = '$x'"));
$contact = $contact[0];
$farmerID = mysql_fetch_array(mysql_query("SELECT mem_id FROM farmer where mem_id = '$x'"));
$farmerID = $farmerID[0];
$area_farmer = mysql_fetch_array(mysql_query ("SELECT area FROM farmer WHERE mem_id = '$x'"));
$area_farmer = $area_farmer[0];
$depth = mysql_fetch_array(mysql_query("SELECT rootdepth FROM mad_depth where crop = '$crop_farmer'"));
$depth = $depth[0];
$mad = mysql_fetch_array(mysql_query("SELECT mad FROM mad_depth where crop = '$crop_farmer'"));
$mad = $mad[0];
$kc = mysql_fetch_array(mysql_query("SELECT kcd FROM crop where crop = '$crop_farmer'"));
$kc = $kc[0];
$am = mysql_fetch_array(mysql_query("SELECT am FROM soil where texture = '$texture'"));
$am = $am[0];
$IC = ($am * $depth * $mad)/($kc * $et0);
$IC = number_format($IC, 0);
$DRT = $et0 * $kc * 1.125*60;
$DRT = number_format($DRT, 0);
$RN = mysql_fetch_array(mysql_query("SELECT nd FROM crop WHERE crop = '$crop_farmer'"));
$RN = $RN[0];
$RP = mysql_fetch_array(mysql_query("SELECT pd FROM crop WHERE crop = '$crop_farmer'"));
$RP = $RP[0];
$RK = mysql_fetch_array(mysql_query("SELECT kd FROM crop WHERE crop = '$crop_farmer'"));
$RK = $RK[0];
$N = $RN * $area_farmer/23;
$N = number_format($N, 2);
$P = $RP * $area_farmer/23;
$P = number_format($P, 2);
$K = $RK * $area_farmer/30;
$K = number_format($K, 2);
echo "<table border='1'>";
echo "<tr> <th>Contact</th> <th>Farmer ID</th> <th>Crop</th> <th>DRT (Minutes)</th> <th>IC (Days)</th> <th>Urea(50 Kg Bag(s))</th> <th>TSP(50 Kg Bag(s))</th> <th>MOP(50 Kg Bag(s))</th>";
echo '<tr><td>'.$contact, "</td><td>".$farmerID."</td><td>" .$crop_farmer."</td><td>" .$DRT."</td><td>".$IC ."</td><td>" . $N."</td><td>". $P."</td><td>". $K;
echo "</td></tr>";
echo "</table";
}
任何帮助将不胜感激。
答案 0 :(得分:0)
在循环之外开始和结束表格:
<?php
include('connection.php');
echo "<table border='1'>";
echo "<tr> <th>Contact</th> <th>Farmer ID</th> <th>Crop</th> <th>DRT (Minutes)</th> <th>IC (Days)</th> <th>Urea(50 Kg Bag(s))</th> <th>TSP(50 Kg Bag(s))</th> <th>MOP(50 Kg Bag(s))</th>";
for ($x = 2; $x <= 4; $x++){
$crop_farmer = mysql_fetch_array(mysql_query("SELECT crop FROM farmer where mem_id = '$x'"));
$crop_farmer = $crop_farmer[0];
$et0 = mysql_fetch_array(mysql_query("SELECT et0 FROM weather where timedate = 0"));
$et0 = $et0[0];
$texture = mysql_fetch_array(mysql_query("SELECT texture FROM farmer where mem_id = '$x'"));
$texture = $texture[0];
$contact = mysql_fetch_array(mysql_query("SELECT phone FROM farmer where mem_id = '$x'"));
$contact = $contact[0];
$farmerID = mysql_fetch_array(mysql_query("SELECT mem_id FROM farmer where mem_id = '$x'"));
$farmerID = $farmerID[0];
$area_farmer = mysql_fetch_array(mysql_query ("SELECT area FROM farmer WHERE mem_id = '$x'"));
$area_farmer = $area_farmer[0];
$depth = mysql_fetch_array(mysql_query("SELECT rootdepth FROM mad_depth where crop = '$crop_farmer'"));
$depth = $depth[0];
$mad = mysql_fetch_array(mysql_query("SELECT mad FROM mad_depth where crop = '$crop_farmer'"));
$mad = $mad[0];
$kc = mysql_fetch_array(mysql_query("SELECT kcd FROM crop where crop = '$crop_farmer'"));
$kc = $kc[0];
$am = mysql_fetch_array(mysql_query("SELECT am FROM soil where texture = '$texture'"));
$am = $am[0];
$IC =($am * $depth * $mad)/($kc * $et0);
$IC = number_format($IC,0);
$DRT = $et0 * $kc * 1.125*60;
$DRT = number_format($DRT,0);
$RN = mysql_fetch_array(mysql_query("SELECT nd FROM crop WHERE crop = '$crop_farmer'"));
$RN = $RN[0];
$RP = mysql_fetch_array(mysql_query("SELECT pd FROM crop WHERE crop = '$crop_farmer'"));
$RP = $RP[0];
$RK = mysql_fetch_array(mysql_query("SELECT kd FROM crop WHERE crop = '$crop_farmer'"));
$RK = $RK[0];
$N = $RN * $area_farmer/23;
$N = number_format($N,2);
$P = $RP * $area_farmer/23;
$P = number_format($P,2);
$K = $RK * $area_farmer/30;
$K = number_format($K,2);
echo '<tr><td>'.$contact,"</td><td>".$farmerID."</td><td>" .$crop_farmer."</td><td>" .$DRT."</td><td>".$IC ."</td><td>" . $N."</td><td>". $P."</td><td>". $K;
echo "</td></tr>";
}
echo "</table>";
?>
答案 1 :(得分:0)
移动包含开口的行,标题行和&amp; foreach循环中的标记
<?php
include('connection.php');
echo "<table border='1'>";
echo "<tr> <th>Contact</th> <th>Farmer ID</th> <th>Crop</th> <th>DRT (Minutes)</th> <th>IC (Days)</th> <th>Urea(50 Kg Bag(s))</th> <th>TSP(50 Kg Bag(s))</th> <th>MOP(50 Kg Bag(s))</th>";
for ($x = 2; $x <= 4; $x++){
$crop_farmer = mysql_fetch_array(mysql_query("SELECT crop FROM farmer where mem_id = '$x'"));
$crop_farmer = $crop_farmer[0];
$et0 = mysql_fetch_array(mysql_query("SELECT et0 FROM weather where timedate = 0"));
$et0 = $et0[0];
$texture = mysql_fetch_array(mysql_query("SELECT texture FROM farmer where mem_id = '$x'"));
$texture = $texture[0];
$contact = mysql_fetch_array(mysql_query("SELECT phone FROM farmer where mem_id = '$x'"));
$contact = $contact[0];
$farmerID = mysql_fetch_array(mysql_query("SELECT mem_id FROM farmer where mem_id = '$x'"));
$farmerID = $farmerID[0];
$area_farmer = mysql_fetch_array(mysql_query ("SELECT area FROM farmer WHERE mem_id = '$x'"));
$area_farmer = $area_farmer[0];
$depth = mysql_fetch_array(mysql_query("SELECT rootdepth FROM mad_depth where crop = '$crop_farmer'"));
$depth = $depth[0];
$mad = mysql_fetch_array(mysql_query("SELECT mad FROM mad_depth where crop = '$crop_farmer'"));
$mad = $mad[0];
$kc = mysql_fetch_array(mysql_query("SELECT kcd FROM crop where crop = '$crop_farmer'"));
$kc = $kc[0];
$am = mysql_fetch_array(mysql_query("SELECT am FROM soil where texture = '$texture'"));
$am = $am[0];
$IC =($am * $depth * $mad)/($kc * $et0);
$IC = number_format($IC,0);
$DRT = $et0 * $kc * 1.125*60;
$DRT = number_format($DRT,0);
$RN = mysql_fetch_array(mysql_query("SELECT nd FROM crop WHERE crop = '$crop_farmer'"));
$RN = $RN[0];
$RP = mysql_fetch_array(mysql_query("SELECT pd FROM crop WHERE crop = '$crop_farmer'"));
$RP = $RP[0];
$RK = mysql_fetch_array(mysql_query("SELECT kd FROM crop WHERE crop = '$crop_farmer'"));
$RK = $RK[0];
$N = $RN * $area_farmer/23;
$N = number_format($N,2);
$P = $RP * $area_farmer/23;
$P = number_format($P,2);
$K = $RK * $area_farmer/30;
$K = number_format($K,2);
echo '<tr><td>'.$contact,"</td><td>".$farmerID."</td><td>" .$crop_farmer."</td><td>" .$DRT."</td><td>".$IC ."</td><td>" . $N."</td><td>". $P."</td><td>". $K;
echo "</td></tr>";
}
echo "</table";
?>
(注意:此代码还存在其他问题,例如,您没有使用SQL绑定变量&amp;您的标题行未正确关闭)