简体   繁体   中英

PHP: SQL Data in HTML table not getting ordered

I am trying to order my data in two ways. When I click the link the table does not update.

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'");

What I am doing wrong?

你有一个错字:

$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '" . $order . "'");

Remove . fron your sql query and try.

$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '{$order}'");

echo ' html code ' ;

in html code use " instead of '

use # in href or ./

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>

To achieve this make some changes in the code. Suppose your page name is details.php , so make following changes:

<a href='details.php?orderBy=producto'>

Now use this orderBy in your query by fetching its values in a variable like:

$orderBy = $_GET['orderBy'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM