2

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?

4 Answers 4

1

You have a typo:

$query_ord = $mysqli->query("SELECT * FROM busca ORDER BY '" . $order . "'");
Sign up to request clarification or add additional context in comments.

3 Comments

Not working. If I echo the query seems legit but apparently the table does not get reorganized.
Run the query output against MySQL directly -- do you get correct output? If so, then could you edit your question and add a code snippet where you iterate through $query_ord and echo the rows/cols? Issue might be there.
I fixed it. I had and early query that was creating an endless loop. Thanks!
0

Remove . fron your sql query and try.

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

Comments

0

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>

Comments

0

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'];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.