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

7lrncoxx  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(374)

我试图将mysql表中的值容纳到php中,我使用的是html代码,但我不确定如何显示这些值。


这是我的密码:

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

igsr9ssn1#

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

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>

我也强烈建议使用 mysqli 或者 PDO ,作为 mysql_* 函数被折旧。

hmae6n7t

hmae6n7t2#

感谢大家,在做了一些更改之后,我决定将另一个表作为透视表,即“producto”,如果我添加一个新的“empresa”,它将动态显示
结果如下结果如下
代码如下

$codigoHTML= '<table width="100%" border="1" class="table table-hover">';
$consultaSucursales = "SELECT id,empresa FROM empresa";
$ejecutar = mysql_query($consultaSucursales);
$codigoHTML.= '<td><strong><center>Producto</center></strong></td>';
while($fila = mysql_fetch_array($ejecutar))
{
    $codigoHTML.= '<td><strong><center>'.$fila['empresa'].'</center></strong></td>';
}

$consultaCods = "SELECT DISTINCT cod FROM producto";
$ejecutaC = mysql_query($consultaCods);
while($filac = mysql_fetch_array($ejecutaC))
{
    $cod = $filac['cod'];
    $codigoHTML.='<tr>';
    $codigoHTML.= '<td>'.$cod.'</td>';
       $consultaSucursales = "SELECT id,empresa FROM empresa";
       $ejecutar = mysql_query($consultaSucursales);
       while($fila = mysql_fetch_array($ejecutar))
       {
           $id_sucursal = $fila['id'];
           $respuesta = datos($id_sucursal,$cod);
           $codigoHTML.= $respuesta;
       }
    $codigoHTML.='</tr>';
}

$codigoHTML.='
</table>';

echo $codigoHTML;

function datos($id_sucursal,$cod)
{
    $consultaCantidades = "SELECT cantidad FROM producto WHERE id_sucursal = '$id_sucursal' AND cod = '$cod'";
    $ejecutar2 = mysql_query($consultaCantidades);
    $codigoHTML2 = "";

    while($fila2 = mysql_fetch_array($ejecutar2))
    {                              
        $codigoHTML2.= '<td>'.$fila2['cantidad'].'</td>';           
    }
    return $codigoHTML2;
}

相关问题