0

I am writing a program that extracts data from a mysql database and displays it on a table but i get the labels displaying more than once. How do i get it to display a "normal" table with labels appearing only once at the top.

the code is as follows:

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";
}

Any help will be appreciated.

marian0
  • 3,336
  • 3
  • 27
  • 37
Austin Roy
  • 19
  • 2
  • use `` and in loop put data in `` – Robert Jul 07 '15 at 13:10
  • For future reference, read how to post a "good" question: http://stackoverflow.com/help/how-to-ask – TurdPile Jul 07 '15 at 13:11
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 07 '15 at 13:14

2 Answers2

0

Start and end the table outside of the loop:

<?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>";
?>
TurdPile
  • 976
  • 1
  • 7
  • 21
0

Move the rows containing your opening , your header row & the tag out of your foreach loop

<?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";
?>

(Note: there are other issues with this code, e.g. you're not using SQL bind variables & your header row isn't closed properly)

Rob G
  • 592
  • 4
  • 18