简体   繁体   中英

Best way to Show data

I have a table with ID, firstname, status and above the table I want 4 links that read all, hot, warm, cold. By clicking on the link the table below will only show those users whose status = hot. Is it best to create new pages in php to display all the users for the corresponding status or is there a better way to do it so that it's all done one a single page?

Thank you in advance!

The decision to use Javascript would come down to how many records you will have displayed at once. If you have a lot of rows in that table your HTML will become quite large and manipulating that with JS will take a lot of resources.

As mentioned, I would recommend passing an argument to the PHP script to filter users with the correct status, something similar to the following:

HTML

<div class="filter-status">
    <a href="/script.php?status=all">All</a> -
    <a href="/script.php?status=hot">Hot</a> -
    <a href="/script.php?status=warm">Warm</a> -
    <a href="/script.php?status=cold">Cold</a>
</div>

PHP SCRIPT

// check the status is set in the arguments, and that its something we expect
if(isset($_GET['status']) 
    && in_array($_GET['status'], array('all', 'hot', 'warm', 'cold'))){
    $status = $_GET['status'];
}
else {
    $status = 'all'; // default to show all rows
}

// get the rows from the database based on the status
$query = "SELECT * FROM table ";
if($status !== 'all')
    $query .= "WHERE status = '".$status."'";

$query .= ';';

// do your mysql query as normal

Then if your script was located at www.domain.com/script.php you could filter all hot ones by visiting www.domain.com/script.php?status=hot .

For that case, you can do 2 things,

  1. you can use Javascript to get the value of the "status" td of each row, and only display those rows.

  2. you can use PHP and Ajax to grab that content from a different page and have jQuery (because it so much more comfortable then plain ol' javascript) to grab the result.

I'll go with option #2, because it would be better on larger tables where you might want to limit results or save resources.

/* JavaScript */
//NOTE THIS IS JQUERY, NOT REGULAR JAVASCRIPT!
//YOU MUST INCLUDE THE JQUERY LIBRARY BEFORE USING THIS!

$(document).ready(function() { //When DOM is ready.
    $table = $('#status-table'); //Cache the table, no need to select it every time.
    $('a.status').click(function() { //Add click handler, "when the link is clicked.."
        status = $(this).text(); //Status = the text it has <a>TEXT</a>
        $.get(
            "filter-status.php", //Target
            'status='+status, //Data
            function(data) { //Callback
                $table.hide().empty().append(data).slideDown();
            }
        );
    })
})

And have the PHP to generate the rows for the table.

you should do it in single page only.. this is the best practice. I am sure you are reloading page on button's click then you should fire query in which you will have condition

$sql = "SELECT * FROM TABLE WHERE 1=1".(isset($_REQUEST['status'])? " AND status = '".$_REQUEST['status']."'":""); 

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