dsa99349 2013-09-16 18:06
浏览 87
已采纳

用PHP填充表格

I have a table in my database like this:

IdUser | Name | Surname | 

And another table with the following strucutre

IdPN | IdUser | PhoneNumber

The PK of this table is IdPN, so each User can have more than one Phone number.

Now I'm getting the data from the first table for pupulating a table in my HTML page. I want something like this:

name | Surname| numbers  |
_____|________|__________|
John | Doe    | 21212121 |
     |        | 23424324 |
_____|________|__________|
Frank|Johnson | 23222111 |
     |        | 23747347 |
     |        | 36363636 |
_____|________|__________|

So the third colume can have more than one numbers.

The code php that I was using when I had just one number for each user, is the following:

<?php
      $connectiondb->set_charset("utf8");
      $query= "SELECT name, surname, phonenumber
      FROM users";
        if ($stmt = $connectiondb->prepare($query)) {
          $stmt->execute();

      $stmt-bind_result($nome,$cognome,$phonenumber);
            while($stmt->fetch()){
      echo '<tr>';
      echo '<td>'.$nome.'</td>';
      echo '<td>'.$cognome.'</td>';
      echo '<td>'.$phonenumber.'</td>';
      echo '</tr>';

}
$stmt->close();
?>

How I have to change my code for showing multiple number for each row of the table (user)?

  • 写回答

6条回答 默认 最新

  • dq_1984 2013-09-16 19:03
    关注

    You need to prepare your recordset before being output to the front end.

    <?php
    $connectiondb->set_charset("utf8");
    $query= "SELECT users.IdUser, users.Name, users.Surname, phones.PhoneNumber
             FROM users
             LEFT JOIN phones ON (users.IdUser = phones.IdUser);";
    if ($stmt = $connectiondb->prepare($query)) {
        $stmt->execute();
    
        $stmt->bind_result($id, $nome, $cognome, $phonenumber);
    
        /*
          Recordset will be 
          |1|Jim|Bowen|07235876583
          |1|Jim|Bowen|07734952651
          |2|Jim|Davidson|07275493085
          |2|Jim|Davidson|07197649387
          |2|Jim|Davidson|07030190594       
    
         */
        $result = array();
        while($stmt->fetch())
        {
            if(!isset($result[$id]))
            {
                // save any previous person in $result
                if(is_array($temp)) $result[$id] = $temp; 
    
                // start a fresh person
                $temp = array();
    
                // first time to come accross this person
                $temp['id'] = $id;
                $temp['name'] = $nome;
                $temp['surname'] = $cognome;
                $temp['phonenumbers'] = array($phonenumber);
            }
            else
            {
                // if we've already come across this person, only store his/her next phonenumber in the array
                $temp['phonenumbers'][] = $phonenumber
            }
        }
    
        /* the $result array will now look thus
        array (size=2)
          0 => 
            array (size=4)
              0 => string '1' (length=1)
              1 => string 'Jim' (length=3)
              2 => string 'Bowen' (length=5)
              3 => 
                array (size=2)
                  0 => string '07235876583' (length=11)
                  1 => string '07734952651' (length=11)
          1 => 
            array (size=4)
              0 => string '2' (length=1)
              1 => string 'Jim' (length=3)
              2 => string 'Davidson' (length=8)
              3 => 
                array (size=3)
                  0 => string '07275493085' (length=11)
                  1 => string '07197649387' (length=11)
                  2 => string '07030190594' (length=11)
        */
    $stmt->close();
    }
    

    You can now echo out like this:

    foreach($result as $row)
        echo '<tr><td>'.$row['name'].'</td><td>'.$row['surname'].'</td><td>'.implode(', ', $row['phonenumber'].'</td></tr>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀