[英]PHP: SQL Data in HTML table not getting ordered
我正在尝试以两种方式对我的数据进行排序。 当我单击链接时,表格不会更新。
HTML:
echo "<tr>";
echo "<table id='customers'>
<thead>
<tr>
<th><a href='?orderBy=producto'> Producto</a></th>
<th>Unidad</th>
<th>Candidad Necesaria</th>
<th>Precio Objetivo por Unidad</th>
<th><a href='?orderBy=ctot_obj'>Valor Producto<a/></th>
<th>Remover Entrada</th>
</tr>
</thead>
PHP:
$orderBy = array('producto', 'ctot_obj');
$order = 'producto';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '.$order'");
我做错了什么?
你有一个错字:
$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '" . $order . "'");
删除.
从您的 sql 查询开始并尝试。
$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '{$order}'");
echo '
html 代码'
;
在 html 代码中使用"
而不是'
在 href 或./
使用#
echo '<tr><table id="customers">
<thead>
<tr>
<th><a href="./?orderBy=producto"> Producto</a></th>
<th>Unidad</th>
<th>Candidad Necesaria</th>
<th>Precio Objetivo por Unidad</th>
<th><a href="?orderBy=ctot_obj">Valor Producto<a/></th>
<th>Remover Entrada</th>
</thead>
为了实现这一点,在代码中进行一些更改。 假设您的页面名称是details.php
,因此进行以下更改:
<a href='details.php?orderBy=producto'>
现在通过在一个变量中获取它的值来在你的查询中使用这个orderBy
:
$orderBy = $_GET['orderBy'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.