我试图将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;
}
答案 0 :(得分:0)
我建议您构建一个可以轻松输出的行数组 我正在考虑这样的结构:
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_*
functions are depreciated。
答案 1 :(得分:0)
感谢大家,在做了一些更改之后,我决定将另一张桌子作为“producto”的支点,如果我添加一个新的“empresa”,它将被动态显示
结果如下 the result is the following
代码如下
$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;
}