1

我的数据库中有一个这样的表:

IdUser | Name | Surname | 

还有另一个具有以下结构的表

IdPN | IdUser | PhoneNumber

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

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

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

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

当我为每个用户只有一个号码时,我使用的代码 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();
?>

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

4

6 回答 6

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定义,放置在您的电话号码区域的标签内就可以了。

于 2013-09-16T18:13:54.247 回答
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>';
}
于 2013-09-16T18:22:49.793 回答
1

我建议使用 aID而不是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 

?>

于 2013-09-16T18:33:30.767 回答
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;

这是一个例子,所以不要复制粘贴进去,但重点是,如果两个表之间的关系是一致的,你应该可以执行一个查询来得到你需要的一切。

于 2013-09-16T18:34:18.853 回答
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>';
于 2013-09-16T19:03:59.677 回答
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; ?"> 

我希望这能帮到您。如果需要任何帮助,请告诉我

于 2013-09-16T18:32:11.177 回答