0

Tried fetching all data from table, but neither it showing data nor any error. I know I've missed something but I don't know exactly what it is.

<?php
$st = $pdo->prepare("SELECT plan_id, plan_name, zone_id, operator_price, selling_price, validity, plan_type FROM plan_tbl");
$st->execute();
$cnt=$st->rowCount();
$user_record=$st->fetchAll();
}
catch (PDOException $ex) {
    echo  $ex->getMessage();
}
$rowdata="<tbody>";
foreach($plan_record as $plan_record1){
    $rowdata.="<tr>";
    $sr_no = $plan_record1['plan_id'];
    $plan_name = $plan_record1['plan_name'];
    //$user_fullname = $user_record1['fname'] . " " . $user_record1['mname'] . " " . $user_record1['lname'];
    //echo $user_fullname;
    $zone_id = $plan_record1['zone_id'];
    $operator_price = $plan_record1['operator_price'];
    $selling_price = $plan_record1['selling_price'];
    $validity = $plan_record1['validity'];
    $plan_type = $plan_record1['plan_type'];
    $icon="<i class='fa fa-pencil-square-o pad' aria-hidden='true'></i><i class='fa fa-times pad' aria-hidden='true'></i>";
    $rowdata.="</tr>";
}
?>
Uwe
  • 41,420
  • 11
  • 90
  • 134
shyamm
  • 506
  • 6
  • 17

2 Answers2

1

I would use meaningful variable names such as $plans and $plan. Notice also, that you are no

<?php
$st = $pdo->prepare("SELECT plan_id, plan_name, zone_id, operator_price, selling_price, validity, plan_type FROM plan_tbl");
$st->execute();
$cnt=$st->rowCount();
$plans=$st->fetchAll();
}
catch (PDOException $ex) {
    echo  $ex->getMessage();
}
$rowdata="<tbody>";
foreach($plans as $plan){
    $rowdata.="<tr>";
    $rowdata .= "<td>{$plan['plan_id']}</td>";
    $rowdata .= "<td>{$plan['plan_name']}</td>";
    $rowdata .= "<td>{$plan['zone_id']}</td>";
    $rowdata .= "<td>{$plan['operator_price']}</td>";
    $rowdata .= "<td>{$plan['selling_price']}</td>";
    $rowdata .= "<td>{$plan['validity']}</td>";
    $rowdata .= "<td>{$plan['plan_type']}</td>";
    $rowdata .= "<td><i class='fa fa-pencil-square-o pad' aria-hidden='true'></i><i class='fa fa-times pad' aria-hidden='true'></i></td>";
    $rowdata.="</tr>";
}
$rowdata .= "</tbody>";

echo $rowdata;

You also need to do something with the data in the loop otherwise you are overwriting it each time in the loop. For example, you might want to change each of the lines so they look like this:

$rowdata .= "<td>{$plan['plan_id']}</td>";

instead of

$sr_no = $plan['plan_id'];

After exit the loop make sure you display it with:

echo $rowdata

I have also taken out you closing ?> tag as this can cause some problems if it's at the very end of your file.

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • 1
    thanx! now my records are fetched but not displayed in table! you got my prblm right?? – shyamm Jan 28 '17 at 07:12
  • Yes, the data stored in $sr_no would get erased each time it went back into the foreach loop. So, you would only end up with the very last value retrieved in the $sr_no variable. Instead, just append the data to $rowdata like I showed you above. – kojow7 Jan 28 '17 at 07:15
  • got it!! now works perfect!! thank you!! i missed following section! – shyamm Jan 28 '17 at 07:17
  • $rowdata.="".$sr_no.""; $rowdata.="".$plan_name.""; $rowdata.="".$user_fullname.""; $rowdata.="".$zone_id.""; $rowdata.="".$operator_price.""; $rowdata.="".$selling_price.""; $rowdata.="".$validity.""; $rowdata.="".$plan_type.""; $rowdata.="".$icon.""; – shyamm Jan 28 '17 at 07:17
  • Looks good. Though you do not need to store into a variable first. Just store in $rowdata directly. I cleaned up my code a bit to show you how. – kojow7 Jan 28 '17 at 07:24
  • 1
    that was perfect code!! thanx for help! – shyamm Jan 28 '17 at 07:29
  • It's also perfectly fine to place variables inside of double quotes. For example, instead of doing this: $rowdata = "" . $sr_no. "". You can do this: $rowdata = "$sr_no"; If you have something a bit more complex such as letters or numbers right next to the variable use braces: $rowdata = "{$sr_no}456"; – kojow7 Jan 28 '17 at 07:32
  • 1
    I would also take out try-catch because it's [harmful](https://phpdelusions.net/delusion/try-catch) and rowCount because it's [useless](https://phpdelusions.net/pdo#count) – Your Common Sense Jan 28 '17 at 08:06
  • Agreed, unless rowCount is being used later in the code. There was no "try" so I am assuming this was only part of the code. But, yes, the try/catch should be removed. – kojow7 Jan 29 '17 at 04:23
0

use $user_record instead of $plan_record because you didn't declare it in your code

foreach($user_record as $plan_record1){
    $rowdata.="<tr>";
    $sr_no = $plan_record1['plan_id'];
    $plan_name = $plan_record1['plan_name'];
    //$user_fullname = $user_record1['fname'] . " " . $user_record1['mname'] . " " . $user_record1['lname'];
    //echo $user_fullname;
    $zone_id = $plan_record1['zone_id'];
    $operator_price = $plan_record1['operator_price'];
    $selling_price = $plan_record1['selling_price'];
    $validity = $plan_record1['validity'];
    $plan_type = $plan_record1['plan_type'];
    $icon="<i class='fa fa-pencil-square-o pad' aria-hidden='true'></i><i class='fa fa-times pad' aria-hidden='true'></i>";
    $rowdata.="</tr>";
}
kojow7
  • 10,308
  • 17
  • 80
  • 135
Rushil K. Pachchigar
  • 1,263
  • 2
  • 21
  • 40