[英]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.