用PHP填充表格

时间:2013-09-16 18:06:59

标签: php html mysql sql mysqli

我的数据库中有一个表格如下:

IdUser | Name | Surname | 

另一张带有以下结构的表

IdPN | IdUser | PhoneNumber

此表的PK为IdPN,因此每个用户可以拥有多个电话号码。

现在我从第一个表中获取数据,用于在我的HTML页面中填充表格。我想要这样的东西:

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

所以第三个colume可以有多个数字。

我每个用户只有一个号码时使用的代码php如下:

<?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();
?>

我如何更改代码以显示表格(用户)的每一行的多个数字?

6 个答案:

答案 0 :(得分:1)

$sql = "SELECT PhoneNumber FROM PhoneNumbers WHERE IdUser = $user_id";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    echo $row['PhoneNumber'] . "<br />\n";
}

该代码块可用于显示由换行符分隔的$user_id的电话号码列表。只要定义了<td>,就可以放置在电话号码区域的$user_id标记内。

答案 1 :(得分:1)

更改您的SQL以包含IdUser列,如下所示:

SELECT users.IdUser, users.Name, users.Surname, phones.PhoneNumber
FROM users
LEFT JOIN phones ON (users.IdUser = phones.IdUser);

然后你可以使用PHP将手机分类为结构化阵列并从那里开始:

$aUsers = array();
$aUserPhones = array();

while ($row = mysql_fetch_assoc($result)) {
    if (!isset($aUsers)) {
        $aUsers['UserId'] = array(
            'nom'       =>  $nom,
            'cognom'    =>  $cognome,
            'phones'    =>  array($phone),
        );
    }
    else {
        $aUsers[$userId]['phones'][] = $phone;
    }

}

foreach ($aUsers as $aUser) {
    echo '<tr>';
    echo '<td>'.$aUser['nome'].'</td>'
    echo '<td>'.$aUser['cognome'].'</td>';
    echo '<td>';
    echo implode('<br>', $aUser['phones']);
    echo '</td>';
    echo '</tr>';
}

答案 2 :(得分:1)

我建议使用ID代替Name

您的数据会像这样回来:

name | Surname| numbers  |
_____|________|__________|
John | Doe    | 21212121 |
John | Doe    | 23424324 |
John | Doe    | ######## |
-------------------------- 

编辑:重新阅读问题,注意到您的表格结构

查询将变为:

            SELECT name, surname, phonenumber, phone_ct

            FROM   users as u 

                   join phone_numbers as ph
                      on ph.IdUser = u.IdUser 

                   join phone_numbers as ph
                      on ph.IdUser = u.IdUser 

                   join (select IdUser, 
                                count(IdPN) as phone_ct 
                         from phone_numbers
                         group by IdUser ) as ph_count   
                      on ph_count.IdUser = u.IdUser 

现在数据会变成:

name | Surname| numbers  | phone_ct
_____|________|__________|___________
John | Doe    | 21212121 |        3 |
John | Doe    | 23424324 |        3 | 
John | Doe    | ######## |        3 | 
-------------------------------------

编辑:更新的代码可能实际工作(大笑......抱歉)

代码将变为:

<?php
      $connectiondb->set_charset("utf8");
      $query = "
            SELECT name, surname, phonenumber, phone_ct

            FROM   users as u 

                   join phone_numbers as ph
                      on ph.IdUser = u.IdUser 

                   join phone_numbers as ph
                      on ph.IdUser = u.IdUser 

                   join (select IdUser, 
                                count(IdPN) as phone_ct 
                         from phone_numbers
                         group by IdUser ) as ph_count   
                      on ph_count.IdUser = u.IdUser ";

      if ($stmt = $connectiondb->prepare($query)) 
      {
         $stmt->execute();  
         $stmt->bind_result($nome, $cognome, $phonenumber, $phone_ct);

         $row_ct = 0; 

         while($stmt->fetch())
         { 
            $row_ct++ 

            // do every row for every section
            echo '<tr>';

            // first row of this section 
            if ($row_ct==1)                    
            { 
               echo "<td rowspan=$phone_ct>".$nome.'</td>';
               echo "<td rowspan=$phone_ct>".$cognome.'</td>';
            }

            // do every row for every section
            echo '<td>'.$phonenumber.'</td>';    
            echo '</tr>';

            // end of this section 
            if ($row_ct==$phone_ct) {           
              $row_ct = 0; 
            }   
        }

        $stmt->close();

     }  // end if $stmt 

&GT;

答案 3 :(得分:1)

如果你有两张桌子;

IdUser | Name | Surname  
IdPN | IdUser | PhoneNumber

我会做什么使用第一个表作为“主”表,第二个表作为辅助表。 IdUser将成为您主服务器中的PK,但是您的辅助服务器中的IdUser将成为主表中IdUser的外键(因为这两条信息是相同的,您实际上不应该在电话号码中添加对象在主表中没有相应条目的表。)您应该能够从两个表执行查询以显示所需的信息(例如,可能是完整连接)。作为一个例子;

select a.IdUser, a.Name, b.Surname, b.IdPN, b.PhoneNumber from <primary table> as a, <secondary table> as b where a.IdUser=b.IdUser;

这是一个例子,所以不要复制并粘贴它,但关键是如果两个表之间的关系是一致的,你应该能够执行一个查询来获得你需要的一切。

答案 4 :(得分:1)

您需要在输出到前端之前准备好记录集。

<?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();
}

你现在可以这样回应:

foreach($result as $row)
    echo '<tr><td>'.$row['name'].'</td><td>'.$row['surname'].'</td><td>'.implode(', ', $row['phonenumber'].'</td></tr>';

答案 5 :(得分:0)

执行类似

的查询
SELECT A.name, A.surname,GROUP_CONCAT(phonenumbers) as phone_number
FROM TABLE1 A 
JOIN TABLE2 B ON A.iduser = B.iduser
GROUP BY A.iduser

通过此查询,您将获得逗号分隔的电话号码

示例:(1123132132,123123133)

使用explode分割

$get_number = explode(" ", $number);

现在,您将获得数组$get_number中的数字。

获取数量
$row_span = count($get_number); 

现在你将拥有行间距。使用foreach,您可以在表格中打印数字。 所以你的td会像

<td rowspan="<?php echo $row_span; ?"> 

我希望这会对你有所帮助。如果需要任何帮助,请告诉我