简体   繁体   中英

Display data using while loop from SQL database php dpo into html table not working

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

     }


   }

I am getting the results from my db but they appear like this

1)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here!

2)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here!

3)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here, etc

Instead of:

firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc

  • All ROWS and Columns data should appear here!?

  • Oh also how to limit the number of column to be display (eg if I only want 10 out of 100 result to be display)?

You need to put table starting and headings outside while loop. Only data to iterate will be in while loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM