这对于专业人士来说很难,但很容易。
我在mysql中有以下字段:产品名称,mycost,sellprice和stock。因为我需要知道每个产品在我的商店投资有多少,所以我创建了后续计算并且工作正常:
<td><?php echo $product['name'] ?></td>
<td><?php echo $product['stock'] ?></td>
<td><?php echo $product['mycost'] ?></td>
<td><?php echo $product['sellprice'] ?></td>
这里我们根据mycost(我为产品支付的价格)*库存计算我在商店里的投资(金钱)多少钱
<td><?php echo number_format($product['mycost'] * $product['stock'],0,',','.'); ?></td>
它工作正常,如果产品的mycost为10且库存为100,那么我即时获得的结果是投资为1000
现在因为产品的结果(1000)是动态的(不存储在mysql中)我有以下问题:
搜索结果显示例如3种不同的产品,3种不同的动态投资结果非存储在mysql中。如何将此结果汇总并显示在页面顶部?
如果结果存储在mysql中很容易,但因为它是这样的:
<td><?php echo number_format($product['mycost'] * $product['stock'],0,',','.'); ?></td>
我认为没办法做到这一点,为什么我在这里问专业人士
有什么想法吗?
编辑: 这是完整的代码
<?php
$cualcategoria = mysqli_real_escape_string($database,$_GET['categoria']);
$verproductos = mysqli_query($database, "SELECT * FROM `productos`");
while($mostrar = mysqli_fetch_assoc($verproductos)) {
?>
<table class="table1000">
<tr>
<td style="width:120px;"><div id="rightImage"><img style="border:2px solid #cccccc;" alt="image" src="fotos/<?php echo $mostrar['codigo'] ?>.jpg" width="90" height="90"></div></td>
<td valign="top" style="width:900px;"><table class="table900">
<tr>
<td class="header150">Producto</td>
<td class="header150">Marca</td>
<td class="header150">Modelo</td>
<td class="header150">Cantidad</td>
<td class="header150">Capital</td>
<td class="header150">Código</td>
</tr>
<tr>
<td class="header150small"><?php echo $mostrar['producto'] ?></td>
<td class="header150small"><?php echo $mostrar['marca'] ?></td>
<td class="header150small"><?php echo $mostrar['modelo'] ?></td>
<td class="header150small"><?php echo number_format($mostrar['stock'],0,',','.'); ?></td>
<td class="header150small">$ <?php echo number_format($mostrar['costo'] * $mostrar['stock'],0,',','.'); ?></td>
<td class="header150small"><a class="header150small" href="generar.php?codigo=<?php echo $mostrar['codigo'] ?>"><?php echo $mostrar['codigo'] ?></a></td>
</tr>
<tr>
<td class="header150">Precio Costo</td>
<td class="header150">Precio Neto</td>
<td class="header150">IVA Incluido</td>
<td class="header150">Ganancia</td>
<td class="header150">Ganancia Total</td>
<td class="header150">Proveedor</td>
</tr>
<tr>
<td class="header150small">$ <?php echo number_format($mostrar['costo'],0,',','.'); ?></td>
<td class="header150small">$ <?php echo number_format($mostrar['venta'],0,',','.'); ?></td>
<td class="header150small">$ <?php echo number_format($mostrar['coniva'],0,',','.'); ?></td>
<td class="header150small">$ <?php echo number_format($mostrar['venta'] - $mostrar['costo'],0,',','.'); ?>
</td>
<td class="header150small">$ <?php echo number_format(($mostrar['venta'] - $mostrar['costo']) * $mostrar['stock'],0,',','.'); ?></td>
<td class="header150small"><?php echo $mostrar['proveedor'] ?></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
?>
答案 0 :(得分:1)
Muy bien,maradoiano,tuve que realizar algunas acrobacias para corrertucódigo,lacosaestáasí:
$total
。$tabla
。 Éstaseráunavariable enorme porque concatena el <table>
,los <tr>
y los <td>
,ademásdelos datosqueéstoscontienen。$total
ydespués$tabla
。Elsiguientecódigohacelo anterior,notas:
foreach
。 Maradoiano debe comentarlo y volver a usar su while
。Este eselcódigo:
<?php
//$cualcategoria = mysqli_real_escape_string($database,$_GET['categoria']);
//$verproductos = mysqli_query($database, "SELECT * FROM `productos`");
$verproductos = Array( Array( "codigo" => "0401",
"producto" => "tenis",
"marca" => "Nike",
"modelo" => "Air",
"stock" => 50,
"costo" => 700,
"venta" => 800,
"coniva" => 125,
"proveedor" => "Importadora de Zapatos"
),
Array( "codigo" => "0992",
"producto" => "camisa",
"marca" => "Eiffel",
"modelo" => "",
"stock" => 14,
"costo" => 250,
"venta" => 300,
"coniva" => 60,
"proveedor" => "Importadora de Camisas"
),
Array( "codigo" => "5800",
"producto" => "anteojos",
"marca" => "Bay",
"modelo" => "Solar",
"stock" => 40,
"costo" => 400,
"venta" => 550,
"coniva" => 95,
"proveedor" => "Importadora de Anteojos"
)
);
$tabla = '<table class="table1000">';
$total = 0;
//while ( $mostrar = mysqli_fetch_assoc( $verproductos ) )
foreach ( $verproductos as $mostrar )
{ $tabla .= '<tr>' .
' <td style="width:120px;">' .
' <div id="rightImage"><img style="border:2px solid #cccccc;" alt="image"' .
' src="fotos/' . $mostrar['codigo'] . '.jpg" width="90" height="90"></div></td>' .
' <td valign="top" style="width:900px">' .
' <table class="table900">' .
' <tr>' .
' <td class="header150">Producto</td>' .
' <td class="header150">Marca</td>' .
' <td class="header150">Modelo</td>' .
' <td class="header150">Cantidad</td>' .
' <td class="header150">Capital</td>' .
' <td class="header150">Código</td>' .
' </tr>' .
' <tr>' .
' <td class="header150small">' . $mostrar['producto'] . '</td>' .
' <td class="header150small">' . $mostrar['marca'] . '</td>' .
' <td class="header150small">' . $mostrar['modelo'] . '</td>' .
' <td class="header150small">' . number_format($mostrar['stock'],0,',','.') . '</td>' .
' <td class="header150small">$' .
number_format($mostrar['costo']*$mostrar['stock'],0,',','.') . '</td>';
$total += $mostrar['costo'] * $mostrar['stock'];
$tabla .= ' <td class="header150small"><a class="header150small"' .
' href="generar.php?codigo=' . $mostrar['codigo'] . '">' . $mostrar['codigo'] . '</a></td>' .
' </tr>' .
' <tr>' .
' <td class="header150">Precio Costo</td>' .
' <td class="header150">Precio Neto</td>' .
' <td class="header150">IVA Incluido</td>' .
' <td class="header150">Ganancia</td>' .
' <td class="header150">Ganancia Total</td>' .
' <td class="header150">Proveedor</td>' .
' </tr>' .
' <tr>' .
' <td class="header150small">$' . number_format($mostrar['costo'],0,',','.') . '</td>' .
' <td class="header150small">$' . number_format($mostrar['venta'],0,',','.') . '</td>' .
' <td class="header150small">$' . number_format($mostrar['coniva'],0,',','.') . '</td>' .
' <td class="header150small">$' . number_format($mostrar['venta'] - $mostrar['costo'],0,',','.') .
' </td>' .
' <td class="header150small">$' . number_format(($mostrar['venta'] - $mostrar['costo']) *
$mostrar['stock'],0,',','.') . '</td>' .
' <td class="header150small">' . $mostrar['proveedor'] . '</td>' .
' </tr>' .
' </table>' .
' </td>' .
'</tr>' .
'<tr><td colspan="2"><hr/></td></tr>';
}
echo "Inversión total = " . $total .
"<br/>" .
$tabla . '</table>';
?>
答案 1 :(得分:0)
一种方法是使用变量在旅途中总结(这里我使用totalInvestment作为变量)
$totalInvestment = 0;
foreach($products as $product):
$totalInventsment += product['mycost'] * $product['stock']; ?>
...
<td><?php echo $product['name'] ?></td>
<td><?php echo $product['stock'] ?></td>
<td><?php echo $product['mycost'] ?></td>
<td><?php echo $product['sellprice'] ?></td>
...
<?php endforeach;
然后你可以像这样回复它
<?php echo number_format($totalInventsment,',','.'); ?>
修改强> 如果你想要它在顶部你可以在文件的顶部添加它(将使网站加载慢一点(x)
$totalInvestment = 0;
foreach($products as $product){
$totalInventsment += product['mycost'] * $product['stock'];
}
echo number_format($totalInventsment,',','.');