php - 如何在 PHP 中使用 html 代码来容纳这些值?

标签 php html mysql html-table

我正在尝试将 MySQL 表中的值容纳到 PHP,我正在使用 HTML 代码,但我不确定如何显示这些值。

enter image description here

enter image description here

这是我的代码:

$codigoHTML= '<table width="100%" border="1" class="table table-

hover">';
$consultaSucursales = "SELECT id,empresa FROM empresa";
$ejecutar = mysql_query($consultaSucursales);
while($fila = mysql_fetch_array($ejecutar))
{
    $codigoHTML.= '<td><strong><center>'.$fila['empresa'].'</center></strong></td>';
    $respuesta = datos($fila['id']);
    $codigoHTML.= $respuesta;
}

$codigoHTML.='
</tbody>
</table>';

echo $codigoHTML;

function datos($id_sucursal)
{
    $consultaCantidades = "SELECT cantidad FROM producto WHERE id_sucursal = '$id_sucursal'";
    $ejecutar2 = mysql_query($consultaCantidades);
    $codigoHTML2 = "";
    while($fila2 = mysql_fetch_array($ejecutar2))
    {
        $codigoHTML2.= '<tr>';                   
            $codigoHTML2.= '<td>'.$fila2['cantidad'].'</td>';
        $codigoHTML2.= '</tr>';    
    }
    return $codigoHTML2;
}

最佳答案

我建议构建一个可以轻松输出的行数组。
我正在考虑这样的结构:

EMPRESA
(
    [1] => empresa 1
    [2] => empresa 2
    [3] => empresa 3
)

CANTIDAD
(
    [1] => Array
        (
            [1] => 2
            [2] => 7
            [3] => 9
        )

    [2] => Array
        (
            [1] => 56
            [2] => 5
            [3] => 8
        )

    [3] => Array
        (
            [1] => 78
            [2] => 5
            [3] => 9
        )

    [4] => Array
        (
            [1] => 100
            [3] => 100
        )

    [5] => Array
        (
            [3] => 1000
        )

)

以下是我构建这些数组的方法:

<?php

$empresa=$cantidad=array();

$consultaSucursales = "SELECT e.`id`,e.`empresa`,p.`cantidad`
                       FROM `empresa` e
                       LEFT JOIN `producto` p ON (p.`id_sucursal`=e.`id`)
                       WHERE 1;";

$ejecutar = mysql_query($consultaSucursales);

$row=0;
while($fila = mysql_fetch_array($ejecutar)) {
  $row++;
  if (empty($empresa[$fila['id']])) {
    $empresa[$fila['id']]=$fila['empresa'];
  }
  $cantidad[$row][$fila['id']]=$fila['cantidad'];
}

?>

然后输出数据:

<table width="100%" border="1" class="table table-hover">
  <thead>
    <tr>
      <th>Nom</th>
      <th><?=implode('</th><th>',$empresa);?></th>
    </tr>
  </thead>
  <tbody><?php

    foreach ($cantidad as $row_number => $this_row) {

      ?><tr>
        <td><?=$row_number?></td><?php

        foreach (array_keys($empresa) as $empresa_id) {
          ?><td><?=$this_row[$empresa_id]?:''?></td><?php
        }

      ?></tr><?php

    }

  ?></tbody>
</table>

生成以下输出:

<table class="table table-hover" width="100%" border="1">
  <thead>
    <tr>
      <th>Nom</th>
      <th>empresa 1</th>
      <th>empresa 2</th>
      <th>empresa 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>7</td>
      <td>9</td>
    </tr>
    <tr>
      <td>2</td>
      <td>56</td>
      <td>5</td>
      <td>8</td>
    </tr>
    <tr>
      <td>3</td>
      <td>78</td>
      <td>5</td>
      <td>9</td>
    </tr>
    <tr>
      <td>4</td>
      <td>100</td>
      <td></td>
      <td>100</td>
    </tr>
    <tr>
      <td>5</td>
      <td></td>
      <td></td>
      <td>1000</td>
    </tr>
  </tbody>
</table>

我还强烈建议使用 mysqliPDO ,如mysql_* functions are depreciated .

关于php - 如何在 PHP 中使用 html 代码来容纳这些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49947401/

相关文章:

HTML/CSS div 样式溢出

php - httpd、myslqd 和 SELinux

PHP SoapClient 版本不匹配

html - 使 flex 元素从下到上

php - 获取分组的 HTML 数据到 PHP 数组? (比如复选框的子元素)

通过单击不同的图像来更改文本区域的值的 JavaScript 代码

php - 获取日期格式未知的两个日期之间的数据

mysql - 剖析 MySQL InnoDB 记录格式以从原始磁盘恢复

php - 在点击功能 joomla 上为文章添加 1 次点击

php - Symfony2 无法在 Debian Linux 上转储 Assets