【问题标题】:Display data using while loop from SQL database php dpo into html table not working使用while循环从SQL数据库php dpo显示数据到html表中不起作用
【发布时间】:2017-12-19 12:22:08
【问题描述】:
$result = $heidisql->prepare($sql);
$result->execute();

while($users = $result->fetch(PDO::FETCH_ASSOC)) {
     $db_userid = $users["id"];
     $db_user_firstname = $users["firstname"];
     $db_user_lastname = $users["lastname"];
     $db_user_username = $users ["username"];
     $db_email_address = $users["emailaddress"];
     $db_postal_address = $users ["postal_address"];
     $db_postal_code = $users["user_postal_code"];
     $db_gender_id = $users["gender_id"];
     $db_date_of_birth = $users["date_of_birth"];
     $db_telephone = $users["userlast_login_time"];
     $db_activation_account = $users["act_token_time"];
     $db_last_logintime = $users["userlast_login_time"];

     if ($users > 0) {

        $tfirst="<table id='example' class='display table table-striped table-bordered' >";

        $theadcoreFirst="<thead><tr><th>";
        $theadcoreMiddle="</th><th>";
        $theadcoreLast="</th></tr></thead>";

        $tbodycoreFirst="<tbody><tr><td>";
        $tbodycoreMiddle="</td><td>";
        $tbodycoreLast="</td></tr></tbody>";

        $tlast="</table>";                      

        $htmlPHP_table = $tfirst. $theadcoreFirst."User ID" .$theadcoreMiddle. "First Name" .$theadcoreMiddle. "Last Name" .$theadcoreMiddle. "User Name" .$theadcoreMiddle
                                                 . "Email Address" .$theadcoreMiddle. "Postal Address" .$theadcoreMiddle. "Postal Code" .$theadcoreMiddle
                                                 . "Gender" .$theadcoreMiddle. "Date of Birth" .$theadcoreMiddle. "Phone No" .$theadcoreMiddle
                                                 . "Active Account Time" .$theadcoreMiddle. "Last Login Time" .$theadcoreLast;

       // Parse the result set, and adds each row and colums in HTML table
       $htmlPHP_table .= $tbodycoreFirst .$db_userid. $tbodycoreMiddle
                                                 .$db_user_firstname .$tbodycoreMiddle. $db_user_lastname .$tbodycoreMiddle. $db_user_username. $tbodycoreMiddle
                                                 .$db_email_address .$tbodycoreMiddle. $db_postal_address. $tbodycoreMiddle. $db_postal_code. $tbodycoreMiddle
                                                 .$db_gender_id .$tbodycoreMiddle .$db_date_of_birth .$tbodycoreMiddle. $db_telephone. $tbodycoreMiddle
                                                 .$db_activation_account .$tbodycoreMiddle. $db_last_logintime .$tbodycoreLast;

    $htmlPHP_table .= $tlast; // ends the HTML table

     echo $htmlPHP_table;

     }


   }

我正在从我的数据库中获取结果,但它们看起来像这样

1)名字|姓氏|用户名|电子邮件地址|邮政地址|用户邮政编码|性别ID|出生日期|等等。

  • 那么这里只有1行数据!

2)名字|姓氏|用户名|电子邮件地址|邮政地址|用户邮政编码|性别ID|出生日期|等等。

  • 那么这里只有1行数据!

3)名字|姓氏|用户名|电子邮件地址|邮政地址|用户邮政编码|性别ID|出生日期|等等。

  • 那么这里只有1行数据,等等

代替:

名字|姓氏|用户名|电子邮件地址|邮政地址|用户邮政编码|性别ID|出生日期|等

  • 所有 ROWS 和 Columns 数据都应该出现在这里!?

  • 哦,还有如何限制要显示的列数(例如,如果我只想显示 100 个结果中的 10 个)?

【问题讨论】:

  • 好吧,我唯一能说的就是在循环上方创建table 和thead,并在循环内部输出rows..
  • 你能举出更多细节的例子吗?你的意思是我把 $htmlPHP_table = $tfirst 放在了 while 循环之上吗?所以我也应该把 theadcoreFirst 分解成两部分 和 ?
  • 表格和标题必须在循环之外
  • @JohnLaimuin 在下面试试我的答案

标签: php mysql while-loop


【解决方案1】:

您需要将表格开始和标题放在 while 循环之外。只有要迭代的数据才会在while循环中

$result = $heidisql->prepare($sql);
$result->execute();
if ($users > 0) {

    $tfirst="<table id='example' class='display table table-striped table-bordered' >";

    $theadcoreFirst="<thead><tr><th>";
    $theadcoreMiddle="</th><th>";
    $theadcoreLast="</th></tr></thead>";

    $tbodycoreFirst="<tbody><tr><td>";
    $tbodycoreMiddle="</td><td>";
    $tbodycoreLast="</td></tr></tbody>";

    $tlast="</table>";                      

    $htmlPHP_table = $tfirst. $theadcoreFirst."User ID" .$theadcoreMiddle. "First Name" .$theadcoreMiddle. "Last Name" .$theadcoreMiddle. "User Name" .$theadcoreMiddle
     . "Email Address" .$theadcoreMiddle. "Postal Address" .$theadcoreMiddle. "Postal Code" .$theadcoreMiddle
     . "Gender" .$theadcoreMiddle. "Date of Birth" .$theadcoreMiddle. "Phone No" .$theadcoreMiddle
     . "Active Account Time" .$theadcoreMiddle. "Last Login Time" .$theadcoreLast;
while($users = $result->fetch(PDO::FETCH_ASSOC)) {
     $db_userid = $users["id"];
     $db_user_firstname = $users["firstname"];
     $db_user_lastname = $users["lastname"];
     $db_user_username = $users ["username"];
     $db_email_address = $users["emailaddress"];
     $db_postal_address = $users ["postal_address"];
     $db_postal_code = $users["user_postal_code"];
     $db_gender_id = $users["gender_id"];
     $db_date_of_birth = $users["date_of_birth"];
     $db_telephone = $users["userlast_login_time"];
     $db_activation_account = $users["act_token_time"];
     $db_last_logintime = $users["userlast_login_time"];



       // Parse the result set, and adds each row and colums in HTML table
       $htmlPHP_table .= $tbodycoreFirst .$db_userid. $tbodycoreMiddle
                                                 .$db_user_firstname .$tbodycoreMiddle. $db_user_lastname .$tbodycoreMiddle. $db_user_username. $tbodycoreMiddle
                                                 .$db_email_address .$tbodycoreMiddle. $db_postal_address. $tbodycoreMiddle. $db_postal_code. $tbodycoreMiddle
                                                 .$db_gender_id .$tbodycoreMiddle .$db_date_of_birth .$tbodycoreMiddle. $db_telephone. $tbodycoreMiddle
                                                 .$db_activation_account .$tbodycoreMiddle. $db_last_logintime .$tbodycoreLast;



     }

     $htmlPHP_table .= $tlast; // ends the HTML table

     echo $htmlPHP_table;
   }

【讨论】:

  • 伙计,如果您希望正确获取它,您应该在循环之外执行$htmlPHP_table .= $tlast; 和echo,这将重复数据...
猜你喜欢
相关资源
最近更新 更多
热门标签