如何在PHP中使用html代码来适应这些值?
我试图将MySQL表中的值转换为PHP,我使用的是HTML代码,但我不确定如何显示这些值 这是我的密码:如何在PHP中使用html代码来适应这些值?,php,html,mysql,html-table,Php,Html,Mysql,Html Table,我试图将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
$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;
}
$codigoHTML='';
$consultaSucursales=“从empresa中选择id、empresa”;
$ejecutar=mysql\u查询($consultaSucursales);
而($fila=mysql\u fetch\u数组($ejecutar))
{
$codigoHTML.='。$fila['empresa'].';
$respuesta=datos($fila['id']);
$codigoHTML.=$respuesta;
}
$codigoHTML.='
';
echo$codigoHTML;
函数数据($id\u sucursal)
{
$consultaCantidades=“从producto中选择cantidad,其中id\u sucursal='$id\u sucursal';
$ejecutar2=mysql\u查询($consultacantides);
$codigoHTML2=“”;
而($fila2=mysql\u fetch\u数组($ejecutar2))
{
$codigoHTML2.='';
$codigoHTML2.='.$fila2['cantidad'.'”;
$codigoHTML2.='';
}
返回$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>
笔名
这将生成以下输出:
笔名
empresa 1
empresa 2
empresa 3
1.
2.
7.
9
2.
56
5.
8.
3.
78
5.
9
4.
100
100
5.
1000
感谢大家,在做了一些更改后,我决定将另一个表作为“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;
}
$codigoHTML='';
$consultaSucursales=“从empresa中选择id、empresa”;
$ejecutar=mysql\u查询($consultaSucursales);
$codigoHTML.='Producto';
而($fila=mysql\u fetch\u数组($ejecutar))
{
$codigoHTML.='。$fila['empresa'].';
}
$consultaCods=“从producto中选择不同的cod”;
$ejecutaC=mysql\u查询($consultaCods);
while($filac=mysql\u fetch\u数组($ejecutaC))
{
$cod=$filac['cod'];
$codigoHTML.='';
$codigoHTML.=''.$cod';
$consultaSucursales=“从empresa中选择id、empresa”;
$ejecutar=mysql\u查询($consultaSucursales);
而($fila=mysql\u fetch\u数组($ejecutar))
{
$id_sucursal=$fila['id'];
$respuesta=datos($id_sucursal,$cod);
$codigoHTML.=$respuesta;
}
$codigoHTML.='';
}
$codigoHTML.='
';
echo$codigoHTML;
函数数据($id\u sucursal,$cod)
{
$consultacantides=“从producto中选择cantidad,其中id_sucursal='$id_sucursal'和cod='$cod';
$ejecutar2=mysql\u查询($consultacantides);
$codigoHTML2=“”;
而($fila2=mysql\u fetch\u数组($ejecutar2))
{
$codigoHTML2.='.$fila2['cantidad'.'”;
}
返回$codigoHTML2;
}
表product中的列Id不在表中就是一个例子。具体出了什么问题?我显示的数据不符合我在图片中想要的内容。我无法执行您的代码,因此我不确定出了什么问题。您当前的代码是做什么的,它与您想要的有什么不同?当周期结束时,下一个“empresa”将显示在最新结果的下面,我想要的是以每个公司的列的形式将其显示在一旁,如图所示